 |
|
 |
Hi,
I compiled the CTest.cpp. But I got error that "unable to open StdAfx.h and jni.h ". Can any body help me?
I am attaching the Source code:
CTest.cpp ===========
#include "StdAfx.h" #include <stdio.h> #include <jni.h> #include <string.h>
#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */ #define USER_CLASSPATH "." /* where Prog.class is */
struct ControlDetail { int ID; char Name[100]; char IP[100]; int Port; };
struct WorkOrder { char sumSerialId[20]; char accessNumber[18]; char actionType[4]; char effectiveDate[24]; char fetchFlag[2]; char reason[456]; char accessSource[100]; };
JNIEnv* create_vm(JavaVM ** jvm) { JNIEnv *env; JavaVMInitArgs vm_args; JavaVMOption options; options.optionString = "-Djava.class.path=D:\\Java Src\\TestStruct"; //Path to the java source code vm_args.version = JNI_VERSION_1_5; //JDK version. This indicates version 1.5 vm_args.nOptions = 1; vm_args.options = &options; vm_args.ignoreUnrecognized = 0; int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args); if(ret < 0) printf("\nUnable to Launch JVM\n"); return env; }
int main(int argc, char* argv[]) { JNIEnv *env; JavaVM * jvm; env = create_vm(&jvm); if (env == NULL) return 1; struct ControlDetail ctrlDetail; ctrlDetail.ID = 11; strcpy(ctrlDetail.Name,"HR-HW"); strcpy(ctrlDetail.IP,"10.32.164.133"); ctrlDetail.Port = 9099; printf("Struct Created in C has values:\nID:%d\nName:%s\n IP:%s\nPort:%d\n",ctrlDetail.ID,ctrlDetail.Name,ctrlDetail.IP,ctrlDetail.Port);
/********************************************************/ struct WorkOrder WO[2]; strcpy(WO[0].sumSerialId,"2000"); strcpy(WO[0].accessNumber,"2878430"); strcpy(WO[0].actionType,"04"); strcpy(WO[0].effectiveDate,"25-12-2007 12:20:30 PM"); strcpy(WO[0].fetchFlag, "0"); strcpy(WO[0].reason,"Executed Successfully"); strcpy(WO[0].accessSource,"PMS"); strcpy(WO[1].sumSerialId,"1000"); strcpy(WO[1].accessNumber,"2878000"); strcpy(WO[1].actionType,"T4"); strcpy(WO[1].effectiveDate,"25-12-2007 11:20:30 PM"); strcpy(WO[1].fetchFlag,"0"); strcpy(WO[1].reason,""); strcpy(WO[1].accessSource,"RMS");
jclass clsH=NULL; jclass clsC = NULL; jclass clsW = NULL; jclass clsR = NULL; jmethodID midMain = NULL; jmethodID midCalling = NULL; jmethodID midDispStruct = NULL; jmethodID midDispStructArr = NULL; jmethodID midRetObjFunc = NULL; jmethodID midCtrlDetConst = NULL; jmethodID midWoConst = NULL; jobject jobjDet = NULL; jobject jobjRetData = NULL; jobjectArray jobjWOArr = NULL; //Obtaining Classes clsH = env->FindClass("HelloWorld"); clsC = env->FindClass("ControlDetail"); clsW = env->FindClass("WorkOrder"); //Obtaining Method IDs if (clsH != NULL) { midMain = env->GetStaticMethodID(clsH, "main", "([Ljava/lang/String V"); midCalling = env->GetStaticMethodID(clsH,"TestCall","(Ljava/lang/String V"); midDispStruct = env->GetStaticMethodID(clsH,"DisplayStruct","(LControlDetail I"); midDispStructArr = env->GetStaticMethodID(clsH,"DisplayStructArray","([LWorkOrder V"); midRetObjFunc = env->GetStaticMethodID(clsH,"ReturnObjFunc","()Ljava/lang/Object;"); } else { printf("\nUnable to find the requested class\n"); } if(clsC != NULL) { //Get constructor ID for ControlDetail midCtrlDetConst = env->GetMethodID(clsC, "<init>", "(ILjava/lang/String;Ljava/lang/String;I)V"); } else { printf("\nUnable to find the requested class\n"); }
if(clsW != NULL) { //Get Constructor ID for WorkOrder midWoConst = env->GetMethodID(clsW, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String V"); } else { printf("\nUnable to find the requested class\n"); }
/************************************************************************/ /* Now we will call the functions using the their method IDs */ /************************************************************************/ if(midMain != NULL) env->CallStaticVoidMethod(clsH, midMain, NULL); //Calling the main method. if (midCalling!=NULL) { jstring StringArg = env->NewStringUTF("\nTestCall:Called from the C Program\n"); //Calling another static method and passing string type parameter env->CallStaticVoidMethod(clsH,midCalling,StringArg); } printf("\nGoing to Call DisplayStruct\n"); if (midDispStruct!=NULL) { if(clsC != NULL && midCtrlDetConst != NULL) { jstring StringArgName = env->NewStringUTF(ctrlDetail.Name); jstring StringArgIP = env->NewStringUTF(ctrlDetail.IP); //Creating the Object of ControlDetail. jobjDet = env->NewObject(clsC, midCtrlDetConst, (jint)ctrlDetail.ID, StringArgName, StringArgIP, (jint)ctrlDetail.Port); } if(jobjDet != NULL && midDispStruct != NULL) env->CallStaticIntMethod(clsH,midDispStruct,jobjDet); //Calling the method and passing ControlDetail Object as parameter } //Calling a function from java and passing Structure array to it. printf("\n\nGoing to call DisplayStructArray From C\n\n"); if (midDispStructArr!=NULL) { if(clsW != NULL && midWoConst != NULL) { //Creating the Object Array that will contain 2 structures. jobjWOArr = (jobjectArray)env->NewObjectArray(2,clsW,env->NewObject(clsW, midWoConst,env->NewStringUTF(""),env->NewStringUTF(""),env->NewStringUTF(""), env->NewStringUTF(""),env->NewStringUTF(""),env->NewStringUTF(""),env->NewStringUTF(""))); //Initializing the Array for(int i=0;i<2;i++) { env->SetObjectArrayElement(jobjWOArr,i,env->NewObject(clsW, midWoConst,env->NewStringUTF(WO[i].sumSerialId), env->NewStringUTF(WO[i].accessNumber), env->NewStringUTF(WO[i].actionType), env->NewStringUTF(WO[i].effectiveDate), env->NewStringUTF(WO[i].fetchFlag), env->NewStringUTF(WO[i].reason), env->NewStringUTF(WO[i].accessSource))); } } //Calling the Static method and passing the Structure array to it. if(jobjWOArr != NULL && midDispStructArr != NULL) env->CallStaticVoidMethod(clsW,midDispStructArr,jobjWOArr); } //Calling a Static function that return an Object if (midRetObjFunc != NULL) { //Calling the function and storing the return object into jobject type variable //Returned object is basically a structure having two fields (string and integer) jobjRetData = (jobject)env->CallStaticObjectMethod(clsH,midRetObjFunc,NULL); //Get the class of object clsR = env->GetObjectClass(jobjRetData); //Obtaining the Fields data from the returned object jint nRet = env->GetIntField(jobjRetData,env->GetFieldID(clsR,"returnValue","I")); jstring jstrLog = (jstring)env->GetObjectField(jobjRetData,env->GetFieldID(clsR,"Log","Ljava/lang/String;")); const char *pLog = env->GetStringUTFChars(jstrLog,0); printf("\n\nValues Returned from Object are:\nreturnValue=%d\nLog=%s",nRet,pLog); //After using the String type data release it. env->ReleaseStringUTFChars(jstrLog,pLog); } //Release resources. int n = jvm->DestroyJavaVM(); return 0; }
Java Codes ***********
ControlNEDetail.java ====================
public class ControlNEDetail { public int ID; public String Name; public String IP; public int Port;
public ControlNEDetail(int nID, String szName, String szIP, int nPort) { this.ID = nID; this.Name = szName; this.IP = szIP; this.Port = nPort; } }
ReturnData.java ================
public class ReturnData { int returnValue; String Log; public ReturnData(int nRetVal, String szLog) { this.returnValue = nRetVal; this.Log = szLog; } }
WorkOrder.java ===============
public class WorkOrder { String sumSerialId; String accessNumber; String actionType; String effectiveDate; String fetchFlag; String reason; String accessSource; public WorkOrder(String szSumID,String szAccNum, String szActType, String szEffectDate, String fetchFlg, String szReason, String szAccSrc ) { this.sumSerialId = szSumID; this.accessNumber = szAccNum; this.actionType = szActType; this.effectiveDate = szEffectDate; this.fetchFlag = fetchFlg; this.reason = szReason; this.accessSource = szAccSrc; } };
HelloWorld.java ================
public class HelloWorld { public static void main(String args[]) { System.out.println("Hello World!"); System.out.println("This is the main function in HelloWorld class"); } public static void TestCall(String szArg) { System.out.println(szArg); } public static int DisplayStruct(ControlNEDetail ctrlDetail) { System.out.println("Structure is:\n-------------------------"); System.out.println("Name:" + ctrlDetail.Name); System.out.println("IP:" + ctrlDetail.IP); System.out.println("Port" + ctrlDetail.Port); return 1; } public static void DisplayStructArray(WorkOrder ArrWO[]) { System.out.println("WorkOrders are Given hereunder:\n----------------------------"); for(int i = 0; i< ArrWO.length;i++) { System.out.println("<---Work Order Number:" + String.valueOf(i+1) + "<---"); System.out.println("Sum_Serial_ID: " + ArrWO[i].sumSerialId); System.out.println("Access_Number: " + ArrWO[i].accessNumber); System.out.println("Action_Type: " + ArrWO[i].actionType); System.out.println("Effective_Date: " + ArrWO[i].effectiveDate); System.out.println("Fetch_Flag: " + ArrWO[i].fetchFlag); System.out.println("Reason: " + ArrWO[i].reason); System.out.println("Access_Source: " + ArrWO[i].accessSource); }
} public static Object ReturnObjFunc() { System.out.println("Going to return an object from java"); ReturnData RetData = new ReturnData(1,"Successfull function call"); return RetData; } }
Thanks
Chinmaya
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
It seems like this isn't working with VS 2008/2005. It gets compiled but not running. Doesn't even give the 'jvm.dll' error. :(
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
I get an error message saying that the application has failed to start because jvm.dll was not found.
I have tried reinstalling my JDK (which is version 1.6.0 rev 14).
Any ideas?
--- There are 10 types of people in this world: those who understand binary, and those who don't.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Fixed it...
I needed to add the directory containing jvm.dll to my path. In my case this was:
C:\Program Files\Java\jre6\bin\client
Hope this is helpful to someone else!
--- There are 10 types of people in this world: those who understand binary, and those who don't.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This is how i am compiling the code...
g++ -I/usr/jdk1.6.0/include -I/usr/jdk1.6.0/include/solaris -L/usr/jdk1.6.0/jre/bin -L/usr/jdk1.6.0/jre/lib/sparc -ljvm -ljava CTest.cpp
any help would be appreciated!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I get the error Unable to load JVM with error -1 which is i think is the unknown error as written in the header file. What should I do Know?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Hi,
I had same error found but i got success when i run my exe from path where jvm.dll is located that is :C:\Program Files\Java\jdk1.6.0_13\jre\bin\client
Hope this will help you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
when i compile the code...i get this error :-
error C2065: 'JNI_VERSION_1_6' : undeclared identifier
please help ....
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Sorry for late reply I was away for some time.
Which version of JDK you have installed on your machine. I am sure that you will not be using JDK 1.6. If not then either you will have to change the C code or you will have to install JDK 1.6.
If you want to change the code then suppose if you have installed JDK 1.4 then JNI_VERSION_1_6 will be replaced by JNI_VERSION_1_4.
Hope this will help.
Ahmad Jalil Qarshi
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
HI...
i have a problem here. i dont know where to read or to get source or tutorials. i have to do a web service (using tomcat as my web server and eclipse as my compiler)...other dept will generate the .dll file (using JNI) and pass it to me...and i have to write the interface and implementation class to package and wrap it with SOA.
I know the basic of web service ( where u have to write the java interface and implementation class using tomcat as my web server and eclipse as compiler). but how can i call the .dll files ... there is a function like
static { System.loadLibrary("imageStitch"); }
i just need help on tutorial..where can i find it...
Thanks. NORAZANITA 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thank you for your good information in advance... I tried to create JVM using JNI_CreateJavaVM function in dll. But this function returned only '-1'. I couldn't find the difference so I tried to create JVM as same code in console exe. In exe, this function returned '0'. Do you know why this difference happened and how i can create JVM in dll. Have a nice day...
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi, Frankly speaking I faced the same problem before uploading this article. I made a static library on IBM AIX server in which I called this JNI_CreateJavaVM function but failed. I tried my level best to find out the solution on different forums but failed. If you find some solution let me know.
Thanks,
Ahmad Jalil Qarshi
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi,
I'm using the dll and getting -1 as well. Can you please tell me how to successfully use JNI_CreateJavaVM?
Any help would be appreciated.
Regards
|
| Sign In·View Thread·PermaLink | 3.00/5 (1 vote) |
|
|
|
 |
|
 |
some actual C/C++ code that demonstrates that side of the equation? Without that, the article is pretty lacking - I'm not going to vote at the moment, but it doesn't bode well.
¡El diablo está en mis pantalones! ¡Mire, mire!Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks Jim Crafton for your comments.
Well, this is my first article on any site. I would like to know exactly what do you mean by "some actual C/C++ code that demonstrates that side of the equation?". I didn't understand what do you want me to do to make it better. Because I have uploaded both C/C++ and Java code.
I would appreciate any recommendation(s)/suggestion(s).
Regards,
Ahmad Jalil Qarshi
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Well in the article, and keep in mind the article is the first thing people see. I don't even bother to download the code unless the article itself is compelling/interesting enough. So in the article, you have only 2 brief lines of C/C++ code, and it's not at all clear what's going on (unless you've had the misfortune of dealing with JNI). So a bit more explanation about what's going on, for example, show an actual function call of a java object, would really help. In other words: more details please!
¡El diablo está en mis pantalones! ¡Mire, mire!Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks Jim,
Yes you are right that it needs some more detail. I am totally agree with you. I will update this article within few days and I will come up with all the missing things.
Thanks for your kind guidance.
Regards,
Ahmad Jalil Qarshi
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
As a rule, it's better to post a more complete article, than to post just an brief outline, and then fill it in later. Many of us tend to vote articles like those down quite severely. This one had just enough to make me hesitate, but if you do submit another article in the future, then try and make it more complete the first time. Good luck!
¡El diablo está en mis pantalones! ¡Mire, mire!Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
|
| Sign In·View Thread·PermaLink | 1.40/5 (4 votes) |
|
|
|
 |