我正在创建一个Java应用程序,使用log4j进行日志记录。我已经给出了log4j配置文件的绝对路径,以及生成的日志文件的绝对路径(这些日志文件的位置)。我可以在运行时通过以下方式获得Java web应用程序的绝对路径:
String prefix = getServletContext().getRealPath("/");
但在普通Java应用程序的上下文中,我们可以使用什么?
试试;
String path = new File(".").getCanonicalPath();
我不清楚你的问题是什么。如果getServletContext().getRealPath()
不是答案,那么“关于我们正在使用的Web应用程序”是什么意思,不清楚。
System.getProperty("user.dir")
给出。System.getProperty("user.home")
给出。this.getClass().getProtectionDomain().getCodeSource().getLocation()
。user.home
是用户的主目录。 - Jonathan那么使用this.getClass().getProtectionDomain().getCodeSource().getLocation()
呢?
由于JAR
应用程序路径和在IDE
中运行的应用程序路径不同,因此我编写了以下代码来始终返回正确的当前目录:
import java.io.File;
import java.net.URISyntaxException;
public class ProgramDirectoryUtilities
{
private static String getJarName()
{
return new File(ProgramDirectoryUtilities.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath())
.getName();
}
private static boolean runningFromJAR()
{
String jarName = getJarName();
return jarName.contains(".jar");
}
public static String getProgramDirectory()
{
if (runningFromJAR())
{
return getCurrentJARDirectory();
} else
{
return getCurrentProjectDirectory();
}
}
private static String getCurrentProjectDirectory()
{
return new File("").getAbsolutePath();
}
private static String getCurrentJARDirectory()
{
try
{
return new File(ProgramDirectoryUtilities.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent();
} catch (URISyntaxException exception)
{
exception.printStackTrace();
}
return null;
}
}
只需调用getProgramDirectory()
函数,无论哪种方式,您都应该能够得到良好的结果。
如果您在谈论一个Web应用程序,您应该使用ServletContext
对象的getRealPath
方法。
示例:
public class MyServlet extends Servlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String webAppPath = getServletContext().getRealPath("/");
}
}
File pto = new File(YourClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
pto.getAbsolutePath());
/*****************************************************************************
* return application path
* @return
*****************************************************************************/
public static String getApplcatonPath(){
CodeSource codeSource = MainApp.class.getProtectionDomain().getCodeSource();
File rootPath = null;
try {
rootPath = new File(codeSource.getLocation().toURI().getPath());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rootPath.getParentFile().getPath();
}//end of getApplcatonPath()
URISyntaxException
,此代码将抛出NullPointerException
异常。 - user207421rootPath
为空且在异常情况下永远不会改变,然后您对其进行了取消引用操作。如果您想辩论,请提供事实或论据,而不仅仅是否认。 - user207421new File(".").getAbsolutePath()
最好将文件保存在用户主目录的子目录中,而不是应用程序可能存在的任何位置。
Sun公司花费了相当大的力气来确保使用Java Web Start启动的小程序和应用程序无法确定应用程序的真实路径。这种改变破坏了许多应用程序。如果这些更改被扩展到其他应用程序,我也不会感到惊讶。
@GetMapping("/")
public String index(ModelMap m, HttpServletRequest request) {
String realPath = request.getServletContext().getRealPath("/");
System.out.println(realPath);
return "index";
}
System.getProperty("user.dir")
,它不会抛出IOException
。 - Jonathan