使用jar之外的config启动.jar,但是可以从任何当前目录执行 [英] Launch .jar with config outside of jar, but be able to do it from ANY current directory

查看:168
本文介绍了使用jar之外的config启动.jar,但是可以从任何当前目录执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决一个特定的问题。我在.jar文件中有一个Java程序,我在.jar之外有一个lib目录和一个config目录,但是在jar本身所在的目录中。

I've been wrestling with a particular problem. I have a Java program in a .jar file, and I have a lib directory and a config directory outside of the .jar, but in the same directory as the jar itself exists in.

我试图从代码中引用 config / foo.config 。如果我与jar在同一目录中,则将其作为相对文件引用。我也试过使用getResourceAsStream并确保config在类路径中。

I am trying to reference config/foo.config from within the code. Referencing it as a relative file works if I'm in the same directory as the jar. I've also tried using getResourceAsStream and making sure config is in the classpath.

到目前为止,这很好,但我也必须能够从中启动.jar任何目录。

So far, so good, but I also have to be able to launch the .jar from any directory.

所以,如果我的结构如此:

So, if my structure is like so:

/prog/util/myprog/myprog.jar
/prog/util/myprog/config
/prog/util/myprog/config/foo.config
/prog/util/myprog/lib

(以及lib中的一大堆第三方罐子)

(and a whole bunch of 3rd party jars within lib)

如何在我的清单文件中正确设置类路径以便可以引用配置?

How do I correctly set up the classpath in my Manifest file so that config can be referenced?

我的清单中的类路径如下所示:
Class-Path:config / lib / jar1.jar lib / jar2.jar (etc)

The classpath in my manifest looks like so: Class-Path: config/ lib/jar1.jar lib/jar2.jar (etc)

我目前正在使用 getResourceAsStream(/ foo.config)

如果我在/ prog / util / myprog目录并运行:

This all works if I am in the /prog/util/myprog directory and run:

java -jar myproj.jar

然而,我不能,比方说,b e in / prog and run:

However, I cannot, say, be in /prog and run:

java -jar util/myprog/myprog.jar

当我这样做时,找不到配置文件。

When I do this, the config file cannot be found.

我如何解决这个问题?

编辑:基于评论/建议的一些额外说明,但我不确定这是否可行:

Some additional notes based on comments/suggestions, though I'm not sure this is feasible:

1)我们不能使用文件系统的绝对路径,我们不知道程序将存储在何处,只是config目录与jar存在于同一目录中。

1) We can't use an absolute path for the file system, we don't know where the program will be stored, just that the config directory will be in the same directory as the jar.

2)我希望能够在代码是jar的时候有一些可以工作的东西,但是当我运行的时候也可以工作。在Eclipse中的调试模式下。此时,config目录是 src bin lib的兄弟。 目录。

2) I would like to be able to have something that works both when the code is jar'd, but also would work not-jar'd such as when I'm running in debug mode in Eclipse. At that point, the config directory is a sibling of the src, bin, and lib directories.

编辑第2部分:好的,在同事和我之间,我们想出了以下内容:
String configDirectory =新文件(QueueMonitor.class.getProtectionDomain()。getCodeSource()。getLocation()。getPath())。getParent()+/ config;

EDIT Part 2: Ok, between a colleague and myself, we came up with the following: String configDirectory = new File(QueueMonitor.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent() + "/config";

当我们在Eclipse中并以调试模式运行,File对象指向 bin 目录,因此在开发/测试期间获取父级然后附加/ config可满足我们的需求。

When we are in Eclipse and running in debug mode, the File object points to the bin directory, so getting the parent then appending "/config" works for our needs during development/testing.

同样,从.jar文件中,File对象指向jar本身,因此获取父级并附加/ config也可以提供我们所需要的内容。

Likewise, from a .jar file, the File object points to the jar itself, so getting the parent and appending "/config" gives us what we need as well.

不过,我有点犹豫。我想知道在这个解决方案中是否存在一些潜在的问题或意外后果。有什么想法?

I'm a little hesitant, though. I'm wondering if there's some potential problem or unintended consequence that I am not seeing in this solution. Any thoughts on that?

推荐答案

你应该可以使用从一个类里面找到一个罐子的路径?找到罐子位置的技术。从那里开始,只需要进出文件夹。

You should be able to use the Finding the path to a jar from a Class inside it? technique to find the location of your jar. From there it's just a matter of climbing in/out of folders.

这篇关于使用jar之外的config启动.jar,但是可以从任何当前目录执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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