Solved

JNI issue - trying to run java program from c++

Posted on 2003-12-07
15
1,443 Views
Last Modified: 2010-05-18
Hello,
I'm new to this particular topic and only a beginner at c++, but all I'm basically trying to do is run a Java program from within a C++ program.  I've been looking for ages and have found lots of things about native methods - but from what I can gather that's for more interaction than I need?
My code is below - very basic - just want to set up a JVM then run the program (call the main method).  For the time being I'm putting all my files in one directory, so the java class file (GetImage.class) is there as well as the .cpp and the jni.h and jni_md.h.
I get compile errors of "base operand of `->' has non-pointer type `JNIEnv_'" WHAT AM DOING WRONG!!
Thanks for your time, really appreciate it, Mark.


#include <stdio.h>
#include "jni.h"

using namespace std;

JNIEnv* create_vm() {
      JavaVM* jvm;
      JNIEnv* env;
      JavaVMInitArgs args;
      JavaVMOption options[1];
      
      args.version = JNI_VERSION_1_4;
      args.nOptions = 1;
      options[0].optionString = "-Djava.class.path=.";
      args.options = options;
      args.ignoreUnrecognized = JNI_FALSE;

      JNI_CreateJavaVM(&jvm, (void **)&env, &args);
      return env;
}

void invoke_class(JNIEnv* env) {
      jclass GetImageClass;
      jmethodID mainMethod;
      jobjectArray applicationArgs;
      jstring applicationArg0;

      GetImageClass = (*env)->FindClass(env, "GetImage");

      mainMethod = (*env)->GetStaticMethodID(env, GetImageClass, "main", "([Ljava/lang/String;)V");

      applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
      applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
      (*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);

      (*env)->CallStaticVoidMethod(env, GetImageClass, mainMethod, applicationArgs);
}


int main(int argc, char **argv) {
      JNIEnv* env = create_vm();
      invoke_class( env );
}
0
Comment
Question by:shifty_mc
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 15

Assisted Solution

by:dualsoul
dualsoul earned 100 total points
ID: 9895148
hm....i can't check it right now...but

(*env)->NewStringUTF(env, "From-C-program");
...it sees it shoud look like:

env->NewStringUTF("From-C-program");

and all other call to JNIEnv the same way...try
0
 

Author Comment

by:shifty_mc
ID: 9895295
Yes you're exactly right - I just spotted it myself.

One other thing though - any ideas what exactly I have to do to create an executable out of this? (assume under windows and I'm using Visual C++ 6)

At the mo it compiless fine but I get a link error: "error LNK2001: unresolved external symbol __imp__JNI_CreateJavaVM@12"

I understand you have to link jvm.lib which I've done (tools, options, directories) but is there anything else? Or maybe I didn't do it right?

Anyone?! Cheers.
0
 
LVL 5

Accepted Solution

by:
lwinkenb earned 50 total points
ID: 9898065
go to Project->Settings->Link Tab
Now add jvm.lib to Object/Library modules
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 9898073
>>I understand you have to link jvm.lib which I've done (tools, options, directories)

Have you tried to add

#pragma comment ( lib, "jni.lib")

also?
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 9899095
> I understand you have to link jvm.lib
certanly you should link library to your project :)
for instance, like lwinkenb suggested you
0
 

Author Comment

by:shifty_mc
ID: 9900571
Thanks for your help - used lwinkenb's suggestion and it was fine - is jkr's suggestion a more general solution, say if you weren't using Visual C++?

Anyway, sorry to keep this going through complete ignorance guys but now it compiles and builds the .exe fine but fails to start because "jvm.dll" wasn't found.

I copied it into the directory and no joy so I presume it's more complicated - do I have to link this in some way as well or somehow build it myself?

Cheers.
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9900738
copying the dll to the project directory should work.
0
 

Author Comment

by:shifty_mc
ID: 9900807
or even better make it work without any .dlls?
0
 

Author Comment

by:shifty_mc
ID: 9900812
like i said - searched for jvm.dll and copied it into my directory but still got error.
0
 

Author Comment

by:shifty_mc
ID: 9900813
like i said - searched for jvm.dll and copied it into my directory but still got error. (points increased to 150 cos I'm asking more questions)

Cheers.
0
 

Author Comment

by:shifty_mc
ID: 9901184
ah - finds dll now - was overlooking something simple as normal.

Now though I just get a general windows error saying it's encountered a problem - I've stripped the java file to an absolute minimum which definitely works on its own so I really don't know. What about JNI_VERSION- is that important? I just left the value that was already there from the example on the internet - how do I tell what version I have? Could that be resonsbile? anything else?

Phew, such a small thing can be so frustrating.
0
 

Author Comment

by:shifty_mc
ID: 9901458
Well, I've narrowed down the problem to the actual call to create the jvm - it isn't working.  Why oh why?
I take it the JNI versions are the same as JDK so I'm using 1_4. Still not sure what the other options are about though or if they're relevant.
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 9902033
hm....do you have JVM installed properly? it's not only jvm.dll, but also a set of runtime classes, so you should run your JVM, as it were installed, not copy it without all files it used.
   I can suggest you 2 things:
 1) there were launcher.zip (or something like this) with C++ launcher source code in your    
JDK installation (see somewhere in JAVA_HOME)...so if you haven't still looked at it, do it :) (this is example of launch JVM from C++ and executing some Java code in it)

 2) if you only need to run your Java from .exe, and don't try to train yourself, there are a lot of Wrappers which can automaticly generate code to run JVM, or pack Java code to .exe and attach launchrer automaticly, so search google.com, and you won't need to write this code.
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 9902048
for instance there a tool, called  jtoexe  (J2EXE) to create an .exe wrapper for Java code
0
 

Author Comment

by:shifty_mc
ID: 9904752
The JVM is installed properly (or at least i cane run 'java' from the command line).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question