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