How to get the real path of Java application at runtime?
Try;
String path = new File(".").getCanonicalPath();
how to get java app full path in windows
The location of the .class file for the currently executing class is given by
this.getClass().getProtectionDomain().getCodeSource().getLocation();
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
How to get the path of running java program
Try this code:
final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
replace 'MyClass
' with your class containing the main method.
Alternatively you can also use
System.getProperty("java.class.path")
Above mentioned System property provides
Path used to find directories and JAR archives containing class files.
Elements of the class path are separated by a platform-specific
character specified in the path.separator property.
How can I get path java source code in runtime?
You can't do this in java, because (unlike Python) java is compiled language and source code usually not bundled in application.
How to get the path of a running JAR file?
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation()
.toURI()).getPath();
Replace "MyClass" with the name of your class.
Obviously, this will do odd things if your class was loaded from a non-file location.
Find absolute java.exe path programmatically from java code
String javaHome = System.getProperty("java.home");
Can you tell me either through pure Java ... on windows how is it possible to find out the location of javaw.exe?
E.G.
import java.io.File;
class JavawLocation {
public static void main(String[] args) {
String javaHome = System.getProperty("java.home");
File f = new File(javaHome);
f = new File(f, "bin");
f = new File(f, "javaw.exe");
System.out.println(f + " exists: " + f.exists());
}
}
Output
C:\Program Files (x86)\Java\jdk1.6.0_29\jre\bin\javaw.exe exists: true
Press any key to continue . . .
And yes, I am confident that will work in a JRE.
Find the path of file in same directory during RUNTIME
After Searching a lot I finally found a way to get application's realpath during runtime. I am posting this in case anybody needs it.
HttpServletRequest servletRequest = ServletActionContext.getRequest();
String app_path;
app_path = servletRequest.getSession().getServletContext().getRealPath("/");
and now (In My Case)
String filepath = app_path + "WEB-INF/classes/hibernate.cfg.xml";
Related Topics
How to Check for a Valid Url in Java
Java.Lang.Noclassdeffounderror: Could Not Initialize Class Xxx
How to Improve Performance of This Regular Expression Further
Why Isn't This Code Causing a Concurrentmodificationexception
Printing Even and Odd Using Two Threads in Java
How to Create a Class Literal of a Known Type: Class<List<String>>
Integer Arithmetic in Java with Char and Integer Literal
Arraylist or List Declaration in Java
Port of Random Generator from C to Java
How to Serialize a List in Java
Which Concurrent Queue Implementation Should I Use in Java
How to Specify the Root Context in Your <Web-App> Tags in Web.Xml