Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How to InvokeC++ from Java

Reply
Thread Tools

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

 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-15-2012
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.
 
Reply With Quote
 
 
 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012
On Friday, June 15, 2012 2:57:42 PM UTC-4, (unknown) wrote:
> 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,
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      06-19-2012
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Friday, June 15, 2012 2:57:42 PM UTC-4, (unknown) wrote:
> > 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.


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>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012
Two Questions:

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

http://netbeans.org/kb/docs/cnd/begi...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.

 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012
On Friday, June 15, 2012 2:57:42 PM UTC-4, (unknown) wrote:
> 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/releas...ons.html#mingw
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012

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\\NetBe ansProjects\\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\\NetBe ansProjects\\JNIDemoCdl\\dist\\libJNIDemoCdl.dll") ;
}

}


 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012
The below link may indicate that I can use the example's "-m32" option on Windows 7.

http://stackoverflow.com/questions/2...of-gcc-be-used

Since I am creating the DLL, it may also be ok for me to use the example's "-shared" option.
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012
Without success, I also tried using loadLibrary instead of "load".
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012
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)
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      06-19-2012
> 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/begi...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");

}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Eclipse vs. Netbeans. Netbeans is winning me over almost at first glance. Thomas G. Marshall Java 2 10-16-2006 02:54 AM
Java Programming Using Netbeans Tutorial Bushido Hacks Java 2 05-31-2005 10:15 AM
[NETBEANS] [EVALUATION] - E12 - The NetBeans Open Source Lie Ilias Lazaridis Java 0 12-12-2004 02:41 PM
Building a GUI using NetBeans platform (not Netbeans IDE !) Pimousse Java 2 04-21-2004 07:22 AM



Advertisments