zoukankan      html  css  js  c++  java
  • JAVA获取Classpath根路径的方法

    方法一:

    String path = Test.class.getResource("/").toString();

    System.out.println("path = " + path);

    此方法在tomcat下面没有问题,可以取到WEB-INF/classes

    path = file:/home/ngidm_db2/AS_Tomcat7_0_29/webapps/NGIDM/WEB-INF/classes/

    但换weblogic之后,取到的为

    path = file:/oracle/weblogic/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/

    在weblogic下面,没能拿到classes路径。

    方法二: 

    String path2 = Thread.currentThread().getContextClassLoader().getResource("/").getPath();

    System.out.println("path2 = " + path2);

    在tomcat和weblogic下面均可取到classes路径

    path2 = /oracle/weblogic/Oracle/Middleware/user_apps/NGIDM/WEB-INF/classes/

    path2 = /home/ngidm_db2/AS_Tomcat7_0_29/webapps/NGIDM/WEB-INF/classes/

    故建议大家多使用

    Thread.currentThread().getContextClassLoader().getResource("/").getPath();

    获取classpath路径,且方法2 取到的classpath不含file:前缀,可以直接使用。

  • 相关阅读:
    函数,封装
    c++增强c
    Git安装与使用
    Ajax&Jquery
    IntelliJ Idea2018.2,Pycharm20183.2破解教程
    事务&数据库连接池&DBUtils&JSP设计模式
    JSP&EL&JSTL
    Http协议和Servlet
    接收流信息---字符串
    lucene索引库优化一
  • 原文地址:https://www.cnblogs.com/aligege/p/9723936.html
Copyright © 2011-2022 走看看