S
shallowpool
I'm having difficulty understanding what's happening with class paths,
environment settings, and system properties when using Runtime.exec().
The following code successfully propagates the new class path to the
forked process:
String[] envParams = {"CLASSPATH=myNewPath"};
Process proc = Runtime.getRuntime().exec(myCmd, envParams);
However, the following code fails to propagate the new class path to
the forked process. I get a class not found exception for my main
class. Setting envParams to null is supposed to cause the forked
process to inherit the parent environment:
String[] envParams = null;
System.setProperty("java.class.path",
System.getProperty("java.class.path") +
";myNewPath");
Process proc = Runtime.getRuntime().exec(myCmd, envParams);
Also, even just setting the class path to only my new class path still
throws the exception:
System.setProperty("java.class.path", "myNewPath");
Meanwhile, if I just set the classpath from my development environment,
the exec() works fine as long as I pass in null for the envParams.
Ideally, I want to be able to pass in the new class path along with the
parent environment from within java. I'm trying to avoid having to set
the classpath from outside java in this case. It would be nice if
there were some sort of getEnv() method. Then at least I could iterate
the environment list and pass it all in along with my new class path
setting.
Any ideas why the example above using System.setProperty() does not
work?
environment settings, and system properties when using Runtime.exec().
The following code successfully propagates the new class path to the
forked process:
String[] envParams = {"CLASSPATH=myNewPath"};
Process proc = Runtime.getRuntime().exec(myCmd, envParams);
However, the following code fails to propagate the new class path to
the forked process. I get a class not found exception for my main
class. Setting envParams to null is supposed to cause the forked
process to inherit the parent environment:
String[] envParams = null;
System.setProperty("java.class.path",
System.getProperty("java.class.path") +
";myNewPath");
Process proc = Runtime.getRuntime().exec(myCmd, envParams);
Also, even just setting the class path to only my new class path still
throws the exception:
System.setProperty("java.class.path", "myNewPath");
Meanwhile, if I just set the classpath from my development environment,
the exec() works fine as long as I pass in null for the envParams.
Ideally, I want to be able to pass in the new class path along with the
parent environment from within java. I'm trying to avoid having to set
the classpath from outside java in this case. It would be nice if
there were some sort of getEnv() method. Then at least I could iterate
the environment list and pass it all in along with my new class path
setting.
Any ideas why the example above using System.setProperty() does not
work?