Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How to InvokeC++ from Java

C

clusardi2k

Could anyone give me a tutorial on how to use NetBeans 7.1.2 to create a java project that uses JNI. I'm looking for something with all the detail I can get.

My small java project will use a small subset of the class' in a large C++ package.

Is there good documentation on doing this. Is there more than one way to do it. Does using a jar file (containing the C++ class') ease the process at all.

Help,
Chris L.
 
C

clusardi2k

Could anyone give me a tutorial on how to use NetBeans 7.1.2 to create a java project that uses JNI. I'm looking for something with all the detail I can get.

My small java project will use a small subset of the class' in a large C++ package.

Is there good documentation on doing this. Is there more than one way to do it.

I'll accept any SIMPLE (complete) JNI example that uses NetBeans 7.1.2, a dot java with a static main, and invokes a C++ project.

The various examples on the Internet tell me to create 2 projects, but involve older versions of NetBeans. I'm still trying, but I can't get a simple "Hello World" to work!

Where is a detailed example that uses NetBeans 7.1.2?

Thanks,
 
J

John B. Matthews

I'll accept any SIMPLE (complete) JNI example that uses NetBeans
7.1.2, a dot java with a static main, and invokes a C++ project.

I don't know anything that specific; I've always relied on the JNI
Tutorial [1] and the JNI Specification [2] for specific problems. You
may not need it for your project, but this JNI_CreateJavaVM example [3]
may come in handy.
The various examples on the Internet tell me to create 2 projects,
but involve older versions of NetBeans. I'm still trying, but I can't
get a simple "Hello World" to work!

Like this one [4]?
Where is a detailed example that uses NetBeans 7.1.2?

I've never tried to do it entirely in NetBeans; I've used `make` to
create the jnilib itself and a suitable ant target to build the Java
dependencies.

[1] <http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html>
[2] <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html>
[3] <http://www.jguru.com/forums/view.jsp?EID=1264492>
[4] <http://patriot.net/~tvalesky/jninative.html>
 
C

clusardi2k

Two Questions:

(1) Here's an example that I've been trying to follow:

http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html

For me, there are two problems with this example. One, it is for "Linux". I'm working on Windows 7 and have to use C compiler options to get this example to work. At the bottom is a direct quote from the linked Page. What should I use in place of "-shared -m32" for a C compiler on Windows 7.

(2) Second, what would be the g++ compiler options if I use a C++ project in place of the C project example.

Thanks,

Direct Quote from Page:

Beginning JNI with NetBeans IDE and C/C++ Plugin on Linux

Setting Up a New C/C++ Dynamic Library Project

7. 7.Find the Command Line area of the C Compiler options. Click in the text field of the Additional Options property and type -shared -m32.

The -shared option tells the compiler to generate a dynamic library.
The -m32 option tells the compiler to create a 32-bit binary. By default on64-bit systems the compiled binaries are 64-bit, which causes a lot of problems with 32-bit JDKs.
 
C

clusardi2k

Could anyone give me a tutorial on how to use NetBeans 7.1.2 to create a java project that uses JNI. I'm looking for something with all the detail I can get.

My small java project will use a small subset of the class' in a large C++ package.

Is there good documentation on doing this. Is there more than one way to do it. Does using a jar file (containing the C++ class') ease the process at all.

Help,
Chris L.

Here are the compilers (and make command) that I'm going to use:

Software or Resource Version Tested Description

gcc 3.4.5 MinGW C compiler
g++ 3.4.5 MinGW C++ compiler
gdb 6.8 MinGW GNU debugger
make 3.79.1 MSYS make utility

Note that MinGW make is not supported

http://netbeans.org/community/releases/69/cpp-setup-instructions.html#mingw
 
C

clusardi2k

Using the "-shared -m32" options, NetBeans does create the dot dll.

I put the folder containing the dot dll into the PATH environment variable.

But, when I press F6 to run, I get the following error message:

run:
java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: libJNIDemoCdl
at java.lang.Runtime.load0(Runtime.java:789)
at java.lang.System.load(System.java:1059)
at jnidemojava.Main.<clinit>(Main.java:11)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Below is Main.java file. I tried various paths to the dll:
-----
package jnidemojava;

public class Main {
private native void nativePrint();

public static void main(String[] args) {
new Main().nativePrint();
}

static {
System.load("libJNIDemoCdl");
System.load("libJNIDemoCdl");
System.load("libJNIDemoCdl");
System.load("libJNIDemoCdl.dll");
System.load("libJNIDemoCdl.dll");
System.load("libJNIDemoCdl.dll");
System.load("C:/Users/THE_USER/Documents/NetBeansProjects/JNIDemoCdl/dist/libJNIDemoCdl");
System.load("C://Users//THE_USER//Documents//NetBeansProjects//JNIDemoCdl//dist//libJNIDemoCdl");
System.load("C:\\Users\\THE_USER\\Documents\\NetBeansProjects\\JNIDemoCdl\\dist\\libJNIDemoCdl");
System.load("C:/Users/THE_USER/Documents/NetBeansProjects/JNIDemoCdl/dist/libJNIDemoCdl.dll");
System.load("C://Users//THE_USER//Documents//NetBeansProjects//JNIDemoCdl//dist//libJNIDemoCdl.dll");
System.load("C:\\Users\\THE_USER\\Documents\\NetBeansProjects\\JNIDemoCdl\\dist\\libJNIDemoCdl.dll");
}

}
 
C

clusardi2k

With the below code, I get a different error. It complains at the "new" (Line 7).

package jnidemojava;

public class Main {
private native void nativePrint();

public static void main(String[] args) {
new Main().nativePrint(); //LINE 7

System.out.println (System.getProperty("java.library.path"));
}

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

run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidemojava.Main.nativePrint()V
at jnidemojava.Main.nativePrint(Native Method)
at jnidemojava.Main.main(Main.java:7)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
 
C

clusardi2k

new Main().nativePrint(); //LINE 7

If I comment out this line it runs perfectly. Again, the example I'm working on is this: http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html

At that linked page, JNIDemoJava.h has the prototype definition:

JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint
(JNIEnv *, jobject);


And JNIDemoJava.c has the body of that prototype:

JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint
(JNIEnv *env, jobject obj)
{

printf("\nHello World from C\n");

}
 
C

clusardi2k

If I comment out this line it runs perfectly. Again, the example I'm working on is this: http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html

What do I have to do be able to run with this line uncommented. Am I missing a compiler/linking option.

If I uncomment the line I get the run-time error message:

run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidemojava.Main.nativePrint()V
at jnidemojava.Main.nativePrint(Native Method)
at jnidemojava.Main.main(Main.java:7)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
 
C

clusardi2k

I have it working. That's if I use the 32bit versions of JDK and JRE, and compile the C project using toptions:

-Wl,--export-all-symbols -Wl,--add-stdcall-alias
 
L

Lew

Could anyone give me a tutorial on how to use NetBeans 7.1.2 to create a java project that uses JNI. I'm looking for something with all the detail I can get.

My small java project will use a small subset of the class' in a large C++ package.

Is there good documentation on doing this. Is there more than one way to do it. Does using a jar file (containing the C++ class') ease the process at all.

Why does my Web viewer show all the messages as deleted?

90% of the value of the newsgroups is in the archival of questions, explorations and
answers.

Failure to archive is a problem.
 
J

John B. Matthews

Lew said:
Why does my Web viewer show all the messages as deleted?

90% of the value of the newsgroups is in the archival of questions,
explorations and answers.

Failure to archive is a problem.

The OP's behavior is disappointing. My fading memory of a cursory
reading mentioned trouble building a shared library using mingw, covered
here: <http://mingw.org/wiki/sampleDLL>.

For reference, Mac OS X/Darwin goes something like this:

gcc -dynamiclib -framework JavaVM -o libhello.jnilib hello.o
 
L

Lew

John said:
The OP's behavior is disappointing. My fading memory of a cursory
reading mentioned trouble building a shared library using mingw, covered
here:<http://mingw.org/wiki/sampleDLL>.

For reference, Mac OS X/Darwin goes something like this:

gcc -dynamiclib -framework JavaVM -o libhello.jnilib hello.o

albanasi.net still has all the messages. Usenet prevails where the
Web-accessed service allowed the deletion of the messages.

To the OP: deleting your messages was not only disappointing and unhelpful,
but pointless as they are not actually deleted.
 
J

John B. Matthews

[QUOTE="Lew said:
The OP's behavior is disappointing. My fading memory of a cursory
reading mentioned trouble building a shared library using mingw,
covered here:<http://mingw.org/wiki/sampleDLL>.

For reference, Mac OS X/Darwin goes something like this:

gcc -dynamiclib -framework JavaVM -o libhello.jnilib hello.o

albanasi.net still has all the messages. Usenet prevails where the
Web-accessed service allowed the deletion of the messages.[/QUOTE]

As does aioe.net; thank you for the reminder.
To the OP: deleting your messages was not only disappointing and
unhelpful, but pointless as they are not actually deleted.

Recalling a time when summaries were more common:

General:
<http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html>
<http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html>

Examples:
<http://www.jguru.com/forums/view.jsp?EID=1264492>
<http://patriot.net/~tvalesky/jninative.html>

NetBeans:
<http://cnd.netbeans.org/docs/jni/beginning-jni-win.html>
<http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html>

MinGW:
<http://mingw.org/wiki/sampleDLL>
<http://netbeans.org/community/releases/69/cpp-setup-instructions.html#mingw>

Mac OS X:
<http://stackoverflow.com/a/1731221/230513>
 
A

Arun GOPI

&gt; On Friday, June 15, 2012 2:57:42 PM UTC-4, (unknown) wrote:
&gt; &gt; Could anyone give me a tutorial on how to use NetBeans 7.1.2 to
&gt; &gt; create a java project that uses JNI. I'm looking for something with
&gt; &gt; all the detail I can get.
&gt; &gt;
&gt; &gt; My small java project will use a small subset of the class' in a
&gt; &gt; large C++ package.
&gt; &gt;
&gt; &gt; Is there good documentation on doing this. Is there more than one
&gt; &gt; way to do it.
&gt;
&gt; I'll accept any SIMPLE (complete) JNI example that uses NetBeans
&gt; 7.1.2, a dot java with a static main, and invokes a C++ project.

I don't know anything that specific; I've always relied on the JNI
Tutorial [1] and the JNI Specification [2] for specific problems. You
may not need it for your project, but this JNI_CreateJavaVM example [3]
may come in handy.

&gt; The various examples on the Internet tell me to create 2 projects,
&gt; but involve older versions of NetBeans. I'm still trying, but I can't
&gt; get a simple &quot;Hello World&quot; to work!

Like this one [4]?

&gt; Where is a detailed example that uses NetBeans 7.1.2?

I've never tried to do it entirely in NetBeans; I've used `make` to
create the jnilib itself and a suitable ant target to build the Java
dependencies.

[1] &lt;http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html&gt;
[2] &lt;http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html&gt;
[3] &lt;http://www.jguru.com/forums/view.jsp?EID=1264492&gt;
[4] &lt;http://patriot.net/~tvalesky/jninative.html&gt;

refer :- http://www.ibm.com/developerworks/java/tutorials/j-jni/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top