Java -jar : 访问外部配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2232602/
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-13 05:02:09  来源:igfitidea点击:

Java -jar : access external configuration file

javaconfigurationjarclasspathexternal

提问by Joel

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.

我有一个应用程序,我想将其打包为 jar,因为我有大约 30 个依赖项,并且希望能够部署单个文件。

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.

我有一些配置文件 - 一个属性文件和一个 spring 配置文件,以及我的 log4 props 文件 - 我想要在 jar 外部。我想我期望如果我将它们放在与 jar 相同的目录中,它会在运行时找到它们,但它没有。

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...

在开发时,我将这些文件放在我的 eclipse 项目的类路径的根目录下,应用程序发现它们很好。我觉得我错过了 jar / classpath 理论的一些关键方面......

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.

所以我想要的是能够将配置文件和 jar 放在同一目录中,并让应用程序在我使用标准 java -jar 东西运行它时找到配置文件。

Is there not a simple way to achieve this?

有没有一种简单的方法可以实现这一目标?

采纳答案by Joel

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.

我认为我可能期望 java -jar 做一些它没有做的事情。

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:

如果您使用常规 java 命令,您可以将 jar 包含在类路径中,最终得到的结果与 java -jar 几乎相同,只是您必须明确命名要使用的类。它看起来像这样:

java -cp .:path/to/Jar.jar com.company.package.Class arg1=val1 arg2=val2 ....

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.

你会看到我包含了'.' 作为类路径上的第一项,这意味着与 jar 相同目录中的任何内容也将包含在类路径中。当然,jar 中的所有内容也包含在类路径中,因为 jar 本身也在类路径中。

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.

您还将看到我已经展示了如何仍然可以在命令行上提交 args。这是我开始时不确定的事情,但它按预期工作。

I'm sure this is just basic java deployment knowledge, but I was lacking it for some reason.

我确定这只是基本的 Java 部署知识,但由于某种原因我缺乏它。

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 工作 - 只需使用 -cp 方法”,那肯定会为我节省很多时间......

回答by Jay R.

You need to add "." to the classpath of the jar file you are building for your application.

您需要添加“。” 到您为应用程序构建的 jar 文件的类路径。

So in the manifest, I'd expect to see

所以在清单中,我希望看到

Main-Class: some.full.Name
Class-Path: . needed-lib.jar other-lib.jar 

Then, when you run your app by executing

然后,当您通过执行运行您的应用程序时

java -jar myapp.jar

it actually uses

它实际上使用

java -classpath .;needed-lib.jar;other-lib.jar some.full.Name

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.

这样,目录中包含 myapp.jar 文件的任何文件也将位于类路径中。该类路径相对于包含它的 jar 的位置。

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.

Log4j 期望 log4j.xml 配置文件位于类路径上。如果您不使用名称 log4j.xml,您还必须向启动命令添加一个系统属性,以告诉 log4j 库寻找不同的名称。

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.

我不确定 Spring 对配置文件的期望。并且属性文件加载取决于用于加载文件的机制。使用 FileReader 或 InputStream 根本不使用类路径机制。在这种情况下,您需要知道应用程序期望文件相对于当前工作目录的位置。