Solved

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

Posted on 2003-12-07
15
1,440 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
object modeling - load and save functions 5 59
How to split this in C++ 4 94
Copy output image from TWindowsMediaPlayer 6 22
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now