tags:

views:

207

answers:

1

I am trying to spawn a process using Runtime.exec. I want to use my current classpath : System.getProperty("java.class.path")

Unfortunately, I am having all kinds of issues. When it works on my mac, it doesn't work on Windows. And doesn't work on my mac ever when there is a space in the classpath. The error I always get is ClassDefNotFound, so it's related to how I'm building and passing in the classpath.

here is some sample code:

String startClass = "com.test.MyClass"
String javaHome = System.getProperty("java.home");
String javaCmd = javaHome + "/bin/java";
String classPath = "-Djava.class.path=" + System.getProperty("java.class.path");
String[] commands = new String[]{javaCmd, classPath, startClass};
String commandString = StringUtils.join(commands, " ");
Process process = Runtime.getRuntime().exec(commandString);

So, how should I setup the classpath?

Thanks for any help

+1  A: 

You need to aim for the overload of 'exec' that takes String[], not String. And you have use the correct path separator from the File class, so that you have colons on Linux and semicolons on Windows.

bmargulies
using the array of strings does solve the prob. I ended up using ProcessBuilder directly, btw
phil swenson