The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Re: Executing multiple DOS commands in Java

Posted by Brian Sanders on October 09, 2001 at 4:35 PM

I can excute on set of DOS command in Java but i need to set a Classpath before executing the final command. Please let me whether its possible. If yes, what is the mode of it.

In general, you set environment variables (like the classpath) using the optional second argument on Runtime.exec. The following example adds two jar files to the current classpath, then starts a new JVM:

String args = "CLASSPATH=" + System.getProperty("java.class.path") + File.pathSeparator + "foo.jar" + File.pathSeparator + "bar.jar";
Process p = Runtime.getRuntime().exec("java FooClass", new String[] { args } );

If you absolutely need to execute the two commands in the same process, you could use a batch file.

With some more info on your particular application, maybe we could come up with something better tailored to your purpose.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us