zoukankan      html  css  js  c++  java
  • Java 获得Class的绝对路径方法

      Java获得class文件的绝对路径:1.e.g. Foo.class => Foo.class.getResuorce("").getFile();

      该方法在eclipse中或未jar打包时,可以得到,但如果打包的话,会显示Null. (security domain问题);

     2.Foo.clas.getProtectionDomain().getCodeSource().getLocation().getFile()来访问jar中Class文件;

    import java.net.URL;
    import java.security.CodeSource;
    import java.security.ProtectionDomain;
    
    import org.objectweb.asm.ClassWriter;
    
    public class Protection {
        
        public static void main(String args[]) {
            
            String absolutePath = ClassWriter.class.getResource("").getFile();
            /*ProtectionDomain domain = ClassWriter.class.getProtectionDomain();
            if (domain == null)
                return;
            CodeSource codeSource = domain.getCodeSource();
            if (codeSource == null)
                return;
            URL location  = codeSource.getLocation();
            if(location == null)
                return;
            String codeLocation = location.getFile();
            System.out.println(codeLocation);*/
            System.out.println(absolutePath);
        }
    
    }
    Exception in thread "main" java.lang.NullPointerException
        at com.sunchao.asm.Protection.main(Protection.java:13)
    /F:/51CTO%e4%b8%8b%e8%bd%bd-asm-all-3.0.jar
  • 相关阅读:
    Docker 部署项目
    Python+Pywinauto+Lackey 实现PC端.exe 自动化测试
    03_Fiddler 导出jmx文件
    02_Postman 中文汉化版
    07_Linux系统(Centos)安装tomcat和部署Web项目
    05_oracel题集
    02_appium基本使用
    01_appium的安装
    02_Monkey使用
    01_Monkey安装
  • 原文地址:https://www.cnblogs.com/onlysun/p/4539455.html
Copyright © 2011-2022 走看看