Is there any way to determine current filesystem location of executed class from the code of this class, in runtime, given that it's executed using java
command?
For example for following class:
public class MyClass{
public static void main(String[] args){\
new MyClass().doStuff();
}
private void doStufF(){
String path = ... // what to do here?!
System.out.println("Path of class being invoked: " + path);
}
}
Is there any way to get and print the correct path in filesystem of MyClass.class file just being invoked?
Similarly, if I invoke jar file from java -jar
command, is there any way to get a path of jar file from within classes inside that jar?
P.S.:Please, do not make any assumptions about working directory from which java
command is invoked!