package com.lu.test; import java.io.IOException; public class CurrentPath { public static void main(String[] args) throws IOException { //得到classpath的绝对路径 System.out.println(CurrentPath.class.getClassLoader().getResource("") .getPath()); //得到当前类所在的绝对路径 System.out.println(CurrentPath.class.getResource("").getPath()); //得到项目的根的绝对路径 System.out.println(System.getProperty("user.dir")); } }
假如当前java工程的根路径为:F:\Eclipse\testjava
那么输出内容为:spa
/F:/Eclipse/test/bin/
/F:/Eclipse/test/bin/com/lu/test/(com/lu/test/为该类所在的包)
F:\Eclipse\test
code