获取当前类的路径

一此不安全的做法:

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.因为文件的目录结构和文件夹本身是两回事。 

老甘注:此处的将文件夹一起打进去是在打包时,勾选Add directory entries。


 

取得类路径的安全做法:取得类本身在系统中存储文件位置,然后根据包层次一直找到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 ));
    }

}
复制代码

 结果:

E:\Workspaces\MyProject\pjoName\target\classes\com\wbtask\UrlUtil.class
E:\Workspaces\MyProject\pjoName\target\classes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值