线程“main”java.lang中的异常。如果3

时间:2022-11-17 17:33:39

Hi guys i have tried all the solutions like java -Djava.library.path=. demo adding the dll path to PATH java -Djava.library.path=c:\JNI\demo.dll demo

我已经尝试了所有的解决方案,如java -Djava.library.path=。演示添加dll路径到path java -Djava.library.path=c:\JNI\demo。dll演示

But still the above error.

但仍然是上面的错误。

Here is my java code..

这是我的java代码。

class demo
{
    public native void printline();
    public static void main(String[]args)
    {
        new demo().printline();
    }
}

Here is my c code...

这是我的c代码…

#include<stdio.h>
#include<jni.h>

#include "demo.h"

JNIEXPORT void JNICALL Java_demo_printline(JNIEnv *a, jobject b)
{
    printf("Hello wrold!!!");
    return;
}

Steps for compiling and running,

编译和运行的步骤,

  1. javac demo.java
  2. javac demo.java
  3. javah demo
  4. javah演示
  5. gcc -c -I"c:\jdk1.7.0_55\include" -I"c:\jdk1.7.0_55\include\win32" demo.c
  6. c -I -c:\jdk1.7.0_55\包括" -I"c:\jdk1.7.0_55\包括\win32" demo.c。
  7. gcc -Wl,--add-stdcall-alias -shared -o demo.dll demo.c
  8. gcc -Wl,- add-stdcall-alias -shared -o演示。dll demo.c
  9. java -Djava.library.path=c:\JNI\demo.dll demo
  10. java -Djava.library.path = c:\ JNI \ demo。dll演示

Am i going wrong somewhere?

我哪里出错了吗?

can someone please help me out,.

有人能帮我吗?

2 个解决方案

#1


1  

Try Run-Time Loading of the dll file within the java code in a static block like:

在静态块中,尝试在java代码中运行dll文件的运行时加载:

static
{
System.loadLibrary("demo");
}

should give you the output.

应该给你输出。

Moreover make sure that dll file generated is x32 or x64 according to the gcc compiler in use.

此外,根据使用的gcc编译器,确保生成的dll文件是x32或x64。

#2


0  

looking for "JNI hello world" (or many other terms, possibly), would have given you the answer.

寻找“JNI hello world”(或者其他许多术语,可能),会给你答案。

for example:

例如:

http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

  • you need to load the library inside your java code
  • 您需要在java代码中加载该库。
  • you need to specify the path to the directory of the library, not to the library itsself, in java.library.path
  • 您需要为库的目录指定路径,而不是在java.library.path中指定库本身。

#1


1  

Try Run-Time Loading of the dll file within the java code in a static block like:

在静态块中,尝试在java代码中运行dll文件的运行时加载:

static
{
System.loadLibrary("demo");
}

should give you the output.

应该给你输出。

Moreover make sure that dll file generated is x32 or x64 according to the gcc compiler in use.

此外,根据使用的gcc编译器,确保生成的dll文件是x32或x64。

#2


0  

looking for "JNI hello world" (or many other terms, possibly), would have given you the answer.

寻找“JNI hello world”(或者其他许多术语,可能),会给你答案。

for example:

例如:

http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

  • you need to load the library inside your java code
  • 您需要在java代码中加载该库。
  • you need to specify the path to the directory of the library, not to the library itsself, in java.library.path
  • 您需要为库的目录指定路径,而不是在java.library.path中指定库本身。