Web 应用程序中使用的 JAR 文件的相对路径

发布于 2025-01-04 11:51:27 字数 579 浏览 2 评论 0原文

我有一个 Java 应用程序(非 Web),它必须从属性文件中读取。该 Java 应用程序也必须导出为 JAR 文件,然后在 Web 应用程序中使用,但属性文件不会放入 JAR 文件中。它看起来像这样:

/webapps/MyWebApp/WEB-INF/myJavaApp.jar
/webapps/MyWebApp/WEB-INF/config.properties

因此,在编写将导出为 myJavaApp.jar 的 Java 应用程序时,我无法执行此操作,因此它将从相对路径读取 config.properties 文件。如果我放置绝对路径,它可以正常工作:

properties.load(new FileInputStream("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/MyWebApp/WEB-INF/config.properties"));

但是,如果我尝试使用相对路径,它就不起作用。我尝试过这里或其他网页上的不同解决方案,但没有一个有效。

差点忘了,我正在读取属性文件的类是静态的!

I have a Java application (non-web) which must read from a properties file. This Java application must as well be exported as a JAR file and then is used in a web application, but the properties file is not into the JAR file. It would look like this:

/webapps/MyWebApp/WEB-INF/myJavaApp.jar
/webapps/MyWebApp/WEB-INF/config.properties

So, when coding the Java application that will be exported as myJavaApp.jar I am unable to do it so it would read the config.properties file from a Relative path. If I put an Absolute path it works correctly:

properties.load(new FileInputStream("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/MyWebApp/WEB-INF/config.properties"));

But, if I try to use a relative path it doesn't work. I've tried different solutions from here or other webpages but none of them are working.

Almost forgot, the class where I am reading the properties file is static!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2025-01-11 11:51:27

如果您确保该文件位于类路径上,那么您可以简单地加载它与YourClassName.class.getResourceAsStream("/config.properties")。这将是实现它的最简单的方法。
对于您的 Web 应用程序,您可以将文件放在 WEB-INF/classes/ 中,对于您的独立应用程序,您只需在启动应用程序时设置类路径即可。

If you make sure the file is on the classpath, then you can simply load it with YourClassName.class.getResourceAsStream("/config.properties"). That would be the easiest way of implementing it.
For your webapplication, you put the file in WEB-INF/classes/, and for your standalone application you simply set the classpath when starting the application.

七色彩虹 2025-01-11 11:51:27

在您的Java应用程序中,您可以通过以下方式获取当前目录的绝对路径:

 System.getProperty("user.dir");  

假设您有config.properties驻留在同一目录中,您应该能够在您的Java应用程序中找出该文件的绝对路径java 代码,您可以将其部署在任何地方,而无需在代码中硬编码绝对路径。

编辑:

如果您的Java应用程序是可执行JAR,则仅当直接调用上述命令时才会返回正确的目录。如果你从另一个进程间接调用这个应用程序,在Windows平台上,起始目录将是windows/system32,所以你必须分析你的jar是如何被调用的。如果是后者,最好在 Web 应用程序中添加功能,为 jar 提供属性文件的路径。

In your Java Application, you can get the absolute path of current directory by:

 System.getProperty("user.dir");  

Assuming you have config.properties residing in the same directory, you should be able to figure out the absolute path of this file in your java code and you can deploy it anywhere, without hard-coding the absolute path in your code.

EDIT:

If your Java App is an executable JAR, above command will return correct directory only if it is directly invoked. If you invoke this application indirectly from another process, on a windows platform, the starting directory will be windows/system32, So you have to analyze how your jar is being invoked. If latter is the case, it is better to add functionality in your web app to provide jar with the path of your properties file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文