2017-04-18 5 views
0

Java 응용 프로그램이 있고 Maven 음영 플러그인을 사용하여 JAR 파일에 번들했습니다. 어떤 이유로, target /에서 JAR 파일을 실행할 때 JAR이 자원 폴더에서 파일을 올바르게 가져 오지 못하여 FileNotFoundException을 발생시킵니다.JAR에서 자원을 올바르게 사용하지 않습니다. (Maven)

오류 메시지 '자바 -jar 대상/speech2code-1.0-SNAPSHOT-fatjar.jar'실행하는 경우 : 내가 파일을 얻고 코드의 라인

java.io.FileNotFoundException: file:/Users/mb/Desktop/Speech2Code-IDE/speech2code/target/speech2code-1.0-SNAPSHOT-fatjar.jar!/analyser/map.txt (No such file or directory) 

입니다

:

File file = new File(getClass().getResource("/analyser/map.txt").getFile()); 

(map.txt는 /src/main/resources/analyser/map.txt에 내 디렉토리 구조는 다음과 같습니다

/src 
    /main 
     /java 
     /resources 
       /analyser 
        /map.txt 
    /test 

내 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>org.mayur.speech2code</groupId> 
<artifactId>speech2code</artifactId> 
<version>1.0-SNAPSHOT</version> 
<build> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <includes> 
       <include>**/*.txt</include> 
       <include>**/*.css</include> 
       <include>**/*.fxml</include> 
       <include>**/*.gram</include> 
       <include>**/*.dict</include> 
      </includes> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>3.0.0</version> 
      <configuration> 
       <finalName>${project.build.finalName}-fatjar</finalName> 
       <transformers> 
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
         <mainClass>App</mainClass> 
        </transformer> 
       </transformers> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

    </plugins> 
</build> 
<repositories> 
    <repository> 
     <id>snapshots-repo</id> 
     <url>https://oss.sonatype.org/content/repositories/snapshots</url> 
     <releases><enabled>false</enabled></releases> 
     <snapshots><enabled>true</enabled></snapshots> 
    </repository> 
    <repository> 
     <id>central</id> 
     <url>http://repo1.maven.org/maven2/</url> 
    </repository> 
</repositories> 
<dependencies> 
    <dependency> 
     <groupId>edu.cmu.sphinx</groupId> 
     <artifactId>sphinx4-core</artifactId> 
     <version>5prealpha-SNAPSHOT</version> 
    </dependency> 
    <dependency> 
     <groupId>edu.cmu.sphinx</groupId> 
     <artifactId>sphinx4-data</artifactId> 
     <version>5prealpha-SNAPSHOT</version> 
    </dependency> 
    <dependency> 
     <groupId>org.fxmisc.richtext</groupId> 
     <artifactId>richtextfx</artifactId> 
     <version>0.7-M2</version> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>3.0.0</version> 
     <type>maven-plugin</type> 
    </dependency> 
    <dependency> 
     <groupId>org.reflections</groupId> 
     <artifactId>reflections</artifactId> 
     <version>0.9.10</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-lang</groupId> 
     <artifactId>commons-lang</artifactId> 
     <version>2.6</version> 
    </dependency> 
</dependencies> 
</project> 

도움을 주실 수 있습니다!

+0

나는 당신의 문제가 당신이'File'을 사용하고 있다고 생각한다. 음, 약간의 인터넷 검색, [read this] (http://stackoverflow.com/questions/4548699/load-file-within-a-jar) . –

+1

'파일 파일 = 새 파일 (getClass(). getResource ("/ analyzer/map.txt"). getFile());'임베디드 리소스에 액세스하는 잘못된 방법이며, 포함 된 경우'File' 참조가 아닙니다. 'Jar'안에는 Zip 항목이 있습니다. getClass(). getResource ("/ analyzer/map.txt")'를 사용하여 URL 참조를 얻거나 getClass(). getResourceAsStream ("/ analyzer/map.txt")' InputStream' 리소스에 대한 참조 – MadProgrammer

+0

고마워요, 2 시간을 계산하고 5 분 안에 나에게 말해 줬어. 그것은 작동합니다! thanks :) – user3650664

답변

1

그래서 자원을 Java의 파일로 변환하는 대신 InputStream을 사용하여 읽어야합니다. 위의 코드를 변경하고 InputStream을 사용하여 작동합니다. 감사합니다 MadProgrammer와 Bagus Tesa!