Java Call DLL or so dynamic library file (C++/C)

Source: Internet
Author: User

Java Call DLL or so dynamic library file (C++/C)
      Blog Category:
    • Job
Cc#c++javaeclipse Java Call DLL or so dynamic library file (C++/C) development platform: ECLIPSE3.3.1.1+CDT (cdt-master-4.0.3) +mingw (MinGW-5.1.4)

One: The following is the Java call DLL (c + +)
1: Download and install cdt:http://www.eclipse.org/cdt/downloads.php: Select your own eclipse-supported CDT plugin, download it, and
Installing the CDT via Eclipse-->software update-->find and install

2: Download and install mingw:http://sourceforge.net/project/showfiles.php?group_id=2435
Then, click Mingw.exe, select Download and install, and then both are selected (speed may be a bit slow, be patient),

3: Environment variable configuration (add the following variable to the system variable or user variable, note that the path is modified according to the actual installation):
Path:d:\program Files\mingw\bin
C_include_path:d:\program Files\mingw\include
Cplus_include_path:d:\program Files\mingw\include\c++\3.4.5;d:\program files\mingw\include\c++\3.4.5\mingw32;d:\ Program Files\mingw\include\c++\3.4.5\backward;d:\program Files\mingw\include
Library_path:d:\program Files\mingw\lib

Library_path This variable is best added, previously did not add, but also can compile the correct DLL, but later compiled DLL has a problem, and finally fixed the Library_path this variable, resulting in compiled DLL can not run properly.
If you add all the variables, it's best to restart your computer.

4: Related Settings
Eclipse-->window->preferences->c/c++->new CDT Project Wizard->makefile Project
Find Binary Parser to remove elf Parser, select PE Windows Parser.

Because the make file under the MinGW directory is named "Mingw32-make.exe", the Eclipse default call file is named "Make.exe"
So first make a backup of the file named "Mingw32-make.exe" in the MinGW directory and rename the file to "Make.exe"

5:java Engineering and C + + engineering, or classical HelloWorld as an example
Java code
  1. Public class Hello {
  2. Public native void SayHello ();
  3. Static {
  4. System.loadlibrary ("Hello");
  5. }
  6. Public Static void Main (string[] args) {
  7. Hello h = new hello ();
  8. H.sayhello ();
  9. }

Then compile the generated class file and generate the header file with the command Javah class file Hello.h

To create a C + + project:
Attention:

1&gt: Set C + + Build mode in Eclipse: Change the release state to active, or the DLL generated by the Java program call will report the following error
Exception in thread "main" Java.lang.UnsatisfiedLinkError:


2&gt: When compiling, the solution is to add an int mian () method to the. h file and the. cpp file if the following error is reported.

Rebuild of configuration Debug for Project helloc++ * * * * *

Internal Builder is used for build * * * * *
g++-o0-g3-wall-c-fmessage-length=0-ohello.o. \hello.cpp
.. \hello.cpp:10:2: Warning:no newline at end of file
g++-ld:\program Files\java\jdk1.6.0_10\include\win32-ld:\program Files\java\jdk1.6.0_10\include-ohelloc++.exe hello.o
D:/program files/mingw/bin/. /lib/gcc/mingw32/3.4.5/. /.. /.. /libmingw32.a (MAIN.O): main.c: (. text+0xbd): Undefined reference to ' [email protected] '
Collect2:ld returned 1 exit status
Build error occurred, build is stopped
Time consumed:5192 Ms.
------------------------------------------------------------------------------
Modified Hello.h Content:
C + + code
  1. /* Don't EDIT this file-it are machine generated * /
  2. #include <jni.h>
  3. / * Header for class Hello * /
  4. #ifndef _included_hello
  5. #define _included_hello
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10. * Class:hello
  11. * Method:sayhello
  12. * Signature: () V
  13.  */
  14. int main ();
  15. Jniexport void Jnicall Java_hello_sayhello
  16. (JNIENV *, jobject);
  17. #ifdef __cplusplus
  18. }
  19. #endif
  20. #endif

Modified Hello.cpp Content:
C + + code
  1. #include <iostream>
  2. #include "Hello.h"
  3. using namespace std;
  4. int Main ()
  5. {
  6. return 1;
  7. }
  8. Jniexport void jnicall Java_hello_sayhello (jnienv *, Jobject) {
  9. printf ("Hello world!\n");
  10. return ;
  11. }

A: Then use the eclipse C + + plugin's make targets--build to generate the HELLO.O
Target name whatever
Make Target casually
The command is available by default
B: g++ the Wingw/bin under the HELLO.O sibling directory that you just generated, and then switch to the directory where hello.o is located in the command line
Execute the command, note the parameters (my Java directory D:\Program files\java\):
g++-i "D:\Program files\java\jdk1.6.0_10\include"-wl,--add-stdcall-alias-shared-o Hello.dll hello.o

6: Run:
Copy the DLL file you just generated to the root directory of your Java project and run Hello.class


Note: If the test class is under a package, for example: Com.test.Hello, when we compile the class file: Javac Com\test\hello.java
Then: Javah Com.test.Hello, so the method in the generated header file can be found when it is called.



Second: Java calls so (C)
Both the Java and C + + communication can be implemented through JNI. In the Java code:
System.loadlibrary ("Hello");
Hello cannot be written as Hello.dll or hello.so, it is automatically populated according to the system platform, and it is important to note that when generating the. So dynamic library file under Unix/linux,
You need to add Lib before hello.so, otherwise you cannot find the. so file (libhello.so), you need to specify the path of. So when running:
Java-djava.library.path=/homw/user/so Directory-jar Hello.jar

Three: Order collation:
Take C For example (if C + +, change gcc to g++ OK):
1: Under the Unix/linux environment
1.1: Generate. o File
Gcc-i/usr/lib/j2sdk1.5-ibm/include-fpic-c example.c
1.2: Generate the Dynamic library. So file
Gcc-shared-wi-soname Example.o-o libexample.so
2: In the Windows environment
2.1: Generate. o File
Gcc-c-i "D:\Program files\java\jdk1.6.0_10\include"-i "D:\Program files\java\jdk1.6.0_10\include\win32"-O hello.o hello.c
2.2: Generate DLL file
Gcc-i "D:\Program files\java\jdk1.6.0_10\include"-wl,--add-stdcall-alias-shared-o Hello.dll hello.o

Due to the time relationship, write more chaotic, O (∩_∩) o ...

Java Call DLL or so dynamic library file (C++/C)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.