Java中获得程序当前路径的4中方法

时间:2023-03-09 04:53:59
Java中获得程序当前路径的4中方法

Java中获得程序当前路径的4中方法:
在Application中:

import java.util.*;

public class TestUserDir {

    public static void main(String[] args) {
Properties property = System.getProperties();
String str = property.getProperty("user.dir");
System.out.println(str); } }

或者:

import java.util.*;

public class getPath {
public static void main(String[] args) {
String s = System.getProperty("java.class.path");
System.out.println(s);
}
}

输出:F:/code/ec/JavaDBStudy

在Servlet中:

ServletContext servletContext = config.getServletContext();
String rootPath = servletContext.getRealPath("/");

相关文章