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

Discussion in 'Java' started by clusardi2k@aol.com, Jun 15, 2012.

  1. Guest

    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.
     
    , Jun 15, 2012
    #1
    1. Advertising

  2. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    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,
     
    , Jun 19, 2012
    #2
    1. Advertising

  3. Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How to Invoke C++ from Java

    In article <>,
    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>
     
    John B. Matthews, Jun 19, 2012
    #3
  4. Guest

    Re: Using NetBeans 7.1.2 JNI: Windows 7 C and C++ Compiler Options

    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.
     
    , Jun 19, 2012
    #4
  5. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    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/releases/69/cpp-setup-instructions.html#mingw
     
    , Jun 19, 2012
    #5
  6. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    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");
    }

    }
     
    , Jun 19, 2012
    #6
  7. Guest

    , Jun 19, 2012
    #7
  8. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    Without success, I also tried using loadLibrary instead of "load".
     
    , Jun 19, 2012
    #8
  9. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    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)
     
    , Jun 19, 2012
    #9
  10. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    > 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");

    }
     
    , Jun 19, 2012
    #10
  11. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    On Tuesday, June 19, 2012 11:35:53 AM UTC-4, (unknown) wrote:
    > > new Main().nativePrint(); //LINE 7, mentioned in error message below

    >
    > 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)
     
    , Jun 19, 2012
    #11
  12. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    If I look at the Windows example at:

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

    None of the following options: "-mno-cygwin -Wl,--add-stdcall-alias -shared -m32" allow me to run with an uncommented line 7.

    Am I wrong, or is NetBeans complaining about the actual versus formal argument discrepancy?
     
    , Jun 19, 2012
    #12
  13. Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    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
     
    , Jun 20, 2012
    #13
  14. Lew Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    On Friday, June 15, 2012 11:57:42 AM UTC-7, (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.


    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.

    --
    Lew
     
    Lew, Jun 20, 2012
    #14
  15. Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How to Invoke C++ from Java

    In article <>,
    Lew <> wrote:

    > On Friday, June 15, 2012 11:57:42 AM UTC-7, (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.

    >
    > 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

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 22, 2012
    #15
  16. Lew Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    John B. Matthews wrote:
    > Lew wrote:
    >> (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.

    >>
    >> 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


    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.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, Jun 22, 2012
    #16
  17. Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How to Invoke C++ from Java

    In article <js0su7$a8u$>, Lew <>
    wrote:

    > John B. Matthews wrote:
    > > Lew wrote:
    > >> (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.
    > >>
    > >> 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

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


    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>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 23, 2012
    #17
  18. Arun GOPI Guest

    Re: Using NetBeans 7.1.2 JNI: Where Is a Good Tutorial on How toInvoke C++ from Java

    On Tuesday, 19 June 2012 16:04:45 UTC+5:30, John B. Matthews wrote:
    > In article &lt;&gt;,
    > wrote:
    >
    > &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;
    >
    > --
    > John B. Matthews
    > trashgod at gmail dot com
    > &lt;http://sites.google.com/site/drjohnbmatthews&gt;


    refer :- http://www.ibm.com/developerworks/java/tutorials/j-jni/
     
    Arun GOPI, Jul 16, 2012
    #18
  19. Arun GOPI Guest

    Arun GOPI, Jul 16, 2012
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bushido Hacks
    Replies:
    2
    Views:
    28,100
    Ross Bamford
    May 31, 2005
  2. Replies:
    0
    Views:
    430
  3. Replies:
    0
    Views:
    436
  4. Replies:
    2
    Views:
    392
  5. Replies:
    0
    Views:
    273
Loading...

Share This Page