Results 1 to 6 of 6

Thread: Java: access a folder shared with Windows on both Linux and Windows

  1. #1
    Join Date
    Jul 2011
    Location
    /Europe/Netherlands
    Beans
    401
    Distro
    Kubuntu 14.04 Trusty Tahr

    Java: access a folder shared with Windows on both Linux and Windows

    In my Java project I want to store data in a folder shared between Windows and Linux called "Web" (or a subfolder of that folder). The folder resides on a shared NTFS data partition. The point is that I want to be able to run the application in both systems and work on the same data. I have the same editor on both systems (Netbeans) but I don't want to store the data in the project folder or working directory of the program.

    In Windows the path to that folder is:
    Code:
    I:\Web
    In Ubuntu it's mounted as
    Code:
    /media/win8data/Web
    Those paths are different so when I run the code with the Windows path it obviously does not work. The Linux mount path would not make sense on Windows. Is it possible in Java to check which operating system is being used and define the proper root for the path?
    "I believe humans get a lot done, not because we're smart, but because we have thumbs so we can make coffee." --Flash Rosenberg

  2. #2
    Join Date
    Dec 2008
    Location
    UK
    Beans
    432
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Java: access a folder shared with Windows on both Linux and Windows

    You should be able to use System.getProperty("os.name") to retrieve the operating system it is running on and then decide how to construct the path to your shared folder
    Last edited by Azdour; August 20th, 2013 at 06:50 PM. Reason: missed out the starting bracket on getProperty

  3. #3
    Join Date
    Aug 2008
    Location
    Sweden
    Beans
    303
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Java: access a folder shared with Windows on both Linux and Windows

    Another solution could be to make use of "org.apache.commons.lang.SystemUtils" contant "IS_OS_UNIX"

    Code:
    if (SystemUtils.IS_OS_UNIX) {
        // TADA
    } else {
        // BOO
    }
    Don't mind my mild humor
    This is my signature

  4. #4
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Java: access a folder shared with Windows on both Linux and Windows

    What about an environment variable, defined with the appropriate path on each OS, then accessed via System.getenv() in your program?

    Or, similar idea, pass in a property (java -D...) or command line argument when you invoke the program?
    Last edited by r-senior; August 17th, 2013 at 09:11 PM.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Java: access a folder shared with Windows on both Linux and Windows

    Quote Originally Posted by Azdour View Post
    You should be able to use System.getProperty"os.name") to retrieve the operating system it is running on and then decide how to construct the path to your shared folder
    +1 to that, this is the standard way.

    Also use System.getProperty("file.separator") at the proper places(*), and the java.io.File methods to handle file names.


    (*) In practice Windows system calls handle forward slashes just as well as back slashes so the only place where "/" v.s. "\" is important is when handling file paths in user input or config files.

  6. #6
    Join Date
    Jul 2011
    Location
    /Europe/Netherlands
    Beans
    401
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Java: access a folder shared with Windows on both Linux and Windows

    Thanks all! I'll think I go with the System.getPropery"os.name".
    "I believe humans get a lot done, not because we're smart, but because we have thumbs so we can make coffee." --Flash Rosenberg

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •