The thing is that System.getProperty("user.dir");
obtains a path without a current folder. So if class is stored in C:\Users\Nika\workspace\sint\src
the output is C:\Users\Nika\workspace\sint
. What should I do?
4 に答える
I think this will help you if you want the current working directory:
The source directory for a .java
is not stored anywhere. You can obtain the based directory of the location of the .class
file by getting the protection domain, however this is usally not needed.
Perhaps what you are trying to do is get a file using the class path. In this case I would suggest using ClassLoader.getResourceAsInputStream()
Because class loader has current path to it. If yoy create jar then src path would not be valid. so append package name if classes are not in archive
Why not use getResource
URL url = Test.class.getClassLoader().getResource(Test.class.getName().concat(".class"));
System.out.println(url.getPath());