运行jar

时间:2019-06-03 16:22:46

标签: java maven file jar embedded-resource

我可以使用Maven正确构建我的jar。 在我的代码中,执行以下操作:

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

然后,当我运行jar时,我得到FileNotFoundException。 我知道jar里有文件configuration_file.txt,因为当我用winrar打开它时,可以直接看到它。

这是我的POM,以防万一:

<?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>

我认为问题可能与POM中的文件夹和路径有关,但我不确定,我真的不知道该尝试什么。

3 个答案:

答案 0 :(得分:3)

  1. 默认情况下,资源文件(您使用的是默认值,因为未在pom中定义它)应该用于maven项目的src / main / resources。默认情况下,Maven从该目录获取非源文件,并将它们放在生成的jar中的正确位置。

  2. 要访问Jar中的资源,您需要使用类加载器。已多次描述:Reading a resource file from within jar

答案 1 :(得分:0)

尝试一下,如果我们的资源在Spring应用程序的jar中,它将可以正常工作

InputStream resource = new ClassPathResource("src/configuration_file.txt").getInputStream();

答案 2 :(得分:0)

我想你可以这样考虑

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