[Q#17939556][A#17939617] How to get the execution directory path in java
https://stackoverflow.com/q/17939556
I have made a pure java app which tells me about number of files in a given directory. Now I set current directory by using the following code: After that I made an installer with its jar file and installed it in my windows 8 and then I add it to the windows right click drop down menu (context menu). When I launch it from the context menu it always tells me about the number of files in the directory where it is actually installed in however I want to know the number of files of that directory from where I am executing it. So plz help me. I am a novice in this field and I don't want you to confuse me in current directory and the current execution directory. So I write this so long and hoping for a clean answer in very simple words. Thanks
Answer
https://stackoverflow.com/a/17939617
As Jarrod Roberson states in his answer here: One way would be to use the system property System.getProperty("user.dir"); this will give you "The current working directory when the properties were initialized". This is probably what you want. to find out where the java command was issued, in your case in the directory with the files to process, even though the actual .jar file might reside somewhere else on the machine. Having the directory of the actual .jar file isn't that useful in most cases. The following will print out the current directory from where the command was invoked regardless where the .class or .jar file the .class file is in. if you are in /User/me/ and your .jar file containing the above code is in /opt/some/nested/dir/ the command java -jar /opt/some/nested/dir/test.jar Test will output current dir = /User/me. You should also as a bonus look at using a good object oriented command line argument parser. I highly recommend JSAP, the Java Simple Argument Parser. This would let you use System.getProperty("user.dir") and alternatively pass in something else to over-ride the behavior. A much more maintainable solution. This would make passing in the directory to process very easy to do, and be able to fall back on user.dir if nothing was passed in. Example : GetExecutionPath output for the above will be like
APIzation
public class Test
{
public static void main(final String[] args)
{
final String dir = System.getProperty("user.dir");
System.out.println("current dir = " + dir);
}
}
import java.util.*;
import java.lang.*;
public class GetExecutionPath
{
public static void main(String args[]) {
try{
String executionPath = System.getProperty("user.dir");
System.out.print("Executing at =>"+executionPath.replace("\\", "/"));
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
}
}
}
C:\javaexamples>javac GetExecutionPath.jav
C:\javaexamples>java GetExecutionPath
Executing at =>C:/javaexamples
package com.stackoverflow.api;
import java.lang.*;
import java.util.*;
public class Human17939617 {
public static String getExecutionDirPath() {
try {
String dir = System.getProperty("user.dir");
// System.out.print("Executing at =>" + dir.replace("\\", "/"));
return dir;
} catch (Exception e) {
System.out.println("Exception caught =" + e.getMessage());
}
return null;
}
}
package com.stackoverflow.api;
/**
* How to get the execution directory path in java
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/17939617">https://stackoverflow.com/a/17939617</a>
*/
public class APIzator17939617 {
public static String getPath() {
final String dir = System.getProperty("user.dir");
return dir;
}
}