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

Posted on 2003-12-07
Last Modified: 2010-05-18
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 );
Question by:shifty_mc
  • 8
  • 4
  • 2
  • +1
LVL 15

Assisted Solution

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

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


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

Author Comment

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.

Accepted Solution

lwinkenb earned 50 total points
ID: 9898065
go to Project->Settings->Link Tab
Now add jvm.lib to Object/Library modules
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

LVL 86

Expert Comment

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")

LVL 15

Expert Comment

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

Author Comment

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?


Expert Comment

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

Author Comment

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

Author Comment

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

Author Comment

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)


Author Comment

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.

Author Comment

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.
LVL 15

Expert Comment

ID: 9902033 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 (or something like this) with C++ launcher source code in your    
JDK installation (see somewhere in JAVA_HOME) 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, and you won't need to write this code.
LVL 15

Expert Comment

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

Author Comment

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

816 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

7 Experts available now in Live!

Get 1:1 Help Now