引言

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();

代码如下:

import java.io.File;
import java.io.IOException;
import java.net.URL;
/*
*获取项目根路径的方法
*/
public class MyUrlDemo { public static void main(String[] args) {
MyUrlDemo muDemo = new MyUrlDemo();
try {
muDemo.showURL();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void showURL() throws IOException { // 第一种:获取类加载的根路径 D:\git\daotie\daotie\target\classes
File f = new File(this.getClass().getResource("/").getPath());
System.out.println("path1: "+f); // 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\git\daotie\daotie\target\classes\my
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println("path1: "+f2); // 第二种:获取项目路径 D:\git\daotie\daotie
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println("path2: "+courseFile); // 第三种: file:/D:/git/daotie/daotie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println("path3: "+xmlpath); // 第四种: D:\git\daotie\daotie
System.out.println("path4:" +System.getProperty("user.dir"));
/*
* 结果: C:\Documents and Settings\Administrator\workspace\projectName
* 获取当前工程路径
*/ // 第五种: 获取所有的类路径 包括jar包的路径
System.out.println("path5: "+System.getProperty("java.class.path").split(";")[0]);
      // 第六种:  获取项目路径  D:/git/daotie/daotie.target/classes/
System.out.println("path6: "+Thread.currentThread().getContentClassLoader().getResource("").getPath()); //第七种 表示到项目的根目录下, 要是想到目录下的子文件夹,修改"/"即可
String path7 = request.getSession().getServletContext().getRealPath("/"));
System.out.pringln("path7: "+path7);
} }

 结果:

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. shell脚本,awk 匹配的做修改后打印,不匹配的打印。

    文件file内容如下a 1a 2b 3b 4 b 5c 6c 7 要求:第一列匹配b时,如果第二列大于3,那么将第二列加上1后打印,其余的原封不动打印.结果如下: a 1a 2b 3b 5 b 6c ...

  2. Lucene入门基础教程

    http://www.linuxidc.com/Linux/2014-06/102856.htm

  3. 关于“xx.xx已被OS X使用,无法打开”的问题

    Mac电脑上的文件拷贝到移动硬盘,有时候会出现“xx.xx已被OS X使用,无法打开”的问题. 解决办法: 1.打开终端(或在Mac搜索里面输入Terminal); 2.在终端里输入 xattr -l ...

  4. 简单jQuery图片自适应宽度插件jQuery.imgAutoSize.js

    (function ($) {       var loadImg = function (url, fn) {         var img = new Image();         img. ...

  5. memory bist lib

    model NVIC_REG6T_1024x32(resetb,margin,clk,en,we,addr,d,q) ( bist_definition( clock clk high; chip_e ...

  6. verilog random使用

    “$random函数调用时返回一个32位的随机数,它是一个带符号的整形数...”,并给出了一个例子: _________________________________________________ ...

  7. Day13有参装饰器,三元表达式,匿名函数

    多个装饰器: 加载顺序:由下而上 执行顺序:由上而下 有参装饰器: 闭包,给函数传参的一种方法 当装饰器内需要参数时,可以采用闭包形式给其传参,第三层函数接收完参数时,就变为无参装饰器 三元表达式: ...

  8. LeetCode(102) Binary Tree Level Order Traversal

    题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...

  9. ctci(1)

    // // main.cpp // proj1 // // Created by Yuxin Kang on 8/24/14. // Copyright (c) 2014 Yuxin Kang. Al ...

  10. java 词频统计代码

    package hello; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.F ...