public class Test {
public static void main(String[] args) {
System.out.println(
Thread.currentThread().getContextClassLoader().getResource(""));
System.out.println(Test.class.getClassLoader().getResource(""));
System.out.println(ClassLoader.getSystemResource(""));
System.out.println(Test.class.getResource(""));
System.out.println(Test.class.getResource("/"));
System.out.println(new File("").getAbsolutePath());
System.out.println(System.getProperty("user.dir")); //尽量不要使用
}
运行结果:
file:/D:/Workspace/Java_Begin/GetPath/bin/
file:/D:/Workspace/Java_Begin/GetPath/bin/
file:/D:/Workspace/Java_Begin/GetPath/bin/
file:/D:/Workspace/Java_Begin/GetPath/bin/
file:/D:/Workspace/Java_Begin/GetPath/bin/
D:\Workspace\Java_Begin\GetPath
D:\Workspace\Java_Begin\GetPath
我的项目名是GetPath,源代码的存放路径是:D:\Workspace\Java_Begin。