File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Creating VM in C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Creating VM in C" Watch "Creating VM in C" New topic
Author

Creating VM in C

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Hi,

I am trying to Call a Java Method from C. But I am getting an very unusual Error.

I get the error when I try to create the virtual Machine.

#include <jni.h>

#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif

int main() {
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean not;

options[0].optionString = "-Djava.class.path=.";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 1;
vm_args.options = options;

status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
}

I get the error when I call JNI_CreateJavaVM.

Error Message:
Error: Unresolved external 'JNI_CreateJavaVM' referenced from C:\MYWORK\CALLIN.OBJ.

Please HELP!!!11


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41591
    
  55
How are you linking this program? Post the command you're using.


Ping & DNS - my free Android networking tools app
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I am using a bcc32 compiler.

I just type the following in the command prompt
c:\mywork> bcc32 calling.c

I don't understand linking. Do I have to do anything else.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
The following is the file bcc32.cfg used for linking

-I"%JAVA_HOME%\include"
-I"%JAVA_HOME%\include\win32"
-L"c:\Program Files\Java\jdk1.5.0_05\jre\bin\client\"
-I"C:\Borland\Bcc55\include"
-L"%JAVA_HOME%\lib"
-L"C:\Borland\Bcc55\Lib"
-I"c:\MyWork"
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I even tried using this...
C:\MyWork>bcc32 -tWM -IC:\Program/Files\Java\jdk1.5.0_05\include -IC:\Program/Files\Java\jdk.1.5.0_05\include\win32 -LC:\Program/Files\Java\jdk.1.5.0_05\lib Cal
lingJavaCodeFromC.c

But still I am getting this error...

HELP!!!
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I again did the whole things from strach but failed again.

1. I wrote the C program
#include <jni.h>

#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif

int main() {
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean not;

options[0].optionString = "-Djava.class.path=.";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 1;
vm_args.options = options;

status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
}

2. I tried compiling the above. But I get
Error: Unresolved external 'JNI_CreateJavaVM' referenced from C:\JNITEST\JNIEXAMPLE.OBJ

i. I am using BCC compiler
ii. bcc32.cfg file contains
-I"C:\BCC55\Include"
-I"C:\Program Files\Java\jdk1.5.0_02\include\win32"
-I"C:\Program Files\Java\jdk1.5.0_02\include"
-L"C:\BCC55\Lib"
-L"c:\Program Files\Java\jdk1.5.0_02\lib"

iii. Command Line Argument being given for compiling is
C:\JniTest>bcc32 -IC:\Program/Files\Java\jdk1.5.0_02\include JniExample.c -Lc:\Program/Files\Java\jdk1.5.0_02\lib\jvm.lib

-IC:\Program/Files\Java\jdk1.5.0_02\include: Contains Jni.h

iv. Env Variable path has the following
C:\Program Files\Java\jdk1.5.0_02\bin\;
C:\bcc55\Bin\;
C:\Program Files\Java\jdk1.5.0_02\jre\bin\client\;
C:\Program Files\Java\jdk1.5.0_02\jre\bin

WHERE AM I GOING WRONG ...



Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
guys....
now I am trying out with Microsoft's compiler. But Again failed.. I think its really not worth spending so much time on it... This is my final try...

1. What are the paths that we need to set in the env variable.
2. What would be the syntax on MS compiler

I am trying the following commands
cl -IC:\Program/Files\Java\jdk1.5.0_05\include -MD invoke.c -link C:\Program/Files\Java\jdk1.5.0_05\lib\jvm.lib
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating VM in C