如何使用java从linux环境中获取tomcat中当前目录的相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12843217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:34:53  来源:igfitidea点击:

how to get relative path of current directory in tomcat from linux environment using java

javalinuxtomcatpathrelative-path

提问by asiam9

I would like to use to read properties file from out side of the my web application. I was deployed a war file in tomcat in windows environment and I am able to read properties file from outside of my web application using following code.

我想使用从我的 Web 应用程序外部读取属性文件。我在 windows 环境中的 tomcat 中部署了一个 war 文件,我能够使用以下代码从我的 Web 应用程序外部读取属性文件。

//(Method 1.)
String filePath = 
new java.io.File( ".").getCanonicalPath()+"/webapps/strsproperties/strs.properties";
// or  
//(Method 2.)
String filePath = new File(System.getProperty("catalina.base"))+ "/webapps/strsproperties/strs.properties";

InputStream inputStream = null;
inputStream = new FileInputStream(filePath);
properties.load(inputStream);
String application = properties.getProperty("applications");

In above both case's I am able to read filePath in windows.

在以上两种情况下,我都可以在 Windows 中读取 filePath。

Problem is I am not able to read filePath in Linux using 1st method ( relative path) process.

问题是我无法使用第一种方法(相对路径)过程在 Linux 中读取 filePath。

Is there any way to read file using relative path in tomcat from Linux env ?.

有什么方法可以从 Linux env 中使用 tomcat 中的相对路径读取文件吗?

采纳答案by Aaron Digulla

Your problem is that you don't know what the "current" directory is. If you start Tomcat on Linux as a service, the current directory could be anything. So new File(".")will get you a random place in the file system.

您的问题是您不知道“当前”目录是什么。如果您在 Linux 上将 Tomcat 作为服务启动,则当前目录可以是任何内容。所以new File(".")会给你在文件系统中的一个随机位置。

Using the system property catalina.baseis much better because Tomcat's start script catalina.shwill set this property. So this will work as long as you don't try to run your app on a different server.

使用系统属性catalina.base要好得多,因为 Tomcat 的启动脚本catalina.sh会设置此属性。所以只要你不尝试在不同的服务器上运行你的应用程序,这就会起作用。

Good code would look like this:

好的代码应该是这样的:

File catalinaBase = new File( System.getProperty( "catalina.base" ) ).getAbsoluteFile();
File propertyFile = new File( catalinaBase, "webapps/strsproperties/strs.properties" );

InputStream inputStream = new FileInputStream( propertyFile );

As you can see, the code doesn't mix strings and files. A file is a file and a string is just text. Avoid using text for code.

如您所见,代码不会混合字符串和文件。文件就是文件,字符串就是文本。避免在代码中使用文本。

Next, I'm using getAbsoluteFile()to make sure I get a useful path in exceptions.

接下来,我要getAbsoluteFile()确保在异常中获得有用的路径。

Also make sure your code doesn't swallow exceptions. If you could have seen the error message in your code, you would have seen instantly that the code tried to look in the wrong place.

还要确保您的代码不会吞下异常。如果您能在代码中看到错误消息,您就会立即看到代码试图在错误的地方查找。

Lastly, this approach is brittle. Your app breaks if the path changes, when a different web server is used and for many other cases.

最后,这种方法很脆弱。如果路径发生变化、使用不同的 Web 服务器以及许多其他情况,您的应用程序就会中断。

Consider extending the webapp strspropertiesto accept HTTP requests. That way, you could configure your app to connect to strspropertiesvia an URL. This would work for any web server, you could even put the properties on a different host.

考虑扩展 webappstrsproperties以接受 HTTP 请求。这样,您就可以将您的应用程序配置为strsproperties通过 URL连接。这适用于任何 Web 服务器,您甚至可以将属性放在不同的主机上。

回答by David Levesque

If your code runs inside a servlet, it may be simpler to use getServletContext().getRealPath("/strs.properties")to retrieve the absolute file path.

如果您的代码在 servlet 内运行,则使用getServletContext().getRealPath("/strs.properties")检索绝对文件路径可能更简单。

回答by bmargulies

This is not a robust way to go about it. If you want your webapp to consume data from 'outside', then call System.getProperty to get an absolute pathname that you, yourself, specify with -D in the tomcat config script.

这不是一个可靠的方法。如果您希望您的 web 应用程序使用来自“外部”的数据,则调用 System.getProperty 以获取您自己在 tomcat 配置脚本中使用 -D 指定的绝对路径名。