1

I have a Maven project that I'm trying to package as both a war and a jar. As part of my application / servlet initialisation (depending on whether I'm running the jar or the war), I need to read a file called server.ini. I've put the file in src/main/resources/server.ini and am trying to load it like so:

System.class.getResourceAsStream("server.ini");

However, this always results in null. What am I doing wrong?

Josh Glover
  • 22,264
  • 24
  • 83
  • 124

3 Answers3

1

The server.ini file should be in the root of a resources directory.

By placing it in the webapp you're making the file available via http, but you need it accessible on the classpath, which means that you should place it in the resources directory.

Software Engineer
  • 13,509
  • 5
  • 57
  • 83
  • This is exactly what I want to do: load the config file as a resource so it works regardless of whether I'm running the war or the jar. I've updated my question to make this more clear. However, my attempt to load the file as a resource is failing on both the jar and the war. I'm sure I'm doing something trivial wrong, but can't figure out what it is. :-/ – Josh Glover Feb 14 '14 at 15:15
  • Are you building with maven? Putting the file in the resources directory will result in it being where it needs to be. If you put it in a sub-directory of resources, then you need to adjust your java line to reference the subdirectories too. – Software Engineer Feb 14 '14 at 15:19
  • And now I know what the trivial error is: I needed to preface my filename with a slash, otherwise the class loader was looking in `java.lang`. So this works: `this.getClass().getResourceAsStream("/server.ini")` – Josh Glover Feb 14 '14 at 15:19
1

There's a good chance web.xml or context.xml is better suited to what you're trying to do, but...

Try putting server.ini in WEB-INF/classes, or do something like this.

Community
  • 1
  • 1
David Ehrmann
  • 6,655
  • 1
  • 23
  • 33
  • You shouldn't put anything directly in WEB-INF/classes. Maven will clear out that directory when you tell it to clean and you'll loose your file. That directory is effectively under maven's control. – Software Engineer Feb 14 '14 at 15:20
  • But I do agree that you're probably better off using web.xml to configure your server. – Software Engineer Feb 14 '14 at 15:21
  • If I was only running this in a servlet container, I could use web.xml. However, I need it to run out of a jar as well. – Josh Glover Feb 14 '14 at 15:24
1

The issue was that I was using the System classloader with an unqualified path, so it was expecting to find my server.ini in the java.lang package.

Since my file is in src/main/resources, I should just use the classloader of my current class, with an absolute path:

getClass().getResourceAsStream("/server.ini")

This works in both the war and the jar.

The "Preferred way of loading resources in Java" question has a great explanation of resource loading.

Community
  • 1
  • 1
Josh Glover
  • 22,264
  • 24
  • 83
  • 124