public class ResourceTest extends TimerTask{
@Override
public void run() {
System.out.println("开始测试ResourceTest------------------");
String s1 = this.getClass().getResource("").getPath();
String s2 = this.getClass().getResource("/").getPath();
String s3 = this.getClass().getResource("/com").getPath();
String s4 = this.getClass().getResource("/com/annotation").getPath();
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
public static void main(String[] args) {
new ResourceTest().run();
}
}
打印的内容:
开始测试ResourceTest------------------
/F:/Workspaces/MyEclipse%20Professional%202014/AnnotationSSH/WebRoot/WEB-INF/classes/com/annotation/action/
/F:/Workspaces/MyEclipse%20Professional%202014/AnnotationSSH/WebRoot/WEB-INF/classes/
/F:/Workspaces/MyEclipse%20Professional%202014/AnnotationSSH/WebRoot/WEB-INF/classes/com
/F:/Workspaces/MyEclipse%20Professional%202014/AnnotationSSH/WebRoot/WEB-INF/classes/com/annotation
ps:URL对空格,特殊字符(%,#,[]等)和中文进行了编码处理,所以空格变为%20