naming for fun --- write portable java code

Discussion in 'Java' started by Shin, Nov 4, 2005.

  1. Shin

    Shin Guest

    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
    Shin, Nov 4, 2005
    #1
    1. Advertising

  2. Shin wrote:

    > 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'.
    Andrew Thompson, Nov 4, 2005
    #2
    1. Advertising

  3. Shin

    Benji Guest

    Shin, while high on whiteboard markers, wrote:
    > <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"

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
    Benji, Nov 4, 2005
    #3
  4. Shin

    Shin Guest

    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.

    Andrew Thompson wrote:
    > Shin wrote:
    >
    > > 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, Nov 4, 2005
    #4
  5. Shin wrote:

    (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.
    Andrew Thompson, Nov 4, 2005
    #5
  6. Shin

    Shin Guest

    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
    Shin, Nov 16, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eli Bendersky
    Replies:
    1
    Views:
    1,152
    Mike Treseler
    Mar 1, 2006
  2. Andy Fish
    Replies:
    65
    Views:
    1,696
    Mabden
    May 18, 2004
  3. Harman
    Replies:
    1
    Views:
    2,489
    Moiristo
    Jul 28, 2006
  4. dolphin
    Replies:
    4
    Views:
    312
    Jorgen Grahn
    Aug 25, 2007
  5. er
    Replies:
    2
    Views:
    485
Loading...

Share This Page