Setting classpath java for use in Runtime.exec

Posted by phil swenson on Stack Overflow See other posts from Stack Overflow or by phil swenson
Published on 2010-03-16T00:38:13Z Indexed on 2010/03/16 0:39 UTC
Read the original article Hit count: 491

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about classpath