File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating VM in C

 
Nikhil Jain
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you linking this program? Post the command you're using.
 
Nikhil Jain
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic