How does a runnable jar load an external XML file at runtime?

(This seems like a pretty simple problem, but got stuck for 2 days :()

I have a runnable jar (created using the maven assembly plugin ). The class inside the jar looks for the xml file in the classpath. However, we do not want to link the XML file in the bank and want it to be external.

Tried so far:

  • Set the class path at runtime:

     java -classpath ./conf -jar my-jar-with-dependencies.jar 

==> not loading (conf folder contains xml)

  1. Set classpath in assembler plugin

      <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <mainClass>com.xxx.Test</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>./conf/</classpathPrefix> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> 

==> does not add ClassPath to MANIFEST.MF in runnable jar

Edit:

Generated by MAINFEST.MF in the bank:

 Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: xxx Build-Jdk: 1.7.0_21 Main-Class: com.xxx.Test 

Edit 2 :

So, I edited the generated MANIFEST in the bank and the recreated bank. Still not finding xml!

 Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: xxx Build-Jdk: 1.7.0_21 Main-Class: com.xxx.Test Class-Path: . /* Tried with both . and ./conf */ 
+7
java xml jar maven
source share
2 answers

There seems to be no good answer to this question. In short:

  • java -jar ignores the -cp
  • Adding ClassPath to the manifset also has no effect (not sure if this is related to any particular way of loading the xml library)

So, the only remaining parameter that works is to manually call the class file:

 $ java -cp <...> MyPackage.MyClass 

where cp contains the jar file and the path to the conf folder.

0
source share

When you use the class tpath> t20> argument that you specify is ignored. It is listed here.

When you use this parameter, the JAR file is the source of all user classes, and other parameters of the user class path are ignored.

The JVM uses the class path specified in the manifest. Make sure the manifest contains the definition of the class path.

+2
source share

All Articles