一此不安全的做法:
1. new File(path),这个方法的路径到底在那里取决于调用java命令的起始位置定义在哪里,
tomcat/bin下面的catalina.bat调用了java,所以在tomcat下相对起始位置是tomcat/bin,但是eclipse启动时,起始位置 是eclipse的项目路径。
2.类.class.getClassLoader().getResource("").getPath()
如果使用了此方法,这把决定权交给了类加载器,例如tomcat的类加载是非委托机制的,而weblogic的类加载是委托的。大部分情况下是安全的。
3.类.class.getResource("")
这是个好方法,但局限性在于如果类文件在jar中的话,那么在打jar包时需要将文件夹一起打进去,否则会返回null,jar文件实际上就是zip文件,zip文件中:文件就是文件,文件夹是文件夹,不是关联在一起的,很多开源的jar包就没有把目录打进去只打了classes文件,虽然你能年到目录层次结构,但是调用类.class.getResource("")会返回null.因为文件的目录结构和文件夹本身是两回事。
取得类路径的安全做法:取得类本身在系统中存储文件位置,然后根据包层次一直找到classpath下面:
package
com.wbtask;
import java.io.File;
import java.net.URL;
public class UrlUtil {
/**
* 取得当前类所在的文件
* @param clazz
* @return
*/
public static File getClassFile(Class clazz){
URL path = clazz.getResource(clazz.getName().substring(
clazz.getName().lastIndexOf( " . " ) + 1 ) + " .classs " );
if (path == null ){
String name = clazz.getName().replaceAll( " [.] " , " / " );
path = clazz.getResource( " / " + name + " .class " );
}
return new File(path.getFile());
}
/**
* 得到当前类的路径
* @param clazz
* @return
*/
public static String getClassFilePath(Class clazz){
try {
return java.net.URLDecoder.decode(getClassFile(clazz).getAbsolutePath(), " UTF-8 " );
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "" ;
}
}
/**
* 取得当前类所在的ClassPath目录,比如tomcat下的classes路径
* @param clazz
* @return
*/
public static File getClassPathFile(Class clazz){
File file = getClassFile(clazz);
for ( int i = 0 ,count = clazz.getName().split( " [.] " ).length; i < count; i ++ )
file = file.getParentFile();
if (file.getName().toUpperCase().endsWith( " .JAR! " )){
file = file.getParentFile();
}
return file;
}
/**
* 取得当前类所在的ClassPath路径
* @param clazz
* @return
*/
public static String getClassPath(Class clazz){
try {
return java.net.URLDecoder.decode(getClassPathFile(clazz).getAbsolutePath(), " UTF-8 " );
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "" ;
}
}
public static void main(String[] args){
System.out.println(getClassFilePath(UrlUtil. class ));
System.out.println(getClassPath(UrlUtil. class ));
}
}
import java.io.File;
import java.net.URL;
public class UrlUtil {
/**
* 取得当前类所在的文件
* @param clazz
* @return
*/
public static File getClassFile(Class clazz){
URL path = clazz.getResource(clazz.getName().substring(
clazz.getName().lastIndexOf( " . " ) + 1 ) + " .classs " );
if (path == null ){
String name = clazz.getName().replaceAll( " [.] " , " / " );
path = clazz.getResource( " / " + name + " .class " );
}
return new File(path.getFile());
}
/**
* 得到当前类的路径
* @param clazz
* @return
*/
public static String getClassFilePath(Class clazz){
try {
return java.net.URLDecoder.decode(getClassFile(clazz).getAbsolutePath(), " UTF-8 " );
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "" ;
}
}
/**
* 取得当前类所在的ClassPath目录,比如tomcat下的classes路径
* @param clazz
* @return
*/
public static File getClassPathFile(Class clazz){
File file = getClassFile(clazz);
for ( int i = 0 ,count = clazz.getName().split( " [.] " ).length; i < count; i ++ )
file = file.getParentFile();
if (file.getName().toUpperCase().endsWith( " .JAR! " )){
file = file.getParentFile();
}
return file;
}
/**
* 取得当前类所在的ClassPath路径
* @param clazz
* @return
*/
public static String getClassPath(Class clazz){
try {
return java.net.URLDecoder.decode(getClassPathFile(clazz).getAbsolutePath(), " UTF-8 " );
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "" ;
}
}
public static void main(String[] args){
System.out.println(getClassFilePath(UrlUtil. class ));
System.out.println(getClassPath(UrlUtil. class ));
}
}
结果:
E:\Workspaces\MyProject\pjoName\target\classes\com\wbtask\UrlUtil.class
E:\Workspaces\MyProject\pjoName\target\classes
E:\Workspaces\MyProject\pjoName\target\classes