Java -jar :访问外部配置文件
我想做一些我认为并不困难的事情。
我有一个应用程序,我想将其打包为 jar,因为我有大约 30 个依赖项,并且我希望能够部署单个文件。
我有一些配置文件 - 一个属性文件和一个 spring 配置文件,以及我的 log4 props 文件 - 我希望将它们放在 jar 外部。我想我预计如果我将它们放在与 jar 相同的目录中,它在运行时会找到它们,但事实并非如此。
在开发过程中,我将这些文件放在 Eclipse 项目的类路径的根目录中,应用程序发现它们很好。我觉得我缺少 jar / 类路径理论的一些关键方面...
所以我想要的是能够将配置文件和 jar 放在同一目录中,并让应用程序在运行时找到配置文件与标准 java -jar 的东西。
难道没有一个简单的方法可以实现这一点吗?
I'm looking to do something which I thought was not going to be difficult.
I have an application that I'd like to package up as a jar because I've got ~30 dependencies and I would like to be able to deploy a single file.
I have some configuration files - a properties file and a spring configuration file, and my log4 props file - that I would like to have external to the jar. I guess I expected that if I put them in the same directory as the jar it would find them when it ran, but it doesn't.
While developing, I have these files at the root of the classpath for my eclipse project and the app finds them just fine. I feel like I'm missing some key aspect of jar / classpath theory...
so what I want is to be able to put the config files and the jar in the same directory and have the app find the config files when I run it with the standard java -jar thing.
Is there not a simple way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将为后代保留我对这个问题的解决方案。
我认为我可能期望 java -jar 做一些它没有做的事情。
如果您使用常规 java 命令,您可以将 jar 包含在类路径中,最终会得到与 java -jar 几乎相同的结果,只是您必须专门命名要使用的类。它看起来像这样:
无论如何,您都有可能构建一个脚本来启动程序,因此命令行调用所增加的复杂性实际上并不算多,因为无论如何您只会构建一次。
你会看到我添加了“.”作为类路径上的第一项,这意味着与 jar 位于同一目录中的任何内容也将包含在类路径中。当然,jar 内的所有内容也包含在类路径中,因为 jar 本身也位于类路径中。
您还将看到我已经展示了如何仍然可以在命令行上提交参数。这是我开始时不确定的事情,但它按预期工作。
我确信这只是基本的 java 部署知识,但由于某种原因我缺乏它。
我希望这对其他人有帮助。如果有人能够说“不用费心去尝试让 -jar 的东西工作 - 只需使用 -cp 方法”,这肯定会节省我很多时间......
I will preserve for posterity my solution to this problem.
I think it's possible that I was expecting java -jar to do something it doesn't do.
If you use the regular java command you can include the jar on the classpath and you'll end up with pretty much the same thing as java -jar, only you have to specifically name the class you want to use. It looks like this:
Chances are you're going to build a script to start the program for you anyway, so the added complexity of the command line call doesn't really amount to much, since you'll only build it once anyway.
You'll see I included '.' as the first item on the classpath, which means anything in the same directory as the jar will be included on the classpath as well. Of course everything inside the jar is included on the classpath also, since the jar itself is on the classpath too.
You'll also see that I've shown how you can still hand in args on the command line. This was something I was unsure about when I started but it works as expected.
I'm sure this is just basic java deployment knowledge, but I was lacking it for some reason.
I hope this helps someone else. It certainly would have saved me a lot of time if someone had been able to say "don't bother trying to get the -jar thing working - just use the -cp method"...
您需要添加“。”到您为应用程序构建的 jar 文件的类路径。
因此,在清单中,我希望看到
然后,当您通过执行它来运行应用程序时,
它实际上使用
这种方式,带有 myapp.jar 文件的目录中的任何文件也将位于类路径上。该类路径相对于包含它的 jar 的位置。
Log4j 期望 log4j.xml 配置文件位于类路径上。如果您不使用名称 log4j.xml,您还必须在启动命令中添加一个系统属性,以告诉 log4j 库查找不同的名称。
我不确定 Spring 对配置文件的期望。而属性文件的加载取决于使用什么机制来加载该文件。使用 FileReader 或 InputStream 根本不使用类路径机制。在这种情况下,您需要知道应用程序期望文件相对于当前工作目录的位置。
You need to add "." to the classpath of the jar file you are building for your application.
So in the manifest, I'd expect to see
Then, when you run your app by executing
it actually uses
This way any file in the directory with the myapp.jar file will also be on the classpath. That classpath is relative to the location of jar containing it.
Log4j for one expects the log4j.xml configuration file to be on the classpath. If you aren't using the name log4j.xml you also have to add a system property to your start up command to tell the log4j library to look for a different name.
I'm not sure what Spring expects for configuration files. And property file loading depends on what mechanism is used to load the file. Using a FileReader or InputStream doesn't use the classpath mechanism at all. In that case you need to know where the application is expecting the file to be relative to the current working directory.