Daniel Gerber
Daniel Gerber

Reputation: 3470

mvn exec:java fails with FileNotFoundException

I have a maven project with 2 modules. There is a crawler module which depends on the core module. Each module has it's own config.ini file in src/main/resources/ and one single main class per module.

Now I want to start the NewsCrawler like this:

mvn exec:java -Dexec.mainClass="org.aksw.simba.rdflivenews.NewsCrawler"

This works for loading the crawlers own config file but fails to load the config file from core-module:

NewsCrawler.CONFIG = new Config(new Ini(File.class.getResourceAsStream("/newscrawler-config.ini")));
RdfLiveNews.CONFIG = new Config(new Ini(File.class.getResourceAsStream("/rdflivenews-config.ini")));

The second config load fails with a NullPointerException. I checked the jar file and the config file is inside. The strange thing is that same two lines work in the test cases. Also with eclipse I can start the main of the class without any problems.

Upvotes: 2

Views: 915

Answers (2)

Daniel Gerber
Daniel Gerber

Reputation: 3470

This solved my problem! I think i look in the wrong (class)path ...

NewsCrawler.CONFIG = new Config(new Ini(NewsCrawler.class.getResourceAsStream("/newscrawler-config.ini")));
RdfLiveNews.CONFIG = new Config(new Ini(RdfLiveNews.class.getResourceAsStream("/rdflivenews-config.ini")));

Upvotes: 0

Jérôme Radix
Jérôme Radix

Reputation: 10533

Here is a well-functioning source code of your example.

dezip, then on the command line, go to the directory containing the parent pom then do :

mvn clean test

Do something like this to know where you are when calling getResourceAsStream() :

try {
    IniFile = new Ini(File.class.getResourceAsStream("/newscrawler-config.ini"));
} catch(Exception e) {
    System.out.println("Resource file not found : " + File.class.getResource("."));
}

Also try to remove / before newscrawler-config.ini. It depends on where you put your files in the hierarchy. getResourceAsStream search the CLASSPATH, not the source path.

You may also use this.getClass().getResourceAsStream() instead of File.class.getResourceAsStream().

It could be a solution to copy the src/main/resource to your target/test-classes by configuring resource in your pom.xml :

<build>
  <resources>
    <resource>
      <filtering>false</filtering>
      <directory>${basedir}/src/main/resource</directory>
    </resource>
  </resources>
  <testResources>
    <testResource>
      <filtering>false</filtering>
      <directory>${basedir}/src/main/resource</directory>
    </testResource>
  </testResources>
</build>

and call getResourceAsStream() with a quasi-full-path :

this.class.getResourceAsStream("/com/jeromeradix/stackoverflow/newscrawler/newscrawler-config.ini"));

Upvotes: 2

Related Questions