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

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 );
}
shifty_mcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dualsoulCommented:
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
shifty_mcAuthor Commented:
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
lwinkenbCommented:
go to Project->Settings->Link Tab
Now add jvm.lib to Object/Library modules
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

jkrCommented:
>>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
dualsoulCommented:
> I understand you have to link jvm.lib
certanly you should link library to your project :)
for instance, like lwinkenb suggested you
0
shifty_mcAuthor Commented:
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
lwinkenbCommented:
copying the dll to the project directory should work.
0
shifty_mcAuthor Commented:
or even better make it work without any .dlls?
0
shifty_mcAuthor Commented:
like i said - searched for jvm.dll and copied it into my directory but still got error.
0
shifty_mcAuthor Commented:
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
shifty_mcAuthor Commented:
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
shifty_mcAuthor Commented:
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
dualsoulCommented:
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
dualsoulCommented:
for instance there a tool, called  jtoexe  (J2EXE) to create an .exe wrapper for Java code
0
shifty_mcAuthor Commented:
The JVM is installed properly (or at least i cane run 'java' from the command line).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.