-
Bug
-
Resolution: Fixed
-
P3
-
1.1, 1.1.2, 1.1.3, 1.1.5
-
1.2beta3
-
x86, sparc
-
solaris_2.4, solaris_2.6, windows_95, windows_nt
-
Not verified
Problem:
I have a java program that needs to exec another java program, and some of the args in the args[] must contain spaces (and other special chars).
Solution:
Use Runtime.exec(String[]).
This works correctly on Solaris, but not on Win32.
Apparently on Windows, it concatenates my String array and reparses, which is extremely problematic when the strings contain spaces.
Here is the test code:
import java.io.*;
public class EA1 {
public static void main(String[] args) throws Exception {
String jav = System.getProperty("java.home")+File.separator+"bin"+File.separator+"java";
String cl = "EA2";
String arg = "X Y";
String[] argsx = {jav,cl,arg};
Process p = Runtime.getRuntime().exec(argsx);
InputStream is = p.getErrorStream();
int x = is.read();
while(x!=-1){
System.err.write(x);
x = is.read();
}
p.waitFor();
}
}
public class EA2 {
public static void main(String[] args) throws Exception {
System.err.println("EA2:");
for(int i=0;i<args.length;i++){
System.err.println(i+": "+args[i]);
}
System.exit(0);
}
}
On Solaris:
% java EA1
EA2:
0: X Y
On Windows:
> java EA1
EA2:
0: X
1: Y
This is not pretty.
[fred.oliver@east]:
This is a hot issue for the JMAPI group right now. We need to invoke one java
program from another on Win32, where the default classpath contains spaces, e.g.
"c:\Program Files\...".
Since the jre command on Win32 ignores the classpath variable (I would really
like to see the justification for that), we are required to supply the
classpath as an argument on the command line.
I have a java program that needs to exec another java program, and some of the args in the args[] must contain spaces (and other special chars).
Solution:
Use Runtime.exec(String[]).
This works correctly on Solaris, but not on Win32.
Apparently on Windows, it concatenates my String array and reparses, which is extremely problematic when the strings contain spaces.
Here is the test code:
import java.io.*;
public class EA1 {
public static void main(String[] args) throws Exception {
String jav = System.getProperty("java.home")+File.separator+"bin"+File.separator+"java";
String cl = "EA2";
String arg = "X Y";
String[] argsx = {jav,cl,arg};
Process p = Runtime.getRuntime().exec(argsx);
InputStream is = p.getErrorStream();
int x = is.read();
while(x!=-1){
System.err.write(x);
x = is.read();
}
p.waitFor();
}
}
public class EA2 {
public static void main(String[] args) throws Exception {
System.err.println("EA2:");
for(int i=0;i<args.length;i++){
System.err.println(i+": "+args[i]);
}
System.exit(0);
}
}
On Solaris:
% java EA1
EA2:
0: X Y
On Windows:
> java EA1
EA2:
0: X
1: Y
This is not pretty.
[fred.oliver@east]:
This is a hot issue for the JMAPI group right now. We need to invoke one java
program from another on Win32, where the default classpath contains spaces, e.g.
"c:\Program Files\...".
Since the jre command on Win32 ignores the classpath variable (I would really
like to see the justification for that), we are required to supply the
classpath as an argument on the command line.
- duplicates
-
JDK-4077850 Executing subprocesses on NT does not quote its arguments
- Closed
- relates to
-
JDK-4109888 (process spec) Semantics of external process is not defined in JLS
- Closed