简体   繁体   中英

Java executable jar classpath confusion

I have read the answer about executable jars and adding the current path to the classpath here .

I added the plugins to pom.xml, I checked and this line is in fact added to MANIFEST.MF:

Class-Path: .

Now according to log4j2 documentation, it will look in the current classpath for a log4j2.xml file.

My understanding is, that if I place log4j2.xml in the same directory as the jar, and execute the jar while I am in that directory, then it will find the log4j2.xml.

However, it only finds it when I add a

-Dlog4j.configurationFile=/path/to/file

to the java command.

What am I missing?

Frankly, it just seems easier to forget about the classpath stuff and just add a bunch of -D terms to the java command line.

I think the . is evaluated as current directory relative to the java process current working directory (when processes are started by the OS, they are given a working directory). You can see the working directory of the JVM that runs your program by examinating the system property user.dir from withing your program.

# current directory is /home/rick
$pwd
/home/rick
# the code archive is not in the current wd
$ls /home/rick/myapps
app.jar
# the conf file is
$ls .
log4j2.xml
# This will work as expected
$java -jar /home/rick/myapps/app.jar

Strictly speaking, the JAR doesn't even need to be on your filesystem: for example, it could be downloaded from the network and extracted in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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