netbeans jni java 类库,About how to access native method (or local library) in JAVA program through JN...

施令雪
2023-12-01

Recently, I am planning to accelerate JAVA program on FPGA-based System. Due to the special characteristics of JAVA platform, one key point is  how to access the lowest level resources (like CPU or peripherals' registers)  in JAVA . It is clear that owe to the limit of JVM, it is hard for JAVA  API  to access such native resources. However, JAVA also provides some mechanisms like JNI (Java Native Interface), JNA (Java Native Access, an update version of JNI) to do such jobs. After searching around this week, there are several different ways and platforms to do this. I will list this methods and show the problems I have encountered.

My platform:

(1) WIN7 + AMD Quad-core + NetBeans 6.9.1 + JDK 1.6.0.21_X64 + Visual Studio 2010

(2) WIN-XP + Intel Dual-Core + NetBeans 6.9.1 +JDK 1.6.0_21_WIN32 + Visual Studio 2010

1. JNI with NetBeans IDE and C/C++ Plugin on Linux

NetBeans is an integrated develop environment provided freely by SUN (now ORACLE) and intended to support JAVA development. And now we can use the C/C++ Plugin support C/C++, which is the native language in Windows/Linus and thus is what we will use to access native resources. Here is the Tutorial Link on NetBeans.org.

Note: My OS is Windows, therefore, I did not try it myself. Whether this tutorial is working now is still needed to be double-checked.

2. JNI with NetBeans IDE, C/C++ Plugin and Cygwin on Windows XP

Cygwin is to provide us the GCC complier sets to create .dll files, which is included in Linux, in Windows environment.  Here is the Link of Tutorial in Chinese

Note: I have tested this both in Platform (1) and Platform (2), however due to the different versions of software, some errors happened, which said the complier options: gcc: The -mno-cygwin flag has been removed .....,I assumed this the conflict between different versions of CYGWIN GCC tools. Therefore, I have to search other solutions.

3. JNI with NetBeans IDE/Eclipse and Microsoft Visual Studio on Windows

Visual Studio is used to replace the role of GCC complier sets to create .dll file.

Here is the link of a good Tutorial:  https://www.javaeye.com/topic/195428

Note: In platform(1), which is 64-bits. Because the JDK is also 64.bits. Therefore, the 32-bit .dll file created by Visual Studio could not be executed correctly. Because I do not know how to produce 64bit .dll file in VS and its compatibility.Therefore, I changed to Platform(2). The compatibility problem was solved. However, some errors still happened, which said could not find dependent library. Well, how to find what are the dependent libraries? I searched around and found a tool called Dependency Walker, which could analyze our .exe or .dll files to find the dependent  library.   And the result is that KERNEL32.DLL and MSVCR100D.DLL are what we needed. I searched in Windows\System32 and found kernel32.dll but no MSVCR100.DLL. I downloaded it from https://www.dll-files.com/dllindex/dll-files.shtml?msvcr100d . After all of these works, my program finally operated well.

4. JNA

JNA is a newest method provides by JAVA Community to easy access to native library without writing anything but JAVA, which is kind of easier that JNI.

Here is the official website of JNA: https://jna.dev.java.net/

Here is a tutorial written by Jeff Friesen, https://www.javaworld.com/javaworld/jw-02-2008/jw-02-opensourcejava-jna.html?page=1  There are also Chinese Version which could be founded.

Note: I have not tried JNA up till now.

Summary:

With so many new different tools and platforms appearing, many incompatibility problems will happen, which we should be careful when choosing the version of softwares.

Also, we should also pay more attention in setting the environmental variables-PATH and such issues about the path of native library, which will be detailed in above links.

I hope this article will help people who want to use JNI to access native library. It has taken me lots of time to get all of problems done. Hopefully, this will save your time.

Also, thanks people who have written the above blogs and articles about JNI. You guys really give me a big hand.

 类似资料: