Trouble with Runtime.exec() and environment settings

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?
 
H

Heiner Kücker

shallowpool schrieb
I'm having difficulty understanding what's happening with class paths,
environment settings, and system properties when using Runtime.exec().
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:

If Java 5 is an useful option, try the new ProcessBuilder class.
 
G

Gordon Beaton

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);

Java system properties are *not* environment variables, and aren't
inherited by child processes.

If myCmd is "java ...", why don't you just append a -cp option to the
command line itself?

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top