3

I am trying to create jar where I can read a config file outside the jar file. Preferably from the directory where the jar file is located. My directory structure is as follows:

/src/main/resources/config

I have a maven project that I run mvn install on to create a jar file.

<plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.expedia.e3.qm.perforce.audit.Main</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>config/config.xml</Class-Path>
                    </manifestEntries>
                </archive>
                <excludes>
                    <exclude>**/config/</exclude>
                </excludes>
            </configuration>
        </plugin>

Right now I am manually moving my config folder from /target/classes/config to /target/config I have researched the maven assembly plugin however I have tried all the different configurations of it and I was unable to get it to output only to /target/config it would always output /target/audit-1.0/config where audit-1.0 is my maven project name.

I am using Intellij IDEA to debug my program and to build/install the maven project.

In code I am trying to access the file via

InputStream stream = Class.class.getResourceAsStream("/config/config.xml");

Which works when I run it in the IDE however when I run it from the command line:

java -jar ./audit-1.0.jar

It throws an error after trying to access "inputStream":

Exception in thread "main" java.lang.NullPointerException

I understand that "/config/config.xml" should point to "/target/audit-1.0.jar!/config/config.xml". However, I would like it to point to "/target/config/config.xml"

Any ideas? Is this even possible?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chris Watts
  • 2,284
  • 4
  • 23
  • 28
  • *"I am trying to create jar where I can read a config file outside the jar file."* Why not include it inside the Jar? – Andrew Thompson Jan 23 '13 at 23:31
  • I want to be able to edit the configuration of the program after I release the jar without having to recompile it. – Chris Watts Jan 23 '13 at 23:35
  • *"without having to recompile it."* Note that creating a new jar is not the same thing as recompiling the code that goes in there. OTOH I would typically put resources like config. in a separate Jar so [Java Web Start](http://stackoverflow.com/tags/java-web-start/info) can update just that part. – Andrew Thompson Jan 23 '13 at 23:42
  • Sorry creating a new jar with the changed config files included in it. Correct me if I'm wrong but putting resources in a separate jar, zip, or dir with the maven assembly plugin? That Java Web Start sounds like a good idea, however I don't have time to implement that right now. Is there a short term fix? – Chris Watts Jan 23 '13 at 23:56
  • *"Correct me if I'm wrong but putting resources in a separate jar, zip, or dir with the maven assembly plugin?"* ..What? That might have made sense if it ended with *".. is impossible(?)"* but as it stands I don't understand the question. BTW - I am pretty sure that Maven can serve JWS apps. – Andrew Thompson Jan 24 '13 at 00:02
  • maybe If you use Spring or something likes you can use property files. as example — using PropertyPlaceHolder like next http://www.javacodegeeks.com/2013/01/spring-property-placeholder-configurer-a-few-not-so-obvious-options.html – iMysak Jan 24 '13 at 00:11
  • @AndrewThompson sorry, I meant "is possible". To reiterate the question; I want to have a config.xml file outside of the jar in the same directory as the jar, so that I can change the configuration on the fly. For example, changing the config file to have test values instead of production values. – Chris Watts Jan 24 '13 at 00:49
  • *"For example, changing the config file to have test values instead of production values"* That's a hand full of lines in Ant and could be rebuilt (as a Jar) using a few key strokes in a short moment. I do not see why you cling to having the configuration as a loose file when the app. is actually running. – Andrew Thompson Jan 24 '13 at 01:03
  • In my case, people who don't speak java handle my applications after I'm done with them. They need to set config values for different environments in order to stand them up. Also, the maven assembly plugin is capable of accomplishing the problem stated above. – kyle Feb 02 '16 at 03:34
  • possible duplicate at http://stackoverflow.com/questions/8775303/read-properties-file-outside-jar-file – Digao Apr 02 '17 at 12:58

1 Answers1

3
File base = new File(<Current class name here>.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile();
File configFile= new File(base,"config.xml");
John Stadt
  • 500
  • 7
  • 17