JNI Runtime.exec hangs

Hi, we're doing some java development, both standalone and used within C using jni.

The exact same application runs fine when used standalone.

But when we try to use it within C (via jni) it hangs on Runtime.exec()... (Used both with a command string and with a command string-array)

All this happens on linux, with Sun's JDK1.3.1 using the gcc-2.9.6 c-compiler and Sun's java-compiler for the java sources.

Does anyone have any clue on this?

We can't use any command using Runtime.exec via JNI (tried a really silly 'echo' command)

[589 byte] By [arjenm] at [2007-9-26 1:40:57]
# 1

We tested now with both JDK1.4 and IBM's JDK1.3

and even with a really simple example it fails:

Test.java :

import java.lang.*;

class Test

{

public Test()

{

System.out.println("Starting program");

try

{

Process p = Runtime.getRuntime().exec("/bin/ls");

p.waitFor(); // Hangs here

}

catch (Exception e){System.out.println("Exception: " + e);}

}

}

The c-code to invoke our java:

#include <jni.h> #ifdef _WIN32

#define PATH_SEPARATOR ';'

#else /* UNIX */

#define PATH_SEPARATOR ':'

#endif

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

main()

{

JNIEnv *env;

JavaVM *jvm;

JDK1_1InitArgs vm_args;

jint res;

jclass test_class;

jobject test_object;

jmethodID mid;

jstring jstr;

jobjectArray args;

char classpath[1024];

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

vm_args.version = 0x00010001;

res = JNI_GetDefaultJavaVMInitArgs(&vm_args);

if(res < 0)

{

printf("VMInitArgs failed\n");

}

/* 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;

/* Create the Java VM */

res = JNI_CreateJavaVM(&jvm,&env,&vm_args);

if (res < 0) {

fprintf(stderr, "Can't create Java VM\n");

exit(1);

}

res = (*env)->GetVersion(env);

printf("env : %X\n", res);

test_class = (*env)->FindClass(env, "Test");

if (test_class == 0) {

fprintf(stderr, "Can't find Test class\n");

exit(1);

}

mid = (*env)->GetMethodID(env, test_class, "<init>", "()V");

test_object = (*env)->NewObject(env, test_class, mid);

printf("Test object created\n"); /* Never reaches this place...*/

args = (*env)->NewObjectArray(env, 1,

(*env)->FindClass(env, "java/lang/String"), jstr);

if (args == 0) {

fprintf(stderr, "Out of memory\n");

exit(1);

}

(*jvm)->DestroyJavaVM(jvm);

}

The process won't get started nor finished. When supplying a non-existend executable, it gives us a IOException... (which is good)

arjenm at 2007-6-29 2:31:57 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Hi, I got the same problem. Did you get any useful response? Or is there any work around?Thanks
markzeng at 2007-6-29 2:31:57 > top of Java-index,Java HotSpot Virtual Machine,Specifications...