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:
`File f = new File(".");`
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
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 thejava
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.
public class Test { public static void main(final String[] args) { final String dir = System.getProperty("user.dir"); System.out.println("current dir = " + dir); } }
if you are in
/User/me/
and your .jar file containing the above code is in/opt/some/nested/dir/
the commandjava -jar /opt/some/nested/dir/test.jar Test
will outputcurrent 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 onuser.dir
if nothing was passed in.
Example : GetExecutionPath
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());
}
}
}
output for the above will be like
C:\javaexamples>javac GetExecutionPath.jav
C:\javaexamples>java GetExecutionPath
Executing at =>C:/javaexamples
You can do some crazy stuff:
String absolute = getClass().getProtectionDomain().getCodeSource().getLocation().toExternalForm();
absolute = absolute.substring(0, absolute.length() - 1);
absolute = absolute.substring(0, absolute.lastIndexOf("/") + 1);
String configPath = absolute + "config/file.properties";
String os = System.getProperty("os.name");
if (os.indexOf("Windows") != -1) {
configPath = configPath.replace("/", "\\\\");
if (configPath.indexOf("file:\\\\") != -1) {
configPath = configPath.replace("file:\\\\", "");
}
} else if (configPath.indexOf("file:") != -1) {
configPath = configPath.replace("file:", "");
}
I use this to read out a config file relativ to execution path. You can use it also to get execution path of your jar file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With