java 项目得到jar和classes路径

    public static String getJarPath(Class clazz) {
        String path = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
        try {
            path = java.net.URLDecoder.decode(path, "UTF-8");
            java.io.File jarFile = new java.io.File(path);
            return java.net.URLDecoder.decode(jarFile.getParentFile().getAbsolutePath(), "UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getClassesPath(Class clazz) {
        String path = clazz.getClassLoader().getResource("").getPath();
        try {
            return java.net.URLDecoder.decode(path, "UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

比如在AppTest.java中,

String path = getClassesPath(AppTest.class);
        print(path);

显示为:

/C:/workspace1/yourproject/target/test-classes/

最新文章

  1. 学会给你的类(及成员)来定制一套自己的Attribute吧
  2. dev GridControl 根据鼠标坐标 选中行
  3. Cannot instantiate the type AppiumDriver
  4. quick lua 3.3常用方法和学习技巧之functions.lua目录
  5. Qt之加载QSS文件
  6. (转)PK系列之六:该不该读中文翻译的专业书
  7. Hibernate——主键配置
  8. Matlab 符号运算
  9. Spring-----Spring整合Struts2实例
  10. JAVA多线程之Synchronized关键字--对象锁的特点
  11. 学习图像算法阶段性总结 (附一键修图Demo)
  12. sts 和 lombok
  13. vmware虚拟机磁盘挂载
  14. JSON 之 SuperObject(11): TSuperTableString、TSuperAvlEntry
  15. JS数组循环的性能和效率分析(for、while、forEach、map、for of)
  16. linux 之网络命令
  17. laravel 兜底路由
  18. Formal Grammars of English -10 chapter(Speech and Language Processing)
  19. React/anu实现弹出层2
  20. 『编程题全队』Alpha阶段事后诸葛亮分析

热门文章

  1. 【mybatis深度历险系列】mybatis中的输入映射和输出映射
  2. 可能是CAP理论的最好解释
  3. 2014 BDTC 参会有感
  4. 使用spark ml pipeline进行机器学习
  5. [Python]print vs sys.stdout.write
  6. Apache commons email 使用过程中遇到的问题
  7. java中&和&&的区别 位运算
  8. reactor线程阻塞引起故障
  9. 程序员高效Windows环境配置
  10. Nginx+PHP-FPM的域Socket配置方法