java获取项目路径

import java.io.File;

/**
 * new File("..\path\abc.txt") 中的三个方法获取路径的方法 <br>
 * 1: getPath() 获取相对路径,例如 ..\path\abc.txt <br>
 * 2: getAbslutlyPath() 获取绝对路径,但可能包含 ".." 或 "." 字符,例如D:\otherPath\..\path\abc.txt <br>
 * 3: getCanonicalPath() 获取绝对路径,但不包含 ".." 或 "." 字符,例如 D:\path\abc.txt
 */
public class PathUtil {

    private static String webRootPath;
    private static String rootClassPath;

    @SuppressWarnings("rawtypes")
    public static String getPath(Class clazz) {
        String path = clazz.getResource("").getPath();
        return new File(path).getAbsolutePath();
    }

    public static String getPath(Object object) {
        String path = object.getClass().getResource("").getPath();
        return new File(path).getAbsolutePath();
    }

    public static String getRootClassPath() {
        if (rootClassPath == null) {
            try {
                String path = PathUtil.class.getClassLoader().getResource("").toURI().getPath();
                rootClassPath = new File(path).getAbsolutePath();
            } catch (Exception e) {
                String path = PathUtil.class.getClassLoader().getResource("").getPath();
                rootClassPath = new File(path).getAbsolutePath();
            }
        }
        return rootClassPath;
    }

    public static String getPackagePath(Object object) {
        Package p = object.getClass().getPackage();
        return p != null ? p.getName().replaceAll("\\.", "/") : "";
    }

    public static File getFileFromJar(String file) {
        throw new RuntimeException("Not finish. Do not use this method.");
    }

    public static String getWebRootPath() {
        if (webRootPath == null)
            webRootPath = detectWebRootPath();
        return webRootPath;
    }

    public static void setWebRootPath(String webRootPath) {
        if (webRootPath == null)
            return;
        if (webRootPath.endsWith(File.separator))
            webRootPath = webRootPath.substring(0, webRootPath.length() - 1);
        PathUtil.webRootPath = webRootPath;
    }

    private static String detectWebRootPath() {
        try {
            String path = PathUtil.class.getResource("/").toURI().getPath();
            return new File(path).getParentFile().getParentFile().getCanonicalPath();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
时间: 2024-12-10 12:31:58

java获取项目路径的相关文章

java获取项目路径,url路径

我的web项目名iamgeModel. 工作空间在D盘 先获取url相关: 需要是HttpServletRequest request; 获取IP: request.getServerName() //服务器地址 获取端口: request.getServerPort() //端口号 获取项目名(out:表示结果): request.getContextPath() // out:/imageModel 获取请求完整url路径: request.getRequestURI();// out: /

Java获取项目路径下的方法(全)

平时写程序的时候,很多时候提示文件找不到,而抛出了异常,现在整理如下 一 相对路径的获得 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) String relativelyPath=System.getProperty("user.dir"); 上述相对路径中,java项目中的文件是相对于项目的根目录 web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于 tomcat安装目录\bin) 二 类加载目录的获得(

java 获取类路径

package com.jason.test; 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) { // T

C#应用程序获取项目路径的方法总结

一.非Web程序 //基目录,由程序集冲突解决程序用来探测程序集 1.AppDomain.CurrentDomain.BaseDirectory //当前工作目录的完全限定路径2.Environment.CurrentDirectory //当前应用程序的 /bin 目录的路径3.HttpRuntime.BinDirectory 二.Web程序 假设Web项目根目录为:"D:\TestPart\WebDirectoryTest". 1.HttpContext.Current //返回与

katalon系列十四:执行Windows命令&amp;获取项目路径

Katalon Studio中也可以运行Windows命令执行一些系统操作. 根据官方文档,在test case中输入命令:cmd = 'del E:\\shot\\*.xlsx E:\\shot\\*.zip'Runtime.getRuntime().exec(cmd) 运行报错 网上搜到解决方案,修改cmd如下cmd = 'cmd.exe /c del E:\\shot\\*.xlsx E:\\shot\\*.zip' 运行成功 除了直接运行cmd命令,也可以执行.bat文件,代码示例如下

java web项目获取项目路径

1.方法一 调试时只能获取eclipse 项目未编译前的路径 不太好用 /* private static Logger logger = Logger.getLogger(BookController.class); */ @RequestMapping("/index") public String bookHandle(HttpServletRequest servlet) { JSONObject json = JsonResourceUtils.getJsonObjFromR

获取项目路径的几种方法

Java获取当前项目路径: object.class.getResource()方法获得当前生成的class的绝对路径(此方法在jar包中无效,因为他获得的是生成的class的路径,返回的内容最后包含/) public String getCurrentPath(){ //取得根目录路径 String rootPath=getClass().getResource("/").getFile().toString(); //当前目录路径 String currentPath1=getCl

jsp及servlet中获取项目路径的一些方法

获取项目的路径:1.在实现了servlet接口的实现类中:根据config 调用方法,config.getServletContext().getContextPath(); 2.在一个直接创建的servlet类中(实际上是实现了httpservlet):request.getContextPath(); 3.在jsp中:由九大内置对象的request对象获取.request.getContextPath(); 4.在EL标签中${pageContext.request.contextPath}

JAVA获取服务器路径的方法

1.在JSF环境中获取到ServletContext: ServletContext sc = (ServletContext)FacesContext. getCurrentInstance().getExternalContext().getContext(); 2.servlet中获得项目绝对路径 String filePath=this.getServletConfig(). getServletContext().getRealPath("/"); 根目录所对应的绝对路径 r