2017-07-17 116 views
1

我正在從另一個jar文件執行jar文件。當從子jar中訪問資源文件時,它給出異常: java.io.FileNotFoundException:文件'file:PATH_TO_CHILD_JAR/example-1.0.0-0-SNAPSHOT-jar -with-dependencies.jar!resource_file_path/abs.sql.tpl ' 不存在。從另一個jar文件執行jar文件

我無法確定它爲什麼要「!」在搜索資源文件時標記。

+0

請檢查一次:https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar#20389418 –

回答

0

試試這個

// Run a java app in a separate system process 
Process proc = Runtime.getRuntime().exec("java -jar A.jar"); 
// Then retreive the process output 
InputStream in = proc.getInputStream(); 
InputStream err = proc.getErrorStream(); 
0

您可以在Java程序與執行的jar文件。

請確保您已在具有Main-Class屬性的jar中添加了Manifest文件。

我的步驟和輸出:主類:com.demo.TestJar

創建的測試Java程序:

package com.demo; 
    public class TestJar extends Object { 
    public static void main(String args[]) { 
    System.out.println("TestJar"); 
    } 
} 

包的

創建了以下行清單文件jar:jar cvfm /home/user/TestJarOne.jar manifest.txt

寫測試程序:

import java.io.BufferedInputStream; 
import java.io.IOException; 

public class TestJSS extends Object { 

static int i = 0; 

public static void main(String args[]) throws IOException, InterruptedException { 
    System.out.println("Calling jar"); 
    Process p = Runtime.getRuntime().exec("java -jar /home/user/TestJarOne.jar arg1 arg2"); 
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream()); 
    synchronized (p) { 
     p.waitFor(); 
    } 
    System.out.println(p.exitValue()); 
    int b=0; 
    while((b=bis.read()) >0){ 

     System.out.print((char)b);  
    }   
    System.out.println(""); 
    System.out.println("Called jar"); 
    } 

}