Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1456
  • Last Modified:

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 );
}
0
shifty_mc
Asked:
shifty_mc
  • 8
  • 4
  • 2
  • +1
2 Solutions
 
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
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!

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now