Name: rlT66838 Date: 04/10/2000
java version "1.2.2"
Classic VM (build JDK-1.2.2-001, native threads, symcjit)
I have an application where a C++ program invokes an instance of the JVM. It
them attempts to call a native method defined in the C++ program which invoked
the JVM, but it instead stops program execution without throwing any
exceptions. Attempts to load the executable (using System.load and
System.loadLibrary) caused early termination of the program without throwing
any exceptions.
Test.java:
public class Test
{
native public void nativeTest();
public void javaTest()
{
System.out.println("java");
nativeTest();
System.out.println("java");
}
}
Test.h (created by the javah tool)
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Test */
#ifndef _Included_Test
#define _Included_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Test
* Method: nativeTest
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Test_nativeTest
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Test.cxx
#include <string>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <jni.h>
#include "Test.h"
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#endif
#define USER_CLASSPATH "."
void error(std::string err)
{
std::cerr << err << std::endl;
exit(1);
}
JNIEXPORT void JNICALL Java_Test_nativeTest(JNIEnv *, jobject)
{ std::cout << "C++" << std::endl; }
int main(void)
{
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jobject obj;
char classpath[1024];
/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
vm_args.version = 0x00010022;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
sprintf(classpath, "%s%c%s",vm_args.classpath,PATH_SEPARATOR,USER_CLASSPATH);
vm_args.classpath = classpath;
if ( (res = JNI_CreateJavaVM( &jvm, ((void**) &env), &vm_args))<0)
error("Can't create Java VM");
if ( !(cls = env->FindClass("Test")) ) error("Can't find Test class");
if ( !(obj = env->AllocObject(cls)) ) error("Cannot instantiate Test");
if ( !(mid = env->GetMethodID(cls, "javaTest", "()V")) )
error("Can't find Test.javaTest");
env->CallVoidMethod(obj, mid);
jvm->DestroyJavaVM();
return 0;
}
Output:
java
(Review ID: 103503)
======================================================================