Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to get Present working Directory(Exact path) in java..?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry How to get Present working Directory(Exact path) in java..?

    Explanation
    I have a project in java & which will be built as a jar after completion. The tree structure of the project is as follows.

    /home/software/Documents/ProjectHome
    │── res
    │** └── ProjectSetupConfiguration.xml
    ├── com
    │** └── peek
    │** └── svn
    │** ├── SVNModel.class
    │** ├── UpdateVersion.class

    Here the two classes I build as a jar called "updater.jar" and I copied in /home/software/Documents/ProjectHome

    Now the problem comes as..

    /home/software/Documents/ProjectHome ]# ll
    -rw-rw-r--. 1 software software 12272033 Nov 2 14:55 updater.jar
    drwxrwxr-x. 3 software software 4096 Nov 2 13:47 res

    /home/software/Documents/ProjectHome ]# java -jar updater.jar
    Output
    ======
    It's working fine.

    /home/software ]# java -jar updater.jar
    canonical : /home/software
    absolute : /home/software.
    path : .
    executionPath : /home/software
    executionPath : /home/software
    java.io.FileNotFoundException: /home/software/res/ProjectSetupConfiguration.xml (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:97)

    I tried:
    System.out.println("canonical : "+new File(".").getCanonicalPath().trim().toString());
    System.out.println("absolute : "+new File(".").getAbsolutePath().trim().toString());
    System.out.println("path : "+new File(".").getPath().trim().toString());
    String executionPath = System.getProperty("user.dir");
    System.out.println("executionPath : "+executionPath);
    and even i tried the command "pwd" and executed using ProcessBuilder().....!!!

    Is there any solution for me..
    Last edited by smilyface; February 8th, 2012 at 08:05 AM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to get Present working Directory(Exact path) in java..?

    You can't (for most sane values of "can't") modify files that are bundled in your .jar. What you can do is to guess an appropriate place to save a config file, look to see if it's already there and if it isn't, copy a default config file out of your .jar and use that in future (or replace it with your bundled default if it goes missing again). Guessing an appropriate place to save a config file is not a trivial job if you want to write Java that delivers the "write once run anywhere" promise. If all you want to do is to save a few settings, don't use a file: use java.util.prefs.Preferences

Similar Threads

  1. How to convert local path drive to UNC path
    By krisswift in forum Object Oriented Programming
    Replies: 2
    Last Post: November 17th, 2011, 07:40 AM
  2. Wrong current working directory
    By tux008 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 11th, 2011, 11:25 AM
  3. Relative path issue with Context path struts
    By chinnu in forum Web Frameworks
    Replies: 1
    Last Post: March 31st, 2011, 10:17 AM
  4. Program to print current directory path to the console
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 12:59 PM
  5. Program to print current directory path to the console
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 12:59 PM

Tags for this Thread