引言

web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法。

(1):this.getClass().getResource("/");

(2):file.getCanonicalPath();

(3):this.getClass().getClassLoader();

(4):System.getProperty("user.dir");

(5):System.getProperty("java.class.path");

(6):Thread.currentThread().getContentClassLoader();

(7):request.getSession().getServletContext();

代码如下:

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.net.URL;
  4. /*
    *获取项目根路径的方法
    */
  5. public class MyUrlDemo {
  6.  
  7. public static void main(String[] args) {
  8. MyUrlDemo muDemo = new MyUrlDemo();
  9. try {
  10. muDemo.showURL();
  11. } catch (IOException e) {
  12. // TODO Auto-generated catch block
  13. e.printStackTrace();
  14. }
  15. }
  16.  
  17. public void showURL() throws IOException {
  18.  
  19. // 第一种:获取类加载的根路径 D:\git\daotie\daotie\target\classes
  20. File f = new File(this.getClass().getResource("/").getPath());
  21. System.out.println("path1: "+f);
  22.  
  23. // 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\git\daotie\daotie\target\classes\my
  24. File f2 = new File(this.getClass().getResource("").getPath());
  25. System.out.println("path1: "+f2);
  26.  
  27. // 第二种:获取项目路径 D:\git\daotie\daotie
  28. File directory = new File("");// 参数为空
  29. String courseFile = directory.getCanonicalPath();
  30. System.out.println("path2: "+courseFile);
  31.  
  32. // 第三种: file:/D:/git/daotie/daotie/target/classes/
  33. URL xmlpath = this.getClass().getClassLoader().getResource("");
  34. System.out.println("path3: "+xmlpath);
  35.  
  36. // 第四种: D:\git\daotie\daotie
  37. System.out.println("path4:" +System.getProperty("user.dir"));
  38. /*
  39. * 结果: C:\Documents and Settings\Administrator\workspace\projectName
  40. * 获取当前工程路径
  41. */
  42.  
  43. // 第五种: 获取所有的类路径 包括jar包的路径
  44. System.out.println("path5: "+System.getProperty("java.class.path").split(";")[0]);
  1.       // 第六种: 获取项目路径 D:/git/daotie/daotie.target/classes/
  2. System.out.println("path6: "+Thread.currentThread().getContentClassLoader().getResource("").getPath());
  3.  
  4. //第七种 表示到项目的根目录下, 要是想到目录下的子文件夹,修改"/"即可
    String path7 = request.getSession().getServletContext().getRealPath("/"));
    System.out.pringln("path7: "+path7);
  1. } }

 结果:

Java中获取项目根路径和类加载路径的7种方法的更多相关文章

  1. java中获取项目在tomcat目录下的路径方法

    HttpServletRequest request //获取的是ROOT项目在tomcat下的路径 方法1: String path = request.getSession().getServle ...

  2. java中获取文件或文件夹的路径方法

    获取当前类的所在工程路径; 如果不加"/" File f = new File(this.getClass().getResource("").getPath( ...

  3. JSF中run项目时候Tomcat8启动不了的一种方法

    把另一个博客内容迁移到这 我的问题是Tomcat是可以启动的 但是run那个jsp的时候 七月 10, 2016 3:14:54 下午 org.apache.tomcat.util.digester. ...

  4. 怎样在Web项目中的service业务层获取项目根路劲

    这里我们有两个前提 1.没有使用struts2框架.没有使用servlet,无法给service层传递request对象. 2.使用了Spring框架. 那你可能问.会有这样的情况吗?答案是有的,比方 ...

  5. JAVA中获取路径

    内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...

  6. java中获取路径中的空格处理(%20)问题

    在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...

  7. js获取项目根路径

    //js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...

  8. Java中获取文件路径

    Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...

  9. 在 Target 中获取项目引用的所有依赖(dll/NuGet/Project)的路径

    原文:在 Target 中获取项目引用的所有依赖(dll/NuGet/Project)的路径 在项目编译成 dll 之前,如何分析项目的所有依赖呢?可以在在项目的 Target 中去收集项目的依赖. ...

随机推荐

  1. C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)

    General 每一个C++表达式(一个操作符和它的操作数,一个字面值,一个变量名等等)都代表着两个独立属性:类型+属性分类.在现代C++中 glvalue(泛左值) = lvalue (传统意义上的 ...

  2. java集合测试类等

    package demo.mytest; import java.lang.ref.SoftReference;import java.lang.ref.WeakReference;import ja ...

  3. mongodb详细教程

    转自:https://www.cnblogs.com/liruihuan/tag/MongoDB/

  4. Java 的访问权限

    public>protected>默认(包访问权限)>private,因为protected除了可以被同一包访问,还可以被包外的子类所访问

  5. 【bug】 1118 Row size too large

    1118 Row size too large Every table (regardless of storage engine) has a maximum row size of 65,535 ...

  6. wordcloud的安装报错 error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows SDK 7.1"解决办法

    cmd中使用pip install wordcloud失败,没看懂报错的原因…… 而在pycharm中添加也报错 解决方法: 1. 下载wordcloud-1.4.1.tar.gz,解压缩 cmd c ...

  7. 多线程并发情况下 重复insert问题

    代码逻辑: if(数据不存在){ insert(); } 线程启动后,发现数据库表中有相同的记录 解决方案 synchronized同步代码块即加同步锁,synchronized同步代码块的功能: 当 ...

  8. 指定字符串 s,返回 s 所有可能的子串,每个子串必须是一个回文(指顺读和倒读都一样的字符串)

    Given a string s, partition s such that every substring of the partition is a palindrome Return all ...

  9. Laya 屏幕适配

    Laya 屏幕适配 @author ixenos 2019-03-20 21:44:52 1.最简单的方案:原比例,对照屏幕尺寸的最小比率缩放,有黑边 Laya.stage.scaleMode = S ...

  10. ListView虚拟模式封装

    public class ListViewAH : ListViewEx { #region 虚拟模式相关操作 ///<summary> /// 前台行集合 ///</summary ...