java项目路径问题
2017-09-05 08:33
302 查看
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(f);
// 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\git\daotie\daotie\target\classes\my
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);
// 第二种:获取项目路径 D:\git\daotie\daotie
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
// 第三种: file:/D:/git/daotie/daotie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println(xmlpath);
// 第四种: D:\git\daotie\daotie
System.out.println(System.getProperty("user.dir"));
/*
* 结果: C:\Documents and Settings\Administrator\workspace\projectName
* 获取当前工程路径
*/
// 第五种: 获取所有的类路径 包括jar包的路径
System.out.println(System.getProperty("java.class.path"));
}
}
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(f);
// 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\git\daotie\daotie\target\classes\my
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);
// 第二种:获取项目路径 D:\git\daotie\daotie
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
// 第三种: file:/D:/git/daotie/daotie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println(xmlpath);
// 第四种: D:\git\daotie\daotie
System.out.println(System.getProperty("user.dir"));
/*
* 结果: C:\Documents and Settings\Administrator\workspace\projectName
* 获取当前工程路径
*/
// 第五种: 获取所有的类路径 包括jar包的路径
System.out.println(System.getProperty("java.class.path"));
}
}
相关文章推荐
- Java Web项目中路径问题的解决方案
- Myeclipse更新整个项目后,有时java或jsp文件,会提示错误,找不到类路径等问题。
- java、JSP web项目中的路径问题
- JavaWeb 项目中的绝对路径和相对路径以及问题的解决方案
- 非web工程java项目中的路径问题(针对以jar包运行的工程)
- 关于java和web项目中的相对路径问题
- JAVA项目下的路径问题
- JAVA在向tomcat部署项目的时候,部署项目找不到路径,在对于款式和项目分离的情况下加载不到款式的路径问题
- java项目文件的路径问题
- java取得项目工程---获取文件的相对路径问题---getResource()和getResourceAsStream以及路径问题
- java 项目中的文件路径问题(待续)
- 关于java和web项目中的相对路径问题
- Java项目下exe文件的路径问题及运行
- 关于java项目路径问题
- 关于java和web项目中的相对路径问题
- Java windows项目迁移到linux上,遇到一些跟路径相关的问题
- 关于Myclipse中java项目新建Source Folder中配置文件路径的问题
- java 项目路径问题(二)
- 关于java和web项目中的相对路径问题
- maven项目java包名的路径问题