Article View: borland.public.jbuilder.java.language
Article #8345JNI and Windows 2000
From: Hosni
Date: Sun, 25 Mar 2001 10:27
Date: Sun, 25 Mar 2001 10:27
119 lines
4685 bytes
4685 bytes
Subject: JNI and Windows 2000
Iļæ½m developing a program in which the C++ code must INVOKE a java method, with the following specifications:
Platform: Windows 2000
Application for C++: MS VC++ 6.0
Application for Java: Borland Jbuilder 4.0.
The description of problem and what I made:
- I have set the CLASSPATH properly to the folder in which my classes exist.
- I have written the java and C++ code in the end of this mail.
- First, The Creation of JavaVM did NOT succeeded in the way that SUN is providing in the javasoft site. I have took the method for this from a guy that submitted the solution (this includes loading of jvm.dll first, and this is NOT mentioned in ALL SUN documents)
- Second, after the Creation of JVM succeeded, the program can NOT find MY class file (Prog.class) although I copied the file in the same folder in which the C++ application is running, and I updated THE CLASSPATH VARABLE, WHICH IT SEEMS TO NOT WORKING WELL ON WINDOWS 2000 , and again NO DOCUMENTS is written about the variable CLASSPATH and Windows 2000.
- PLEASE HELP ME IT IS URGENT.. THANK YOU VERY MUCH.
- Here is the Java code:
//===============================================
public class Prog {
public static void main(String[] args) {
System.out.println("Hello World" + args[0]);
}
}
//===============================================
- Here is the C++ code with must call the main method in the java code above:
//===============================================
#include "jni.h"
#include <afxwin.h>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#endif
#define USER_CLASSPATH "." /* where Prog.class is */
main() {
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];
jint ( JNICALL *JNI_CreateJavaVM)(JavaVM **pvm, void **penv, void *args);
jint (JNICALL *AttachCurrentThread)(JavaVM *vm, void **penv, void *args);
jclass (JNICALL *FindClass)(JNIEnv *env, const char *name);
/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
vm_args.version = JNI_VERSION_1_2;//0x00010001;
//JNI_GetDefaultJavaVMInitArgs(&vm_args);
HINSTANCE jvmDll= (HINSTANCE)NULL;
jvmDll = LoadLibrary("C:\\jdk1.3.0_02\\jre\\bin\\classic\\jvm.dll");
if( jvmDll == NULL )
{
printf( "Problem loading VM library");
}
// Get the .DLL function address of JNI_CreateJavaVM
JNI_CreateJavaVM = (jint (JNICALL *)(JavaVM **, void **, void* ) ) GetProcAddress( jvmDll, "JNI_CreateJavaVM" );
if( !JNI_CreateJavaVM )
{
printf( "Could not find the JNI_CreateJavaVM function in the specified .DLL") ;
}
vm_args.classpath = ".;D:\\JNIPrograms\\prog";
/* Append USER_CLASSPATH to the end of default system class path */
//sprintf(classpath, "%s;.",
// vm_args.classpath);
//vm_args.classpath = classpath;
/* 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("Prog");
if (cls == 0) {
fprintf(stderr, "Can't find Prog class\n");
exit(1);
}
mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if (mid == 0) {
fprintf(stderr, "Can't find Prog.MAIN\n");
exit(1);
}
jstr = env->NewStringUTF(" from C!");
if (jstr == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
args = env->NewObjectArray(1,
env->FindClass("java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
env->CallStaticVoidMethod(cls, mid, args);
jvm->DestroyJavaVM();
}
//===============================================
Technical Details
Message-ID:
<3abdc7f7$1_2@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!207.105.83.65Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f74b3,e463d090334a24cc,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-25 02:38:18 PST From: "Hosni" <h.saqallah@eskadenia.com> Sender: "Hosni" <h.saqallah@eskadenia.com> Reply-To: "Hosni" <h.saqallah@eskadenia.com> Subject: JNI and Windows 2000 Newsgroups: borland.public.jbuilder.java.language X-User-Info: 212.118.3.114 212.118.3.114 NNTP-Posting-Host: 207.105.83.65 Message-ID: <3abdc7f7$1_2@dnews> Date: 25 Mar 2001 02:27:03 -0800 X-Trace: 25 Mar 2001 02:27:03 -0800, 207.105.83.65 Lines: 120 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!207.105.83.65