Name: skT45625 Date: 05/04/2000
java version "1.2.2"
Classic VM (build JDK-1.2.2-001, native threads, symcjit)
The jvm is started and attached to native thread using the attache.c jni
example, but a method "run" of a class "HelloClient" is invoked that uses rmi
to invoke a method "print" on server "HelloServer" implementing "HelloInterf".
Both sides (client and server) use a general policy file granting all
permissions. The "ClassFileServer" from Sun is used to provide the stubs. The
registry and the client do not have access to the stubs via CLASSPATH.
The "ClassCastException" does not occur when executing the "HelloClient" on
command line when moving run to main. When executing on command line
the "ClassFileServer" prints
"reading: HelloServer_Stub" while it prints
"reading: HelloServer_Stub
reading: HelloInterf"
when calling the "HelloClient" through jni.
Calling through jni the exception java.lang.ClassCastException: HelloServer_Stub
at HelloClient.run(HelloClient.java:15)
is always thrown.
The exception doesn't occure when making the stub accessable through CLASSPATH
********************************
Here is the c-code:
/* Note: This program only works on Win32. */
#include <windows.h>
#include <jni.h>
JavaVM *jvm; /* The virtual machine instance */
#define PATH_SEPARATOR ';'
#define USER_CLASSPATH "c:\\rmi" /* where HelloClient.class is */
void thread_fun(void *arg)
{
const char* str;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
JNIEnv *env;
int threadNum = (int)arg;
/* Pass NULL as the third argument */
res = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
if (res < 0) {
fprintf(stderr, "Attach failed\n");
return;
}
cls = (*env)->FindClass(env, "HelloClient");
if (cls == 0) {
goto detach;
}
mid = (*env)->GetStaticMethodID(env, cls, "run",
"()Ljava/lang/String;");
if (mid == 0) {
goto detach;
}
jstr = (*env)->CallStaticObjectMethod(env, cls, mid);
if((*env)->ExceptionCheck(env) == JNI_TRUE)
{
(*env)->ExceptionClear(env);
goto detach;
}
str = (*env)->GetStringUTFChars(env, jstr, NULL);
printf("Java says: %s", str);
(*env)->ReleaseStringUTFChars(env, jstr, str);
detach:
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
(*jvm)->DetachCurrentThread(jvm);
}
int main() {
JNIEnv *env;
int i;
jint res;
JavaVMInitArgs vm_args;
JavaVMOption options[2];
options[0].optionString =
"-Djava.class.path=" USER_CLASSPATH;
options[1].optionString = "-Xnoclassgc";
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = TRUE;
/* 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);
}
for (i = 0; i < 1; i++)
/* We pass the thread number to every thread */
_beginthread(thread_fun, 0, (void *)i);
Sleep(1000); /* wait for threads to start */
(*jvm)->DestroyJavaVM(jvm);
return 0;
}
********************************
Here is the java-code:
import java.rmi.*;
public interface HelloInterf extends Remote
{
public String print() throws RemoteException;
}
*************
import java.rmi.server.*;
import java.rmi.*;
public class HelloServer extends UnicastRemoteObject implements HelloInterf
{
public HelloServer() throws RemoteException
{
super();
}
public String print() throws RemoteException
{
return "I am the server";
}
public static void main (String[] s)
{
try
{
System.setProperty("java.rmi.server.useCodebaseOnly", "true");
System.setProperty("java.rmi.server.codebase","http://schaeferm-
tr:8080/");
System.setProperty("java.security.policy", "c:\\rmi\\myPolicy.policy");
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
HelloServer server = new HelloServer();
Naming.rebind("//schaeferm-tr/HelloServer", server);
} catch (Exception e) {e.printStackTrace();}
}
}
**************
import java.rmi.*;
public class HelloClient
{
public static String run ()
{
try
{
System.setProperty("java.security.policy", "c:\\rmi\\myPolicy.policy");
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
HelloInterf server = (HelloInterf) Naming.lookup("//schaeferm-
tr/HelloServer");
return server.print();
} catch (Exception e)
{e.printStackTrace(); throw new RuntimeException();}
}
}
(Review ID: 101676)
======================================================================
- duplicates
-
JDK-4489399 Thread losing ContextClassLoader through Invocation API
-
- Closed
-