Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Under Linux, Call java from C++

Reply
Thread Tools

Under Linux, Call java from C++

 
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-01-2005
Yes they are. U r right. I should look a little bit more about the
manual. Sorry.
Tony

 
Reply With Quote
 
 
 
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-01-2005
>It seems you have several java versions installed. You need to make
>sure you compile and link against the correct (and same) one.


I think that you are right. But how to cehck if I compile and link
against the correct java version?
I tried in different computers, and it was the same: can not creat
JavaVM!
TOny

 
Reply With Quote
 
 
 
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-01-2005
I really thank Gordon Beaton and others a lot! Actually he showed me
already how to do it in his answers:
Just add one line in jni.h, to define JNI_VERSION as 1_4_2_08. Then I
can call java codes from C++ under Linux!
Thanks again for all the hints, especially the ones from Gordon Beaton!

 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-01-2005
But still two quesions:

For the compiling and linking:
gcc -Wall -D_REENTRANT -I $JAVA_HOME/include -I
$JAVA_HOME/include/linux -c invoke.cpp
gcc -L $JAVA_HOME/jre/lib/i386/client invoke.o -lstdc++ -ljvm -lpthread
-o invoke

The parameters for gcc are really important. Without anyone of them,
the JNI calling can not work. But How do I know which parameter I
should choose?

Question two:
For the 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 was told by Gordon that I should set the ID_LIBRARY_PATH like this:
/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
But why?

Thanks a lot

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-02-2005
On 1 Oct 2005 09:57:01 -0700, wrote:
> I really thank Gordon Beaton and others a lot! Actually he showed me
> already how to do it in his answers:
> Just add one line in jni.h, to define JNI_VERSION as 1_4_2_08. Then I
> can call java codes from C++ under Linux!


Uhh, I don't recall suggesting you add your own JNI_VERSION to the
header file. My suggestion was to use one of the existing ones.

/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
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-02-2005
On 1 Oct 2005 10:30:29 -0700, wrote:
> But still two quesions:
>
> For the compiling and linking:
> gcc -Wall -D_REENTRANT -I $JAVA_HOME/include -I
> $JAVA_HOME/include/linux -c invoke.cpp
> gcc -L $JAVA_HOME/jre/lib/i386/client invoke.o -lstdc++ -ljvm -lpthread
> -o invoke
>
> The parameters for gcc are really important. Without anyone of them,
> the JNI calling can not work. But How do I know which parameter I
> should choose?
>
> Question two:
> For the 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 was told by Gordon that I should set the ID_LIBRARY_PATH like this:
> /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
> But why?


There are two versions of libjvm.so, a server and a client version,
and you need to link against one of them. I can't say exactly what the
difference is (some optimization details I think), but you can check
the documentation for the java command line options -server and
-client.

At runtime there are additional dependencies on library files found in
$JAVA_HOME/jre/lib/i386 (common to both server and client). So at your
library path needs at least two directories, the server (or client),
and the common 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
 
Reply With Quote
 
James McIninch
Guest
Posts: n/a
 
      10-03-2005
<posted & mailed>

It's not likely that the Linux code will need the Windows JVM library. Why
not use the Linux one. The errors also indicate that you didn't include
jni.h, including the headers might be helpful too.

wrote:

> 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.l ib")"
>
> 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.l ib")
>
> 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/StringV");
> 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.


--
Remove '.nospam' from e-mail address to reply by e-mail
 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-03-2005
No, u did not suggest that I should add my own JNI_VERSION to the
header file. It was my idea. But this idea was initiated by your and
other's words. Thanks a lot.
Tony

 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      10-04-2005
On Mon, 03 Oct 2005 09:43:04 -0700, tony_lincoln wrote:

> No, u did not suggest that I should add my own JNI_VERSION to the
> header file. It was my idea. But this idea was initiated by your and
> other's words. Thanks a lot.
> Tony


Modifying library headers is almost never the right way to fix problems
using the library. You're likely to introduce subtle bugs into your code
that way (eg., by using a constant value outside the expected range of
values).

 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-04-2005
But in my library headers there is only:
#define JNI_VERSION_1_1 0x00010001
#define JNI_VERSION_1_2 0x00010002
There is no definition for JNI_VERSION_1_4 at all. As my current java
version is 1.4, I guess that I should add this new definition into the
header.
Another alternative is, to download a new jni.h which has the
definition for JNI_VERSION_1_4, I guess.
Am I right?

 
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
Tomcat 5.5+ On a Mac, Under Eclipse, Under OS X Edward V. Berard Java 4 04-04-2006 05:14 AM
Why does Rpy/R-plot work under PythonWin, but not under commandline/IDLE? Bo Peng Python 1 01-09-2006 07:05 AM
help : my jar file is not running under linux terminal , but it runs under JbuilderX ide bronby Java 1 07-15-2005 07:23 AM
[newbie]How to install python under DOS and is there any Wxpython can be installed under dos? john san Python 19 02-18-2005 12:05 PM
Java application developped under Linux running ridiculously slow under Windows hshdude Java 12 11-04-2004 05:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57