How to get current working directory in Java?
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.
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 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.
How to get the execution directory path in java
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
will output
/opt/some/nested/dir/test.jar Testcurrent dir =
.
/User/meYou 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
Change the Current Working Directory in Java
I'm pretty sure you can't modify the current process' working directory. Instead you can use the File(String, String)
constructor which creates a new File
instance from a parent pathname string and a child pathname string.
Not getting previous path name 2 folder back from the current directory in java
You may want to use a Path, then call its parents the number of times you need :
System.out.println(Paths.get(currentpath).getParent().getParent().getParent());
How to get JUST the current working directory (not the full path) in java
@Lorenzo, this works for me. Let me know if any questions.
import org.apache.commons.lang3.StringUtils;
public class Test {
public static void main(String[] args) {
String input ="C:\\Users\\user\\Desktop\\folder";
String[] output = StringUtils.split(input, "\\");
System.out.println(output[output.length-1]);
}
}
Changing the current working directory in Java?
There is no reliable way to do this in pure Java. Setting the user.dir
property via System.setProperty()
or java -Duser.dir=...
does seem to affect subsequent creations of Files
, but not e.g. FileOutputStreams
.
The File(String parent, String child)
constructor can help if you build up your directory path separately from your file path, allowing easier swapping.
An alternative is to set up a script to run Java from a different directory, or use JNI native code as suggested below.
The relevant OpenJDK bug was closed in 2008 as "will not fix".
Related Topics
Mocking Static Methods With Mockito
How to Really Read Text File from Classpath in Java
Method Overloading For Null Argument
Why Is Using a Wild Card With a Java Import Statement Bad
How to Best Position Swing Guis
Creating a Custom Button in Java With Jbutton
Why Is It Bad Practice to Call System.Gc()
When Would You Use the Builder Pattern
How to Create a Memory Leak in Java
Calling Non-Static Method in Static Method in Java
Why Does Gson Fromjson Throw a Jsonsyntaxexception: Expected Begin_Object But Was Begin_Array
How to Get Stock Quotes Using Google Finance API
How to Return a Documentsnapshot as a Result of a Method
Xss Prevention in Jsp/Servlet Web Application
Error Java.Lang.Outofmemoryerror: Gc Overhead Limit Exceeded