引言

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. java socket domain name 使用域名.

    java 的 socket 依赖了 nameService.  引擎模式. 使得 socket tcp 层 具有了上层业务的能力 (应用层) Socket socket=new Socket(&quo ...

  2. Broadcast BCM94322 用ubuntu修改ID

    1.按这个教程的6楼做的http://bbs.pcbeta.com/viewthread-1324168-1-1.html.注意我先下载 的是ubuntu9.05版本,做U盘启动进live 模式,43 ...

  3. 初涉倍增&&LCA【在更】

    一种特殊的枚举算法 什么是倍增 顾名思义,即每一次翻倍增加.那么,这样我们就有了一种$O(logn)$阶的方法处理枚举方面的问题了. 参考:[白话系列]倍增算法 一些题目 [倍增]luoguP1613 ...

  4. C++简单年月日的功能实现

    // C++年月日判断初步代码 #include <iostream> using namespace std; class Data { int year; int month; int ...

  5. linux中的硬盘及flash操作

    磁盘操作是块设备的必备操作,需要认真掌握. 一.硬盘 1.硬盘文件 默认串口硬盘的设备文件为sda(第一块硬盘).sdb(第二块硬盘).... 默认并口硬盘的设备文件为hda(第一块硬盘).hdb(第 ...

  6. Lex与Yacc学习(六)之lex & yacc (简单计算器程序) 运行

    词法分析程序ch3-01.l %{ #include "ch3-01.tab.h" extern int yylval; %} %% [0-9]+ { yylval = atoi( ...

  7. 关于JPA的理解

    JPA全称 Java Persistence API.JPA通过JDK5.0注解或者XML描述对象和关系表的映射关系,并将运行期的实体对象持久化到数据库中.持久化:即把数据(如内存中的对象)保存到可永 ...

  8. List<T> List<?> 区别用法

    List<T>是泛型方法,List<?>是限制通配符 List<T>一般有两种用途:1.定义一个通用的泛型方法.伪代码: public interface Dao{ ...

  9. NYOJ 219 An problem about date

    An problem about date 时间限制:2000 ms  |  内存限制:65535 KB 难度:2   描述 acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你 ...

  10. Leetcode 413.等差数列划分

    等差数列划分 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为等差数列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 ...