我在java中写了如下的函数:
class MyJniTest()
{
  public native void ShowHello();
  public static main()
  {
    System.out.print("begin\n");
    new MyJniTest().ShowHello();
    System.out.print("end\n");
  }
}
//在c++中的函数如下
JNIEXPORT void JNICALL Java_MyJniTest_ShowHello
  (JNIEnv *, jobject)
{
   printf("hello\n");
}
//但是执行结果确是:
begin
end
hello
hello 应该在begin 和 end 之间但确跑到最后去了,为什么?

解决方案 »

  1.   

    你的dll怎么没有在程序中载入??
      

  2.   

    是有点奇怪,难道运行 DLL 中的函数是用的新线程?没理由啊!
      

  3.   

    调用方法用LoadLibrary如下:
    class MyJniTest()
    {
      public native void ShowHello();  static
      {
         System.LoadLLibrary("hello");
       }
      public static main()
      {
        System.out.print("begin\n");
        new MyJniTest().ShowHello();
        System.out.print("end\n");
      }
    }
      

  4.   

    public class MyJniTest(){  static{
      System.loadLibrary("hello"); // hello.dll的文件名
      }  public native void ShowHello();  public static main(){
        System.out.print("begin\n");
        new MyJniTest().ShowHello();
        System.out.print("end\n");
      }}
      

  5.   

    我发现非常奇怪,好像是在程序退出时才执行那个函数的,如果程序不退出一直都看不到hello的输出
      

  6.   

    yaray大哥,按照您的写法试了,还是不行。我用的是jbuilder9会不会有问题呀?
      

  7.   

    这是流buffer的因素,你清buffer看看
      

  8.   

    public native String ShowHello();
    你让native返回一个jstring就会发现是立即执行的