路径的获取

path:
工程目录:D:\workspace\myTest;
main方法目录:D:\workspace\myTest\src\com\javaeye\Domain.java;

代码:
Class<?> clazz = Class.forName(DomainObject.class.getName())
System.setProperty("user.dir", "D:/workspace/myTest/src");
String dir = System.getProperty("user.dir");
URL url = clazz.getResource(".");
URL url1 = clazz.getResource("");
URL url2 = clazz.getResource("/");
String path = url.getPath();
String path1 = url1.getPath();
String path2 = url2.getPath();

输出
dir=D:/workspace/myTest/src,默认输出D:/workspace/myTest
path=/D:/workspace/myTest/bin/com/javaeye/;
path1=/D:/workspace/myTest/bin/com/javaeye/;
path2=/D:/workspace/myTest/bin/
forName(完整包名);

你可能感兴趣的:(路径)