2017-02-10 15 views
2

jarアーカイブにあるファイルを開こうとしています。jarファイルを読む

私はこのMavenプラグインを使用し

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>3.0.0</version> 
    <configuration> 
     <archive> 
     <manifest> 
      <addClasspath>true</addClasspath> 
      <mainClass>com.fatec.migration.script.utils.Script</mainClass> 
     </manifest> 
     </archive> 
     <descriptorRefs> 
     <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
    </configuration> 
    <executions> 
     <execution> 
     <id>assemble-all</id> 
     <phase>package</phase> 
     <goals> 
      <goal>single</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 

私が構築し、その後、私のアーカイブをコピーします。

cp /home/stephane/dev/java/projects/fatec/Fiabilisation_Controles_XP_AS/target/fatec-script-jar-with-dependencies.jar . 

今、私はそれを実行してみてくださいすることができます

java -jar fatec-script-jar-with-dependencies.jar 1 2017-02-01 2017-02-02 all 

ああ、それができませんファイルを見つける:

The file secure/extrapack-renew.sql could not be opened. 
java.lang.NullPointerException 
    at com.fatec.migration.script.utils.AbstractScript.loadSqlStatement(AbstractScript.java:75) 

しかし、ファイルが存在しない、私はアーカイブにそれを見ることができます。

protected String loadSqlStatement(String scriptPath, String filename) { 
    String filepath = buildSqlFilePath(scriptPath, filename); 
    try { 
     return new String(Files.readAllBytes(Paths.get(getClass().getResource(filepath).toURI()))); 
    } catch (Exception e) { 
     System.err.println("The file " + filepath + " could not be opened."); 
     e.printStackTrace(); 
    } 
    return null; 
} 

private String buildSqlFilePath(String scriptPath, String filename) { 
    return scriptPath + "/" + filename; 
}  

は、scriptPathは、「安全な」で、ファイル名:ここでは

$ jar -tvf target/fatec-script-jar-with-dependencies.jar | grep "secure/extrapack-renew.sql" 
    1844 Fri Feb 10 17:43:46 CET 2017 secure/extrapack-renew.sql 

は、私は、ファイルを開いてみてください方法です"extrapack-renew.sql"です。

私には何が欠けていますか?

答えて

1

私はあなたの問題はあなたがgetResource()を使用する方法であると思う:

Paths.get(getClass().getResource(filepath).toURI()); 

あなたは"extrapack-renew.sql"ファイルを取得するために(つまり、現在のクラスの場所に相対的である)相対クラスパスを使用します。
このリソースは、検索するためにjarのこのパスの内側に配置する必要があります。

リソースが現在のクラスパスの内側に配置されていない場合は、リソースを取得するために使用されるパスは、リソースの絶対名を指定するために"/"文字で始める必要があります。もちろん

Paths.get(getClass().getResource("/"+filepath).toURI()); 

mavenを使用する場合、"/secure/extrapack-renew.sql"がクラスパス内のリソースになるように、extrapack-renew.sql

src/main/resources/secureソースプロジェクトのフォルダにある必要があります。

+0

SQLファイルはjarファイル内にあります。移動や変更は想定されていません。ハードコーディングされているとしましょう。たぶん、私は外部ファイルでそれを持つ必要はありません、リソースファイルではるかに少ない。 – Stephane

+1

実際には、jarがパッケージ化された後にファイルを変更する必要がない場合、jarからファイルを移動する義務はありません。むしろ展開の選択肢です。 – davidxxx

0

解決方法は、質問の元のタイトルで尋ねられた内容とは異なり、jarアーカイブにプロパティファイルを持たないことでした。

プロジェクトのホームディレクトリにプロパティファイルを置いて動作させました。

$ ll 
total 52K 
-rw-rw-r-- 1 stephane 10 févr. 13 10:49 application.properties 
-rw-r--r-- 1 stephane 6,1K févr. 10 17:22 pom.xml 

私はこのファイルを開くと、そのプロパティを読み込むことができます。その後、私は.jarアーカイブを移動することができますし、プロパティを別のディレクトリにファイル、およびアプリケーションを実行

properties.load(new FileInputStream(new File(DB_AUTOSELF_PROPERTIES_FILENAME))); 

$ pwd 
/home/stephane/trash 
$ cp ~/dev/java/projects/AS/target/script-jar-with-dependencies.jar . 
$ cp ~/dev/java/projects/AS/application.properties . 
$ vi application.properties 
-rw-rw-r-- 1 stephane 10 févr. 13 10:53 application.properties 
java -jar script-jar-with-dependencies.jar 1 2016-12-01 2017-02-12 all 

私のpom.xmlファイルには、リソースをコピーして脂肪を作成するためのプラグインが含まれていますjarアーカイブ:

<plugin> 
    <artifactId>maven-resources-plugin</artifactId> 
    <executions> 
     <execution> 
     <id>copy-resources</id> 
     <phase>validate</phase> 
     <goals> 
      <goal>copy-resources</goal> 
     </goals> 
     <configuration> 
      <outputDirectory>${basedir}/target</outputDirectory> 
      <resources> 
      <resource> 
       <directory>src/main/resources</directory> 
       <filtering>true</filtering> 
      </resource> 
      </resources> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>3.0.0</version> 
    <configuration> 
     <archive> 
     <manifest> 
      <addClasspath>true</addClasspath> 
      <mainClass>com.fatec.migration.script.utils.Script</mainClass> 
     </manifest> 
     </archive> 
     <descriptorRefs> 
     <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
    </configuration> 
    <executions> 
     <execution> 
     <id>assemble-all</id> 
     <phase>package</phase> 
     <goals> 
      <goal>single</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 
関連する問題