2016-12-24 4 views
0

IntelliJからmain()を実行すると、標準外の結果としてpeter,johnsonが返されます。しかし、mvn clean packageを実行してjava -jar target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jarを実行しようとすると、次のエラーメッセージが表示されます。なぜ、どのように.propertiesファイルが読み込まれていることを確認できますか?jarファイルのJavaプロパティファイルが見つかりません。

java -jar target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar 
Exception in thread "main" java.io.FileNotFoundException:  file:/Users/ismar.slomic/src/ADOP/example/target/example-1.0.0-SNAPSHOT- jar-with-dependencies.jar!/my-example.properties (No such file or directory) 
at java.io.FileInputStream.open0(Native Method) 
at java.io.FileInputStream.open(FileInputStream.java:195) 
at java.io.FileInputStream.<init>(FileInputStream.java:138) 
at java.io.FileInputStream.<init>(FileInputStream.java:93) 
at com.example.MyPropertiesClass.getFullName(MyPropertiesClass.java:18) 
at com.example.MyMainClass.main(MyMainClass.java:9) 

MyMainClass

package com.example; 

import java.io.IOException; 

public class MyMainClass { 

    public static void main(String[] args) throws IOException { 
     MyPropertiesClass mpc = new MyPropertiesClass(); 
     System.out.println(mpc.getFullName()); 
    } 
} 

MyPropertiesClass

package com.example; 

import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.Properties; 

public class MyPropertiesClass { 

    public String getFullName() throws IOException { 
    Properties properties = new Properties(); 
    ClassLoader cl = this.getClass().getClassLoader(); 
    String filePath = cl.getResource("my-example.properties").getFile(); 

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath)); 
    properties.load(bis); 

    String name = properties.getProperty("name"); 
    String lastname = properties.getProperty("lastname"); 
    return name + "," + lastname; 
    } 
} 

my-example.properties

name=peter 
lastname=johnson 

のpom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    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>com.example</groupId> 
<artifactId>example</artifactId> 
<version>1.0.0-SNAPSHOT</version> 

<name>${project.artifactId}</name> 
<description>My Example</description> 

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

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>3.0.0</version> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <classpathPrefix>lib/</classpathPrefix> 
         <mainClass>com.example.MyMainClass</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
      <executions> 
       <execution> 
        <id>make-assembly</id> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

答えて

2

あなたのプロパティファイルには、あなたのjarファイル内にある場合は、getResourcegetFileの代わりにgetResourceAsStreamを使用した方が良いと思います。

ような何か:

ClassLoader cl = this.getClass().getClassLoader(); 
    try (InputStream stream = cl.getResourceAsStream("my-example.properties")) { 
    properties.load(bis); 
    } 

のjar内部のファイルを指すURLを返すgetResource(String)方法。あなたの場合は、file:/Users/ismar.slomic/src/ADOP/example/target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar!/my-example.propertiesのようなものです。そのURL上でgetFileを呼び出すと、内部のプロパティファイルへのポインタではなく、そのファイル(つまりjar)が返されます。

URLで接続を開き、返されたJarURLConnectionを使用することもできます。しかし、それはgetResourceAsStreamで簡単です。

+0

ありがとうございました!これはうまくいった。しかし、なぜ 'getResourceAsStream'が動作し、' getResource'がしなかったのか説明してください。彼らはまったく同じファイルパスを指していますが、唯一の違いはファイルの読み方です。 –

+0

ようこそ。回答は詳細で編集されました。 – YMomb

関連する問題