java -classpath ....................
won't work on the Mac??
Main Topics
Browse All TopicsDear:
I met two problem when running a process using Process class.
My Java program called Program-A, needs to run another Java program called Program-B as a sub-process. P-A needs to run P-B. My solution is to make P-A compily P-B and then runs the entry class file of P-B. When running, P-A add "bin" directory of P-B into CLASSPATH variable to guarantee that "java" command can find the entry class of P-B under its "bin" directory. Then P-A use Process class to run the command "java -classpah ..."Main" to run P-B from Main.class.
The first problem occurs when updating CLASSPATH variable. We know that the value of CLASSPATH is a combination of several directory names that are separated by ";" under Windows, or separated by ":" under MacOS, or by another character under other OS'. My program need to run under different OS'. Is there an appropriate way to obtain the separating character of CLASSPATH variable from some system variable, so I don't need to detected the OS when updating CLASSPATH every time.
The second problem is about the execution of a .class file. The entry class of P-B is Main.class. P-A runs it using java command. But the command I used under MacOS can not work under Windows. The same case happens under Windows. The command I use under MacOS is:
/* for macos. This command does not work under Windows*/
String[] cmd = {"java", "Main", "0"};
String[] env = {"CLASSPATH=" + strClsPath};
Process ps = Runtime.getRuntime().exec(
The comnand under Windows is
/* for win os. This command does not work under MacOs. */
String strCmd = "java \"-classpath\" \"" + strClsPath + "\" \"Main\" 0";
Process ps = Runtime.getRuntime().exec(
Does someone know the appropriate format of command that can be used under all OS'.
A further question is about the solution. Is there a better solution for P-A to run P-B?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
http://java.sun.com/j2se/1
Why aren't u just calling main() directly?
make sure you pass the full path to your java executable, exec() is not a shell :)
http://www.javaworld.com/j
why do you need to call them using Runtime utility?
I think its better to instantiate the class to run the main method (or any entry point).
You just need to set the CLASSPATH environment variable.
If the Program-B is some sort of a daemon that has blocking I/O, create a thread wrapper class so that you can instantiate and invoke it and still allowing Program-A to continue with other tasks.
OK, forget P-A and P-B firstly. What I want is an universal format of "Runtime.getRuntime().exec
under MacOS and Windows. As mentioned above, the command below works under MacOS but does not under Windows.
/* for macos. This command does not work under Windows*/
String[] cmd = {"java", "Main", "0"};
String[] env = {"CLASSPATH=" + strClsPath};
Process ps = Runtime.getRuntime().exec(
The code below works under Windows but does not under MacOS unless I remove the quote string for -classpath and for Main. If quote strings are remained, NoClassDefFoundException will occurs. But I can't remove quote string because path name under Windows may contain space character.
/* for win os. This command does not work under MacOs. */
String strCmd = "java \"-classpath\" \"" + strClsPath + "\" \"Main\" 0";
Process ps = Runtime.getRuntime().exec(
Business Accounts
Answer for Membership
by: CEHJPosted on 2007-04-26 at 17:01:55ID: 18986064
>>Is there an appropriate way to obtain the separating character of CLASSPATH
/6/docs/ap i/java/io/ File.html# pathSepara torChar
It's the value of http://java.sun.com/javase