简体   繁体   English

从与jar文件相同的目录加载文件

[英]Load file from same directory as jar-file

I'm using log4j for my logging purposes. 我将log4j用于记录目的。 To configure everything, I've made a log4j.xml that is outside of the jar-file. 为了配置所有内容,我在jar文件之外创建了一个log4j.xml。 It works great, but gives problems when the jar-file isn't executed from the same directory where the log4j.xml is. 它的工作原理很好,但是当没有从log4j.xml所在的目录执行jar文件时,就会出现问题。

Example: jar-file is in /opt/myapp/test.jar 示例:jar文件位于/opt/myapp/test.jar中

user@host:/opt/myapp$ java -jar test.jar

works great, but when I try this: 效果很好,但是当我尝试这样做时:

user@host:~$ java -jar /opt/myapp/test.jar

I get an error that log4j.xml cannot be found. 我收到一个错误消息,找不到log4j.xml。 That's because it is looking for it in my current working directory. 那是因为它正在我当前的工作目录中寻找它。

I also have a properties file that I use in my application. 我也有一个我在应用程序中使用的属性文件。 This file is in the config/ directory where the jar-file is, so /opt/myapp/config in this example. 该文件位于jar文件所在的config /目录中,因此在此示例中为/ opt / myapp / config。 The same issue like the one above occurs when trying to load the config file. 尝试加载配置文件时,会发生与上述相同的问题。

How can I let Java load a file from the directory where the jar-file is (no matter from where the jar is executed)? 如何让Java从jar文件所在的目录中加载文件(无论从jar的执行位置开始)?

Probably something very easy, but I can't figure it out :( 可能很简单,但我无法弄清楚:(

You need to pass the file with the command line argument -Dlog4j.configuration=/path/to/file. 您需要使用命令行参数-Dlog4j.configuration = / path / to / file传递文件。

Check the manual if you need more options. 如果需要更多选项,请查阅手册

The most reliable way to do this is to ask the JVM where the byte code for a given class came from. 最可靠的方法是询问JVM给定类的字节码来自哪里。

See http://www.exampledepot.com/egs/java.lang/ClassOrigin.html for this snippet: 有关此代码段,请参见http://www.exampledepot.com/egs/java.lang/ClassOrigin.html

// Get the location of this class
Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/

When loaded from inside a JAR you need to do a bit more post-processing. 从JAR内部加载后,您需要做更多的后处理。

Try: 尝试:

java cp /opt/myapp/someohter.jar -jar /opt/myapp/test.jar

Use properties, to specify yourself, where to look for config files, for resources and such (as Paulo pointed out). 使用属性来指定您自己,在哪里可以找到配置文件,资源等(如Paulo所指出的)。

It is usually a bad idea to put such files close to the jar.file and make execution depending on the place from where you start. 通常将这样的文件放在jar.file附近并根据您开始的位置执行代码是一个坏主意。 Often the directory, where an application is installed (the jar) is write protected for users, but config files are made to be editable. 通常,安装应用程序的目录(罐子)对用户具有写保护,但使配置文件可编辑。

  1. Specify using log4j system property, add this before that "-jar" part of the command: -Dlog4j.configuration=/opt/myapp/log4j.xml 使用log4j系统属性指定,在命令的“ -jar”部分之前添加此属性:-Dlog4j.configuration = / opt / myapp / log4j.xml

  2. Add the directory to the classpath, add this before that "-jar" part of the command: 将目录添加到类路径,将其添加到命令的“ -jar”部分之前:

    -cp /opt/myapp/ -cp / opt / myapp /

  3. You can specify the log4j.xml file manually in code and then reference it relative your jar file. 您可以在代码中手动指定log4j.xml文件,然后相对于jar文件引用它。

    String path = ... ; 字符串路径= ...; PropertyConfigurator.configure(path); PropertyConfigurator.configure(路径);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM