naming for fun --- write portable java code

S

Shin

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
 
A

Andrew Thompson

Shin said:
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'.
 
B

Benji

Shin said:

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"
 
S

Shin

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.
 
A

Andrew Thompson

Shin wrote:

(Please refrain from top-posting)
not the java source file name, but how about path components?

That is entirely different. See Benji's answer.
 
S

Shin

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top