Thread.currentThread().getContextClassLoader() is failled when calling from JNI
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ranga_reddy
Posted On:   Monday, June 7, 2004 07:26 AM

HI, Getting strange error in JNI..Thread.currentThread().getContextClassLoader() is failled when calling from JNI. Calling java from C using JNI invocation API.Please find here My programs I am working on Solaris 8 SPARC machine, java 1.2 installed When I execute using "java" it works fine, But from the C programme using JNI it fails ...!!! Please help to resolve this C- programme **********************************************************invoke.c******************************** #include #ifdef _WIN32 #define PATH_SEPARATOR ';' #else /* UNIX */ #define PATH_SEPARATOR ':' #endif #define USER_CLASSPATH ".&quo   More>>

HI,
Getting strange error in JNI..Thread.currentThread().getContextClassLoader() is failled when calling from JNI.

Calling java from C using JNI invocation API.Please find here My programs

I am working on Solaris 8 SPARC machine, java 1.2 installed

When I execute using "java" it works fine, But from the C programme using JNI it fails ...!!!

Please help to resolve this

C- programme
**********************************************************invoke.c********************************
#include

#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#endif

#define USER_CLASSPATH "." /* where Prog.class is */

main() {
JNIEnv *env,*env1,*env2,*env3;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];
jthrowable exc;
jobject object_MyClass;


/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
vm_args.version = 0x00010001;

JNI_GetDefaultJavaVMInitArgs(&vm_args);

/* Append USER_CLASSPATH to the end of default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
//JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args);
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,&env,&vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM
");
exit(1);
}

cls = (*env)->FindClass(env, "Myname");
if (cls == 0) {
fprintf(stderr, "Can't find Prog class
");
exit(1);
}



mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
if (mid == 0) {
fprintf(stderr, "Can't find Prog.main
");
exit(1);
}

jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == 0) {
fprintf(stderr, "Out of memory
");
exit(1);
}

args = (*env)->NewObjectArray(env, 1,
(*env)->FindClass(env, "java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "Out of memory
");
exit(1);
}

(*env)->CallStaticVoidMethod(env, cls, mid, args);
exc = (*env)->ExceptionOccurred(env);
if(exc)
{
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
printf("Sorry Failed in adding user
");
}
(*jvm)->DestroyJavaVM(jvm);
}

****************************************************************************************************************************

Java programe is
*****************************************************Myname.java******************************************
import java.io.InputStream;
import java.net.URL;



public class Myname{

public InputStream getResourceAsStream(Class c, String s)
{


InputStream inputstream = null;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();

if(classloader != null)
inputstream = classloader.getResourceAsStream(s);

if(inputstream == null)
{
inputstream = c.getClassLoader().getResourceAsStream(s);
if(inputstream == null)
inputstream = ClassLoader.getSystemResourceAsStream(s);
}
return inputstream;
}

public static void main(String[ ] args){
try{
Myname c = new Myname();
Object str = c.getResourceAsStream((Class) c.getClass(),"crystal_enterprise.properties");
}
catch(Exception e)
{
System.out.println("Error
"+e);
e.printStackTrace();

}
}
}
**************************************************************************************************************************

   <<Less
About | Sitemap | Contact