• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
  • Tim Holloway
  • Carey Brown
  • salvin francis

Undefined reference in JNI

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,I was trying to embedd some java code in C and needed to invoke the JVM.But i keep getting an error - 'undefined reference to 'JNI_CreateJavaVM''.I'm not sure what i'm doing wrong.
Here is the program and the command i use to run it.
(the following program is a sample program i took off the web,but it shows me the same error)I'd greatly appreciate it if someone could help me here.I'm almost done pulling all my hair out.
I'm programming on linux and have jdk1.2.2 installed.
I think the problem i might have is because of my libjava.so and libjvm.so and the like,but i'm not sure exactly tho.
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#define USER_CLASSPATH "." /* where Prog.class is */
main() {
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];

/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
vm_args.version = 0x00010001;
/* Append USER_CLASSPATH to the end of default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,&env,&vm_args);
if (res < 0) {<br /> fprintf(stderr, "Can't create Java VM\n");<br /> exit(1);<br /> }<br /> cls = (*env)->FindClass(env, "Prog");
if (cls == 0) {
fprintf(stderr, "Can't find Prog class\n");
mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String <http://www.javaranch.com/ubb/wink.gif>; V");
if (mid == 0) {
fprintf(stderr, "Can't find Prog.main\n");
jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == 0) {
fprintf(stderr, "Out of memory\n");
args = (*env)->NewObjectArray(env, 1,
(*env)->FindClass(env, "java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "Out of memory\n");
(*env)->CallStaticVoidMethod(env, cls, mid, args);

gcc -I -L-ljava invoke.c
The java file is a simple program(Prog.java) which just prints something to the screen.
Anyhelp will be greatly appreciated it.
frankie aranha
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,like i guessed the problem was due to my not loading the shared libraries properly(i.e.libjvm.so,libjava.so,libhpi.so)
.I load them explicitly when i compile them and it compiles fine.But when i try to run the program it shows an error saying that it cannot load the shared libraries.Here's the screen command and output:
gcc -L/home/frankie/Java/JNI/Ex3 -ljvm
-L/home/frankie/Java/JNI/Ex3 -ljava
-L/home/frankie/Java/JNI/Ex3 -lhpi invoke.c -o invoke
now when i run invoke:
error while loading shared libraries:libjvm.so:cannot load shared object file:no such file or directory

Any suggestions ??I know i'm screwing up with the shared libs but where should they all be??and am i loading them right??
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds as though you need to provide the JVM with the same classpaths to your shared libraries as you used when you compiled. Since the compiler and jvm use their own classpath settings you must make external lib's available for both operations. For instance you can run the program like so (example on a solaris system)
java -classpath $CLASSAPTH:path/to/shared/libs MyApp
This is my guess anyway. Hope this helps.
[This message has been edited by Sean MacLean (edited October 02, 2001).]
frankie aranha
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right.The program had to load the library files at runtime also, and so i had to specify the path in the library load file-
'ld.so.conf'.I did that and it runs fine,apart from the fact that my program isn't right..:-)but i'll take em on one at a time.Thanks a bunch.!
money grubbing section goes here:
create, convert, edit or print DOC and DOCX in Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!