0

I'm able to build my jar correctly using maven. Inside my code I make the following:

    InputStream input = new FileInputStream("src/configuration_file.txt");

Then when I run my jar I get the FileNotFoundException. I know the file configuration_file.txt is there inside the jar because when I open it with winrar I'm able to see it directly.

Here's my POM just in case:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>group7</groupId>
  <artifactId>genetic</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>main</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

    <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/</sourceDirectory>
    <resources>
        <resource>
            <directory>src/</directory>
            <includes>
                <include>*.txt</include>
            </includes>
        </resource>
    </resources>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>


        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>genetic_algorithm.Main</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
        </plugin>


        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

I'm thinking the problem might be related with the folders and the path inside of the POM but I'm not sure and I really don't know what else to try.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Flama
  • 772
  • 3
  • 13
  • 39
  • you need to set the directory path in the application.configuration file so that the jar the file paths are relative to the jar rather than being on your local system. – LearningToCode Jun 03 '19 at 16:28

3 Answers3

3
  1. The resource files by default (you are using defaults cause you don't define it in your pom) are supposed to go to src/main/resources for maven projects. Maven by default gets non source files from that directory and puts them in the correct place in the resulting jar.

  2. To access resource that is inside the Jar you need to use the classloader. It has been described multiple times: Reading a resource file from within jar

LearningToCode
  • 392
  • 5
  • 18
maslan
  • 2,078
  • 16
  • 34
0

Try this, it will work when our resource is inside a jar in case of a Spring Application

InputStream resource = new ClassPathResource("src/configuration_file.txt").getInputStream();
Shashank Gupta
  • 321
  • 3
  • 15
  • 1
    this only works if you have spring dependencies, by the pom I can tell the OP is not using spring – maslan Jun 03 '19 at 16:28
0

I thing you may consider this way

  InputStream i = this.getClass().getResourceAsStream(File.separator+"configuration_file.txt");
  BufferedReader r = new BufferedReader(new InputStreamReader(i));
  System.out.println(r.readLine());
Gorden
  • 1
  • 2