1

I have written a Java program which I package and run from a JAR file. I need to have some user-changeable configuration files which are simply text lines of:

key = value

format. To load these files I used the class described here. When I run my program through Netbeans IDE all works fine as I have included the directory where I store the configuration files in the Project properties.

The problem comes when I build my application into a JAR file. As I want the configuration files to be user-editable I keep them OUTSIDE of the JAR but in the same directory but now when I run my application from the command line it cannot find the configuration files. If I manually add the files to JAR file at the ROOT folder then all is well.

So how can I tell Java to look outside of the JAR for my loadable files? The -classpath option has no effect.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97

3 Answers3

3

That's because the way you are loading them requires that they be inside the .jar when running from a jar, or inside the project directory if not; it's relying on the classloader to tell it where to find the file.

If you want to open a file outside the .jar, you need to just open it as a File and read it in.

One of the ways we've approached this is to take the external filename as an option on the command line (e.g. java -jar myJar.jar -f filename). This allows you to explicitly state where the file is located. You can then decide whether or not to also look in a default location, or inside the .jar if the file isn't specified on the command line.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • When you say it's required that the files be in a JAR, can you give me a reference for that? I don't understand how they are loaded when I run my application through Netbeans. As far as I know it doesn't make a JAR from my code just .class files. I haven't found a JAR file anyway – D-Dᴙum Feb 04 '12 at 17:15
  • @Kerbu - read the second part of the sentence I wrote. When the compiled classes *aren't* in a jar, the classloader returns a location in your project's directory structure (the location of the compiled .class files). – Brian Roach Feb 04 '12 at 17:17
  • @BrianRoach the solution was simple - please see my answer below. – likejudo Mar 21 '13 at 17:13
  • @Anil - Why are you posting an answer to a > 1 year old question that's already been (correctly) answered and accepted? Furthermore, yes, that's exactly what my (over one year old) answer says to do. – Brian Roach Mar 21 '13 at 17:18
  • @BrianRoach it was not clear to me how to do this in Java Webstart - the user cannot specify the file location, and I did not know where the file should be placed in the directory tree outside the jar, to be accessible. – likejudo Mar 21 '13 at 19:13
  • This should be the answer. Simple and perfect. You need to have a script to execute the Jar file anyway... – www Dec 08 '15 at 09:42
0

I had the same problem and saw your post, but the answer in the end, was simple.

I have an application deployed via Java Webstart and am building it in Netbeans 7.3.

I have a properties file config.xml that will be updated during run time with user preferences, for instance, "remember my password". Hence it needs to be external to the jar file.

Netbeans creates a 'dist' folder under the project folder. This folder contains the project jar file and jnlp file. I copied over the config.xml to the dist folder and the properties file was loaded using standard

    FileInputStream in = new FileInputStream("config.xml");
    testData.loadFromXML(in);
    in.close();
likejudo
  • 3,396
  • 6
  • 52
  • 107
0

I resolved it by referring to this question. I Added the current directory to the MANIFEST file of the jar and it works.

Why is the -classpath option ignored in this case I wonder? Security?

Community
  • 1
  • 1
D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
  • Sorry, I should have thought of that as well; never approached it that way. As for why `java` ignores `-classpath` when there's a manifest ... probably, yes. Also simplifies things (you can't have conflicting paths, etc) – Brian Roach Feb 04 '12 at 17:30
  • No problem. The purpose of the Propertyloader class I used (from the Javaworld link) was to simplify loading of configuration files as much as possible. also Netbeans doesn't allow..manual tweaking for the manifest prior to building (well as far as I can tell anyway). – D-Dᴙum Feb 04 '12 at 17:35