Hello there,
Here is the deal: spaces are allowed in file names; space is also used
to separate files on both win32 and *nix platform.
The following program won't run correctly on both win32 and *nix:
class PathName {
public static void main(String[] args) {
String javafile = args[0];
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(new String[]{"javac", javafile}); //XXX
p.waitFor();
System.out.println( (p.exitValue() == 0)? "succeeded" :
"failed");
} catch (Exception e) {}
}
}
Consider file name with space in it, what should be passed to
Runtime.exec at XXX to make this code run both on win32 and *nix?
I tried putting quotation marks around the filename, replace space with
%20, neither seem to work perfectly.
Any tips?
-Shin
Andrew Thompson - 04 Nov 2005 02:33 GMT
> Here is the deal: spaces are allowed in file names; space is also used
> to separate files on both win32 and *nix platform.
Java source code files are a subset* of those files that do
not include characters which are illegal in Java class names*.
Included amongst those illegal characters is ' '.
* and other things, like 'contain Java source code'.
Shin - 04 Nov 2005 19:41 GMT
not the java source file name, but how about path components? It's an
option to call for not ever using space in file/path names in any part
of your java program, but it's not the best, seemingly.
> > Here is the deal: spaces are allowed in file names; space is also used
> > to separate files on both win32 and *nix platform.
[quoted text clipped - 4 lines]
>
> * and other things, like 'contain Java source code'.
Andrew Thompson - 04 Nov 2005 20:12 GMT
(Please refrain from top-posting)
<http://www.physci.org/codes/javafaq.jsp#netiquette>
> not the java source file name, but how about path components?
That is entirely different. See Benji's answer.
Benji - 04 Nov 2005 02:51 GMT
> <snip>
It's system-dependant, so I don't think there's a standard way of doing
it. You're really not dealing with the file name correctly if it can't
be loaded. A file name with spaces in unix has to be backspace escaped:
/home/bdg/My\ Java\ Files/Test.java
in windows, the entire path has to be quoted:
"c:\Documents and Settings\bdg\Test.java"

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
Shin - 16 Nov 2005 18:22 GMT
True observation.
My original question is if you can use the same Runtime.exec() to
compile such files on both platforms, it turns out instead of using
exec(String), use exec(String[]) and put your file names inside
quotation works fine on both platform. So, in some sense (at least) for
this problem, there is an system independent way. I would hate to see
Java not allow me to write portable code at this kind of level.
-Shin