0

I have a maven project that builds a xmlreader class that has 2 constructors. One that takes a external filename and one default that loads a resource.

The default constructor works for test, but doesn't find the file when I execute program with default parameters. Changing the way I access the resource makes the test fail and maven stops building. I have extracted the jar and the file is located there.

Here is the constructor:

public XmlReader() {
    this.filename = getClass().getResource("/Default.xml").getPath();
    this.git = new GitClass();
    this.cucumber = new CucumberClass();
    this.execute = new ExecuteClass();
}

Errormessage:

5030 [main] ERROR - File file:/Users/andreasjoelsson/XmlReader/target/XmlReader-1.0-SNAPSHOT.jar!/Default.xml not found.
5030 [main] DEBUG - Context: init of reader: false

JUnit test:

@Test
public void test2() {
    XmlReader testObject = new XmlReader();
    // Subject to change as it's the default config to run.
    assertEquals(true, testObject.init());
    assertEquals(25, testObject.getExecute().getMaxUsers());
    assertEquals(true, testObject.getExecute().isRepeatFeature());
    assertEquals(1, testObject.getExecute().getSecondsBetweenIncrease());
    assertEquals(2, testObject.getExecute().getUsersIncreasedEachInterval());
    assertEquals(20, testObject.getExecute().getWaitAfterRampSeconds());
    System.out.println(testObject.toString());
}

So the package that I execute test on works, but not when I run it outside of maven.

Andreas
  • 539
  • 1
  • 5
  • 13
  • Your question has already been answerred : http://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder. Try to use getResourceAsStream() methods. – Jean-Rémy Revy Feb 04 '14 at 15:12

2 Answers2

2

You cannot work with files that are part of the JAR as you work with them as part of your harddisk's file system.

During development, you have all the source files in a workspace. So there is indeed a physical file in the file system. Statements like new File(fileName) or getResource(resourceName).getPath() then work well.

When dealing with resources inside a JAR, you first must retrieve the resource via Class.getResource(resourceName) or via ClassLoader.getResource(resourceName). You will get a URL that does not have the file protocol, which is also the reason why you just can't invoke getPath() on it. You must retrieve an input stream with URL.openStream(). With that you can read your resource (and don't forget to close the stream afterwards).

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • Worked well when using streams instead. Was rather odd failure as I thought the program could access files in the jar like a regular filesystem. – Andreas Feb 04 '14 at 16:17
0

What you need is to create a Source Folder in your Project as the one called src but with a different name. To refer the files from this folder on Windows you just need to indicate the Project's name and the resource name as it:

getResource("ProjectName/Default.xml")

I hope this helps!

javixanxez
  • 86
  • 6