Java中获得程序当前路径的4中方法:
在Application中:
import java.util.*;
public class TestUserDir {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties property = System.getProperties();
String str = property.getProperty("user.dir");
System.out.println(str);
}
}
在Application中:
import java.util.*;
public class TestUserDir {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties property = System.getProperties();
String str = property.getProperty("user.dir");
System.out.println(str);
}
}
或者:
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("/");