java class路径获取

2014-11-24 03:08:04 · 作者: · 浏览: 1

1. console模式下:

String fileDir = Thread.currentThread().getContextClassLoader().getResource("./").getPath();

或者 = PropertiesFacoty.class.getClassLoader().getResource("./").getPath();

或URL fileDir = PropertiesFacoty.class.getResource(".");

在web容器中直接获取到系统的lib目录而不是项目的。

2. web容器模式下:

ServletContext context;

String path = context.getRealPath("") + "\\WEB-INF\\classes\\"; 通过ServletContext获取

摘自 liuxinbo_1984的专栏