LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Failure with JNI_CreateJavaVM

Anyone had luck starting the java virtual machine in CVI?

 

I've found a few C++ examples (for example: http://www.jguru.com/forums/view.jsp?EID=1264492) and while they all compile none start the JVM.

0 Kudos
Message 1 of 11
(7,188 Views)

Hi Mark,

 

I've been trying to do this as well but haven't had a whole lot of success with it either.  May I ask what version of the JDK you are using and, could you post the code that you are attempting to run right now?  Is it straight from the link you posted or have you modified it?  I'll take a look at this and see if, between the two of us, there might be a way to get this done.

 

Happy Holidays!

Trey C.

0 Kudos
Message 2 of 11
(7,173 Views)

below is a straight cut and paste from what I'm running (My apologies if there's some pointless code in there as it's just a mess-around module to do a proof of concept).

 

For JNI I believe I'm using 1.2 (but I'm having a difficult time confirming it)...

 

Everything compiles and runs fine, I just get a -1 return on the jni_CreateJavaVM call and the env remains a null...

 

If you have any ideas or things to try it would be greatly welcomed!

 

// Include files
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <jni.h>
#include "JavaCVI.h"


//==============================================================================
// Global functions

int main(int argc, char **argv) {
   
     JNIEnv *env;
     JavaVM *jvm;
     jint res;
     jclass cls;
     jmethodID mid;
     jstring jstr;
     jclass stringClass;
     jobjectArray args;
     JavaVMOption options[1];

 
     JavaVMInitArgs vm_args;
     
     vm_args.version = JNI_VERSION_1_2; //  JNI_VERSION_1_2 is interchangeable for this example
     vm_args.nOptions = 1;
     options[0].optionString = "-Djava.class.path=C:\\Work\\Software\\CVI\\JavaCVI";
     vm_args.options = options;
     vm_args.ignoreUnrecognized = JNI_FALSE;

     
     // Create the Java VM 
     res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);


 
     if (res < 0) {
         fprintf(stderr, "Can't create Java VM\n");
         exit(1);
     }
     cls = (*env)->FindClass(env, "Prog");
     if (cls == NULL) {
         goto destroy;
     }

     
destroy:
     if ((*env)->ExceptionOccurred(env)) {
         (*env)->ExceptionDescribe(env);
     }
     (*jvm)->DestroyJavaVM(jvm);
     
     return 0;
}

        

    





 

0 Kudos
Message 3 of 11
(7,171 Views)

MarksAlias,

 

It looks like your JNI connection is not setup correctly. My guess is that a location for where to locate variables needs to be defined still. Try displaying your java variables to the screen, this should help you debug your issue.

 

 

 

Regards,

Renée M
Applications Engineer
National Instruments
0 Kudos
Message 4 of 11
(7,161 Views)

Can you clarify what you mean by 'java variables'?

 

I read it in two ways:

 

1) the variables in CVI pertaining to java - and those I can view in the 'variables' window in CVI.  They all look acceptable to me, but that could easily be because I'm not sure what I'm looking for.

 

2) The other way i read it is that you are referring to the variables in my java program - but I can't print those because I can't start the JVM to run my program.  I did try adding the '"-verbose:gc,class,jni"; to the option string but I didn't see anything different

0 Kudos
Message 5 of 11
(7,158 Views)

Hello MarksAlias,

 

This sounds like an interesting project.  Ultimately, I have not seen any example code posted on the forums on developer zone.  However, I'm sure there are articles and sample code on the internet that could help to invoke the JVM from C code.

 

Maybe the following article may help.

 

Invoking the Java Virtual Machine

http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/invo.html

Regards,

Shawn S. | NIC
Instrument Driver/IVI PSE
National Instruments
0 Kudos
Message 6 of 11
(7,140 Views)

I tried the sample you linked without much luck - but I wonder if you're onto something in the compiler linking.  Compilers are not my area of expertise, but the command line switches used below:

cl -I<where jni.h is> -MT invoke.c -link <where javai.lib is>\javai.lib

 seems to just refer to where to find stuff, but if I'm using the CVI IDE do I need to explicitly state some of this?  It does seem to find everything ok, including all the java defines but....

 

When I tried running python in CVI, someone on this forum noted the python.h was written for a c++ application and needed some tweaking to work in CVI (though that error was discovered at compile time, not run time like this one) so it is possible that's sublety in there?

 

Open to any ideas

0 Kudos
Message 7 of 11
(7,128 Views)

Mark,

 

CVI will always look in a set of search directories in an attempt to find the libraries required for your program.  If it is unable to find these directories then you should receive an error during compile time that points directly to (for example) the jni.h file.  

 

As for using the python.h file - I am not familiar with this file specifically but, as I'm sure you are aware, there are differences in writing a program in purely C versus C++.  However, to my knowledge, these differences are based on the capabilities of C and would also throw an error if there was a problem with the syntax.  I did look at some of the python documentation and it appears to me that you should be able to use the header file without modification.  The only special notes that are mentioned regarding using it in C are found in section 1.6 of the link above.

 

Finally, there is one document on our website that you may or may not have seen - How Do I Integrate Java with National Instruments Software Products?  Specifically, with the link to Sun's Java Technology homepage, you may find information on their community site regarding this.  From the first link in this paragraph, it doesn't sound like this problem should be restricted to the CVI environment - are you successful in creating the JVM if you compile using a different IDE?

 

Regards,

Trey C

Applications Engineer

National Instruments

0 Kudos
Message 8 of 11
(7,110 Views)

Hi Mark,

 

I'd like to point some alternative on interop between C/C++ and java, there are tools to better handle this such as: Bridj (on the google code) or JNA.

 

Both tools are meant to simplify the process of code-interop between Java and C, and hiding the JNI detail implementations. 

 

 

0 Kudos
Message 9 of 11
(7,100 Views)

Thanks Kusg, I'll definately take a look.

 

I did try recreating the projects in VS2008 using c++ and after some fighting with linking I'm at the same point.  Code all compiles and runs but doesn't start the JVM - so not a CVI thing so much as a me specific problem....I hate those...

0 Kudos
Message 10 of 11
(7,088 Views)