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
  • 相关阅读:
    弹出层layer的使用
    SQL Server SQL分页查询
    C#过滤html标签
    SQLServer ForXmlPath应用
    js调用soapWebService服务
    MediaWiki使用指南
    阿里云金融云服务器配置
    VS无法启动 IISExpress web 服务器
    mysql服务突然丢失解决方案
    [k8s]通过openssl生成证书
  • 原文地址:https://www.cnblogs.com/onlysun/p/4539455.html
Copyright © 2011-2022 走看看