Java -jar : 访问外部配置文件 [英] Java -jar : access external configuration file

查看:70
本文介绍了Java -jar : 访问外部配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些我认为不会很难的事情.

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

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

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

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

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

解决方案

我会为后代保留我对这个问题的解决方案.

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

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

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

无论如何,您很有可能会构建一个脚本来为您启动程序,因此命令行调用增加的复杂性实际上并不多,因为无论如何您只会构建一次.

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

您还会看到我已经展示了您仍然可以在命令行上提交参数的方法.这是我开始时不确定的事情,但它按预期工作.

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

我希望这对其他人有帮助.如果有人能够说不要费心让 -jar 工作 - 只需使用 -cp 方法",那肯定会为我节省很多时间......

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?

解决方案

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:

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.

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

这篇关于Java -jar : 访问外部配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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