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
 
      09-29-2005
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.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2005
On 29 Sep 2005 15:19:01 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      09-30-2005

On 29 Sep 2005 15:19:01 -0700, (E-Mail Removed) 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-30-2005
On 30 Sep 2005 09:34:40 +0200, Gordon Beaton <(E-Mail Removed)> 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.
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      09-30-2005
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
 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      09-30-2005
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

 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      09-30-2005
What do you mean "updatedb and locate"?

 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      09-30-2005
Roedy Green wrote:
> On 30 Sep 2005 09:34:40 +0200, Gordon Beaton <(E-Mail Removed)> 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.
 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      09-30-2005
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?

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      09-30-2005
On 30 Sep 2005 08:28:33 -0700, (E-Mail Removed) 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
 
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