Java中获取项目根路径和类加载路径的7种方法
引言
在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种方法的更多相关文章
- java中获取项目在tomcat目录下的路径方法
HttpServletRequest request //获取的是ROOT项目在tomcat下的路径 方法1: String path = request.getSession().getServle ...
- java中获取文件或文件夹的路径方法
获取当前类的所在工程路径; 如果不加"/" File f = new File(this.getClass().getResource("").getPath( ...
- JSF中run项目时候Tomcat8启动不了的一种方法
把另一个博客内容迁移到这 我的问题是Tomcat是可以启动的 但是run那个jsp的时候 七月 10, 2016 3:14:54 下午 org.apache.tomcat.util.digester. ...
- 怎样在Web项目中的service业务层获取项目根路劲
这里我们有两个前提 1.没有使用struts2框架.没有使用servlet,无法给service层传递request对象. 2.使用了Spring框架. 那你可能问.会有这样的情况吗?答案是有的,比方 ...
- JAVA中获取路径
内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径: 以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...
- java中获取路径中的空格处理(%20)问题
在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...
- js获取项目根路径
//js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...
- Java中获取文件路径
Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...
- 在 Target 中获取项目引用的所有依赖(dll/NuGet/Project)的路径
原文:在 Target 中获取项目引用的所有依赖(dll/NuGet/Project)的路径 在项目编译成 dll 之前,如何分析项目的所有依赖呢?可以在在项目的 Target 中去收集项目的依赖. ...
随机推荐
- C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)
General 每一个C++表达式(一个操作符和它的操作数,一个字面值,一个变量名等等)都代表着两个独立属性:类型+属性分类.在现代C++中 glvalue(泛左值) = lvalue (传统意义上的 ...
- java集合测试类等
package demo.mytest; import java.lang.ref.SoftReference;import java.lang.ref.WeakReference;import ja ...
- mongodb详细教程
转自:https://www.cnblogs.com/liruihuan/tag/MongoDB/
- Java 的访问权限
public>protected>默认(包访问权限)>private,因为protected除了可以被同一包访问,还可以被包外的子类所访问
- 【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 ...
- 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 ...
- 多线程并发情况下 重复insert问题
代码逻辑: if(数据不存在){ insert(); } 线程启动后,发现数据库表中有相同的记录 解决方案 synchronized同步代码块即加同步锁,synchronized同步代码块的功能: 当 ...
- 指定字符串 s,返回 s 所有可能的子串,每个子串必须是一个回文(指顺读和倒读都一样的字符串)
Given a string s, partition s such that every substring of the partition is a palindrome Return all ...
- Laya 屏幕适配
Laya 屏幕适配 @author ixenos 2019-03-20 21:44:52 1.最简单的方案:原比例,对照屏幕尺寸的最小比率缩放,有黑边 Laya.stage.scaleMode = S ...
- ListView虚拟模式封装
public class ListViewAH : ListViewEx { #region 虚拟模式相关操作 ///<summary> /// 前台行集合 ///</summary ...