如何在运行时获取Java应用程序的真实路径? [英] How to get the real path of Java application at runtime?

查看:222
本文介绍了如何在运行时获取Java应用程序的真实路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Java应用程序,我正在使用log4j。我已经给出了配置log4j文件的绝对路径以及生成的日志文件的绝对路径(生成此日志文件的位置)。我可以在运行时通过以下方式获取Java Web应用程序的绝对路径:

I am creating a Java application where I am using log4j. I have given the absolute path of configuration log4j file and also an absolute path of generated log file(where this log file are generated). I can get the absolute path of a Java web application at run time via:

String prefix =  getServletContext().getRealPath("/");

但是在普通Java应用程序的上下文中,我们可以使用什么?

but in the context of a normal Java application, what can we use?

推荐答案

尝试;

String path = new File(".").getCanonicalPath();

这篇关于如何在运行时获取Java应用程序的真实路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆