Under Linux, Call java from C++

Discussion in 'Java' started by tony_lincoln@yahoo.com, Sep 29, 2005.

  1. Guest

    Dear Friends,

    Under Linux, I tried to call java from C++ using JNI. The following is
    the C++ code which calls one java class named Menu_3D.
    When I compiled it using g++, there were many mistakes like:

    ........
    invoke.cpp:26: error: 'struct JavaVMInitArgs' has no member named
    'version'
    invoke.cpp:26: error: `JNI_VERSION_1_4' undeclared (first use this
    function)
    invoke.cpp:27: error: 'struct JavaVMInitArgs' has no member named
    'nOptions'
    invoke.cpp:31: error: `jint' undeclared (first use this function)
    invoke.cpp:31: error: syntax error before `=' token
    invoke.cpp:32: error: `res' undeclared (first use this function)
    invoke.cpp:41: error: `FindClass' undeclared (first use this function)
    invoke.cpp:50: error: `GetStaticMethodID' undeclared (first use this
    function)
    invoke.cpp:56: error: `NewObjectArray' undeclared (first use this
    function)
    invoke.cpp:60: error: `CallStaticVoidMethod' undeclared (first use this
    function)
    invoke.cpp:73: error: `DestroyJavaVM' undeclared (first use this
    function)
    ........

    The above errors are only one part of the whole errors. I know that I
    am wrong in this line:
    "#pragma comment
    (lib,"E:\\Programme\\Java\\jdk1.5.0_02\\lib\\jvm.lib")"

    But how can I find the jvm.lib? I tried and I can not. Does invoke.cpp
    need jvm.lib under Linux?

    Or are there other reasons to cause the errors?
    Thanks a lot
    tony


    __________________________________________________________________________
    /*for C++,debugged with Visual C++ 6.0*/

    #ifndef __cplusplus
    #define __cplusplus
    #endif

    #include "jni.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>

    #pragma comment (lib,"E:\\Programme\\Java\\jdk1.5.0_02\\lib\\jvm.lib")

    void main() {

    JavaVM *jvm;
    JNIEnv *env;

    JavaVMInitArgs vm_args;
    JavaVMOption options[3];

    options[0].optionString = "-Djava.compiler=NONE";
    options[1].optionString = "-Djava.classpath=.";
    options[2].optionString = "-verbose:jni";

    vm_args.version = JNI_VERSION_1_4;
    vm_args.nOptions = 3;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = JNI_TRUE;

    jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    if (res < 0) {
    fprintf(stderr, "Can't create Java VM\n");
    exit(1);
    };


    // jclass cls = env->FindClass("DemoMain");
    // This sentence can work. Just put it into the same directory as
    invoke.cpp.

    jclass cls = env->FindClass("Menu_3D");
    if (cls == 0) printf("Sorry, I can't find the class");

    fprintf(stdout, "This is invokeSimplified4.\n");

    jmethodID get_main_id;

    if(cls != NULL)
    {
    get_main_id =
    env->GetStaticMethodID(cls,"main","([Ljava/lang/String;)V");
    fprintf(stdout, "This is invokeSimplified5.\n");

    if(get_main_id != NULL )
    {
    jclass string = env->FindClass("java/lang/String");
    jobjectArray args = env->NewObjectArray(0,string, NULL);

    fprintf(stdout, "This is invokeSimplified6.\n");

    env->CallStaticVoidMethod(cls, get_main_id, args);
    /*
    if (env->ExceptionOccurred())
    {
    env->ExceptionDescribe();
    env->ExceptionClear();
    }
    */ fprintf(stdout, "This is invokeSimplified7.\n");
    }// end IF.

    }// end IF.


    jvm->DestroyJavaVM();
    fprintf(stdout, "Java VM destory\n");
    }//end main.
    ______________________________________________________codes end.
     
    , Sep 29, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 29 Sep 2005 15:19:01 -0700, wrote or quoted
    :

    >But how can I find the jvm.lib?

    check out updatedb and locate.

    It is one of the slickest features of linux. It is so fast.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 30, 2005
    #2
    1. Advertising

  3. On 29 Sep 2005 15:19:01 -0700, wrote:
    > Under Linux, I tried to call java from C++ using JNI. The following
    > is the C++ code which calls one java class named Menu_3D. When I
    > compiled it using g++, there were many mistakes


    I don't get the errors you report, but there are some things that need
    to be fixed:

    - main() must return int, not void.

    - #include <windows.h> isn't necessary at all, remove it
    (anyway I suspect you don't have such a file on linux).

    - normally you should include the system header files first, followed
    by jni.h and finally your own header files.

    - the pragma is unknown to g++

    After making the above changes, the code compiles fine using the
    following commands:

    (compile: invoke.cpp -> invoke.o)
    gcc -Wall -D_REENTRANT -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -c invoke.cpp

    (link: invoke.o -> invoke)
    gcc -L $JAVA_HOME/jre/lib/i386/client invoke.o -lstdc++ -ljvm -lpthread -o invoke

    (note that each of these two commands is exactly one line, even though
    my newsreader or yours may have broken them)

    To run the program you need to have the following directories in your
    LD_LIBRARY_PATH:

    $JAVA_HOME/jre/lib/i386/client
    $JAVA_HOME/jre/lib/i386

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 30, 2005
    #3
  4. Roedy Green Guest

    On 30 Sep 2005 09:34:40 +0200, Gordon Beaton <> wrote or
    quoted :

    >LD_LIBRARY_PATH


    what is LD_LIBRARY_PATH? a system property, a SET environment
    parameter? just a name for the path? I was looking for signs of it
    today earlier on my Windows machine. Nothing. I vaguely recall seeing
    it earlier..
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 30, 2005
    #4
  5. On Fri, 30 Sep 2005 08:22:37 GMT, Roedy Green wrote:
    > what is LD_LIBRARY_PATH? a system property, a SET environment
    > parameter? just a name for the path? I was looking for signs of it
    > today earlier on my Windows machine. Nothing. I vaguely recall seeing
    > it earlier..


    One of several enviroment variables that can be used to control the
    dynamic linker/loader. They are described on the relevant man pages
    (e.g. man ld.so on linux).

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 30, 2005
    #5
  6. Guest

    Hi Friends and especially Gordon,

    Thanks a lot for the words. I did the following:
    1.changed the path setting :
    export
    LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/i386/client:$JAVA_HOME/jre/lib/i386.
    2. changed "void main()" into "int main()"

    THen the erros are like this:
    ____________________________________________________________________
    rakta:~/jniLinux$ gcc -Wall -D_REENTRANT -I $JAVA_HOME/include -I
    $JAVA_HOME/include/linux -c invokeLinux.cpp
    invokeLinux.cpp: In function `int main()':
    invokeLinux.cpp:24: error: `JNI_VERSION_1_4' undeclared (first use this
    function)
    invokeLinux.cpp:24: error: (Each undeclared identifier is reported only
    once
    for each function it appears in.)
    __________________________________________________________________________________

    line 24 in invokeLinux.cpp is:
    vm_args.version = JNI_VERSION_1_4;

    Is there anything wrong with the version of JVM?
    Thanks in advance.
    tony
     
    , Sep 30, 2005
    #6
  7. Guest

    What do you mean "updatedb and locate"?
     
    , Sep 30, 2005
    #7
  8. Jeff Schwab Guest

    Roedy Green wrote:
    > On 30 Sep 2005 09:34:40 +0200, Gordon Beaton <> wrote or
    > quoted :
    >
    >
    >>LD_LIBRARY_PATH

    >
    >
    > what is LD_LIBRARY_PATH? a system property, a SET environment
    > parameter? just a name for the path? I was looking for signs of it
    > today earlier on my Windows machine. Nothing. I vaguely recall seeing
    > it earlier..


    LD_LIBRARY_PATH is an environment variable used to tell some linkers
    where to look for libraries. I have found it useful with the GNU tools
    on various flavors of Unix.
     
    Jeff Schwab, Sep 30, 2005
    #8
  9. Guest

    When I remove this line,
    vm_args.version = JNI_VERSION_1_4;

    I can get invoke.o and invoke. But when I typed invoke, there is one
    error:
    error while loading shared libraries: libjvm.so: cannot open shared
    object file: No such file or directory

    I guess that is the problem about LD_LIBRARY_PATH. But I set it already
    in bashrc file...
    Why?
     
    , Sep 30, 2005
    #9
  10. On 30 Sep 2005 08:28:33 -0700, wrote:
    > When I remove this line,
    > vm_args.version = JNI_VERSION_1_4;


    It sounds like you are using an earlier version of java - try using
    JNI_VERSION_1_2, or check the value of JAVA_HOME.

    What does "java -version" say?

    > I can get invoke.o and invoke. But when I typed invoke, there is one
    > error:
    > error while loading shared libraries: libjvm.so: cannot open shared
    > object file: No such file or directory
    >
    > I guess that is the problem about LD_LIBRARY_PATH. But I set it already
    > in bashrc file...


    LD_LIBRARY_PATH is what I'd check too. Where does JAVA_HOME point? Can
    you find libjvm.so yourself somewhere in or below JAVA_HOME?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 30, 2005
    #10
  11. Guest

    I set the path like this:

    export
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/jvm/java-1.4.2-sun-1.4.2.05/jre/lib/i386/server/

    This time I can compile it and got invoke.o and invoke. But when I type
    invoke
    then the new information comes:
    Can't create Java VM

    I really wonder why?
     
    , Sep 30, 2005
    #11
  12. Guest

    I found two libjvm.so.
    /usr/lib/jvm/java-1.4.2-sun-1.4.2.05/jre/lib/i386/client/libjvm.so
    /usr/lib/jvm/java-1.4.2-sun-1.4.2.05/jre/lib/i386/server/libjvm.so
    I know that they are different. But what is the difference?
     
    , Sep 30, 2005
    #12
  13. On 30 Sep 2005 08:52:19 -0700, wrote:
    > I set the path like this:
    >
    > export
    > LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/jvm/java-1.4.2-sun-1.4.2.05/jre/lib/i386/server/
    >
    > This time I can compile it and got invoke.o and invoke. But when I type
    > invoke
    > then the new information comes:
    > Can't create Java VM
    >
    > I really wonder why?


    You need to add *two* directories to LD_LIBRARY_PATH:

    /usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386/server
    /usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 30, 2005
    #13
  14. Guest

    You are right. My java version is 1.4.2_05.
    So I changed the codes into
    vm_args.version = JNI_VERSION_1_4_2_05;

    But the error kept the same:

    rakta:~/GAQ$ gcc -Wall -D_REENTRANT -I
    /home/gao/java/j2sdk1.4.2_08/include -I
    /home/gao/java/j2sdk1.4.2_08/include/linux-c invokeLinux.cpp
    invokeLinux.cpp: In function `int main()':
    invokeLinux.cpp:24: error: `JNI_VERSION_1_4_2_05' undeclared (first use
    this
    function)
    invokeLinux.cpp:24: error: (Each undeclared identifier is reported only
    once
    for each function it appears in.)

    :-(
     
    , Sep 30, 2005
    #14
  15. Guest

    Yes. I changed the path into:

    rakta:~/GAQ$ export
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386/server:/usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386

    Then compile it, and the error kept the same:

    invokeLinux.cpp: In function `int main()':
    invokeLinux.cpp:24: error: `JNI_VERSION_1_4_2_05' undeclared (first use
    this
    function)
    invokeLinux.cpp:24: error: (Each undeclared identifier is reported only
    once
    for each function it appears in.)

    If I remove the line "vm_args.version = JNI_VERSION_1_4_2_05",
    I can compile it, but the final result kept the same:
    "Can't create Java VM"

    Sorry for my questions.
     
    , Sep 30, 2005
    #15
  16. On 30 Sep 2005 08:59:19 -0700, wrote:
    > You are right. My java version is 1.4.2_05.
    > So I changed the codes into
    > vm_args.version = JNI_VERSION_1_4_2_05;
    >
    > But the error kept the same:
    >
    > rakta:~/GAQ$ gcc -Wall -D_REENTRANT -I
    > /home/gao/java/j2sdk1.4.2_08/include -I
    > /home/gao/java/j2sdk1.4.2_08/include/linux-c invokeLinux.cpp
    > invokeLinux.cpp: In function `int main()':
    > invokeLinux.cpp:24: error: `JNI_VERSION_1_4_2_05' undeclared (first use
    > this
    > function)
    > invokeLinux.cpp:24: error: (Each undeclared identifier is reported only
    > once
    > for each function it appears in.)


    The only valid values are (AFAIK) JNI_VERSION_1_4, JNI_VERSION_1_2 and
    JNI_VERSION_1_1, they are listed in jni.h.

    The paths you mention in the example above don't agree with what
    you've posted elsewhere, where you mentioned
    /usr/lib/jvm/java-1.4.2-sun-1.4.2_05.

    It seems you have several java versions installed. You need to make
    sure you compile and link against the correct (and same) one.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 30, 2005
    #16
  17. On 30 Sep 2005 09:03:57 -0700, wrote:
    > Yes. I changed the path into:
    >
    > rakta:~/GAQ$ export
    > LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386/server:/usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386
    >
    > Then compile it, and the error kept the same:


    LD_LIBRARY_PATH has nothing to do with compiling the code. It's only
    there to help find the libraries that are needed when you *run* the
    program.

    > invokeLinux.cpp: In function `int main()':
    > invokeLinux.cpp:24: error: `JNI_VERSION_1_4_2_05' undeclared (first use
    > this
    > function)
    > invokeLinux.cpp:24: error: (Each undeclared identifier is reported only
    > once
    > for each function it appears in.)
    >
    > If I remove the line "vm_args.version = JNI_VERSION_1_4_2_05",
    > I can compile it, but the final result kept the same:
    > "Can't create Java VM"


    I've responded to this in another post. This JNI_VERSION isn't valid.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 30, 2005
    #17
  18. Guest

    Thanks. I think that you are correct. I will try to compile and link
    against the correct (and same) version tomorrow.
    Thanks a lot and Have a nice weekend.
    tony
     
    , Sep 30, 2005
    #18
  19. Roedy Green Guest

    On 30 Sep 2005 07:06:42 -0700, wrote or quoted
    :

    >What do you mean "updatedb and locate"?


    Are those not standard Linux commands? I has been a while. Look in a
    bash manual.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 30, 2005
    #19
  20. Roedy Green Guest

    On 30 Sep 2005 08:53:55 -0700, wrote or quoted
    :

    >I know that they are different. But what is the difference?


    there are two versions of java.exe and its attendant dlls. You
    control which one you get with the -server command line option.

    The -server version is slower to start, but presumes it will keep
    running the same code for a long time, so spends more time optimising
    it.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 30, 2005
    #20
    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. AJ
    Replies:
    0
    Views:
    389
  2. hshdude
    Replies:
    12
    Views:
    1,085
    Dimitri Maziuk
    Nov 4, 2004
  3. Angus Parvis
    Replies:
    4
    Views:
    1,114
    janne
    Nov 23, 2004
  4. bronby
    Replies:
    1
    Views:
    640
    Andrew Thompson
    Jul 15, 2005
  5. Luke Kenneth Casson Leighton
    Replies:
    0
    Views:
    374
    Luke Kenneth Casson Leighton
    Jan 15, 2009
Loading...

Share This Page