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

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

问题描述

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

I'm looking to do something which I thought was not going to be difficult.

我有一个应用程序,我想打包成一个jar,因为我有〜30个依赖项,我想能够部署单个文件。

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.

我有一些配置文件 - 一个属性文件和一个spring配置文件,以及我的log4 props文件 - 我想在jar外部。我想我希望如果我把它们放在同一个目录下的jar它会找到它们当它运行,但它不是。

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.

在开发时,我有这些文件在我的eclipse项目的类路径的根,应用程序发现他们很好。我觉得我缺少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...

所以我想要的是能够把配置文件和jar相同的目录,并让应用程序找到配置文件,当我运行它与标准的java -jar的东西。

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.

我认为这是可能的,我期望java -jar做一些不做的事情。

I think it's possible that I was expecting java -jar to do something it doesn't do.

如果你使用常规的java命令,你可以包括jar classpath,你会得到与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 -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.

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

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.

你也会看到,仍然可以在命令行上输入args。这是我不确定当我开始,但它的工作原理。

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.

我确定这只是基本的java部署知识,但我缺乏它的一些原因。

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

我希望这可以帮助别人。这当然会节省了我很多时间,如果有人能够说不要试图让-jar工作 - 只是使用-cp方法...

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天全站免登陆