My java jar is installed in c:\program files<i do not know>\i do not know\here.jar. When the user execute my application.
I always get c:\uesrs\\here.jar and its failing.
String Windows7HomeEditionProgramFileDirectroyStruct = System.getProperty("user.dir");
How can i get the correct path where it was double clicked or executed from?
Details (tested both answer but none is saying where the jar is located):
Try 1)
new File(".").getAbsolutePath();
or
new File(".").getCanonicalPath();
C:\Users\sun>java -jar "C:\Users\sun\Documents\NetBeansProjects\JavaApplication1
\dist\JavaApplication1.jar"
C:\Users\sun\.
Try 2)
ClassLoader.getSystemClassLoader().getResource(".").getPath();
C:\Users\sun>java -jar "C:\Users\sun\Documents\NetBeansProjects\JavaApplication1
\dist\JavaApplication1.jar"
Exception in thread "main" java.lang.NullPointerException
at javaapplication1.JavaApplication1.main(JavaApplication1.java:18)
Follow up:
String test = JavaApplication1.class.getProtectionDomain().getCodeSource().getLocation().getPath();
C:\>java -jar "C:\Users\sun\Documents\NetBeansProjects\JavaApplication1\dist\Jav
aApplication1.jar"
/C:/Users/sun/Documents/NetBeansProjects/JavaApplication1/dist/JavaApplication1.
jar
The “user.dir” system property will return the working directory for the JAR file. The same is true for Binyamin’s answer.
You are looking for the directory that contains the executing JAR file. A similar question was posed in this thread, the answer may help you.