简体   繁体   English

在Java中使用本机代码

[英]Using native code in java

I want to create c library and use it in my java code on an Linux OS. 我想创建c库,并在Linux OS上的Java代码中使用它。 I'm trying to understand and implement natural library concept. 我正在尝试理解和实现自然图书馆的概念。 I'm following this tutorial http://diglib.stanford.edu:8091/~testbed/doc/JavaUsage/JNI/tutorial.txt 我正在关注本教程http://diglib.stanford.edu:8091/~testbed/doc/JavaUsage/JNI/tutorial.txt

Which is helpful me to understand concept a little. 这有助于我稍微了解一下概念。 However, I get errors when I try to do it myself. 但是,当我自己尝试执行此操作时会出错。 I searced for errors I am getting but none of solutions helped. 我搜寻到我遇到的错误,但是没有解决方案有帮助。

Main class code and class for natural library I wrote is as follows: 我编写的自然库的主要类代码和类如下:

package natLib;
import natLib.getKeyPressed;
public class main {
public static void main(String[] args) {
    getKeyPressed natlab=new getKeyPressed();
    char c=natlab.keyboardPressedKey();

}
}

package natLib;    
public class getKeyPressed {
static {
 System.loadLibrary("natlab");
    }
public native char keyboardPressedKey();
}

when I write "javac main.java" I get errors like 当我写“ javac main.java”时,我得到类似的错误

"main.java:6: error: cannot find symbol getKeyPressed natlab=new getKeyPressed();" “ main.java:6:错误:找不到符号getKeyPressed natlab = new getKeyPressed();”

And when I skip for main and just do javac prcess for class with native method, try to obtain a header file javah -jni getKeyPressed.class 当我跳过main并只使用本机方法为类做javac prcess时,尝试获取头文件javah -jni getKeyPressed.class

Although there is a file as getKeyPressed.class, I get errors like: 尽管有一个名为getKeyPressed.class的文件,但出现如下错误:

"Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class name:     getKeyPressed.class"

I try it without .class extention it says 我尝试没有.class扩展它说

"Error: Could not find class file for 'getKeyPressed'."

It says that even when I make getKeyPressed class file by copying getKeyPressed.class. 它说,即使我通过复制getKeyPressed.class来制作getKeyPressed类文件。

It seems I am making a major mistake, any suggestions to solve this? 看来我犯了一个重大错误,对此有什么建议吗?

javah expects a fully qualified classname. javah需要完全限定的类名。 (eg natLib.getKeyPressed, not just getKeyPressed) (例如natLib.getKeyPressed,而不仅仅是getKeyPressed)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM