Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

naming for fun --- write portable java code

Thread view: 
Shin - 04 Nov 2005 02:14 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.