2016-05-17 105 views
3

嗨,我從jar文件運行java應用程序。如下面的java -cp test.jar com.test.TestMain。在java應用程序中,我正在閱讀csv文件。這是拋出異常。java.io.FileNotFoundException從jar文件運行java應用程序

java.io.FileNotFoundException: file:\C:\Users\harinath.BBI0\Desktop\test.jar!\us_postal_codes.csv (The filename, directory name, or volume label syntax is incorrect) 
     at java.io.FileInputStream.open(Native Method) 
     at java.io.FileInputStream.<init>(FileInputStream.java:146) 
     at java.util.Scanner.<init>(Scanner.java:656) 
     at com.test.TestMain.run(TestMain.java:63) 
     at com.test.TestMain.main(TestMain.java:43) 

*csv file is located in src/main/resources folder. 

代碼導致的例外是

public static void main(String[] args) throws Exception { 
    TestMain trainerScraper = new TestMain(); 
    trainerScraper.run(); 
} 

private void run() throws JsonParseException, JsonMappingException, IOException{ 
    String line = ""; 
    String cvsSplitBy = ","; 

    //Get file from resources folder 
    ClassLoader classLoader = getClass().getClassLoader(); 
    System.out.println(csvFile); 
    URL url = classLoader.getResource("us_postal_codes.csv"); 
    String fileName = url.getFile(); 
    File file = new File(fileName); 

    try (Scanner scanner = new Scanner(file)) { 
     line = scanner.nextLine();   //header 
     while ((scanner.hasNextLine())) { 

感謝。

+0

你能分享更多來自TestMain.java的代碼請 – nullpointer

+0

這兩行是什麼'at com.test.TestMain.run(TestMain.java:63) at com.test.TestMain.main(TestMain.java: 43)'你們班上? – nullpointer

+1

將'src/main/resources'放入類路徑 – Sanjeev

回答

2

基於堆棧跟蹤下面我們可以看到,掃描儀無法找到文件:

at java.util.Scanner.<init>(Scanner.java:656) 
at com.test.TestMain.run(TestMain.java:63) 

順便說一句,這裏是什麼文件?如果是在罐子裏,然後你可以使用TestMain.class.getResourceAsStream() - ScannerInputStream構造太:

InputStream iStream = TestMain.class.getResourceAsStream("/us_postal_codes.csv"); // this supposes the csv is in the root of the jar file 
try (Scanner scanner = new Scanner(iStream)) { 
    //... 
} 
//... 
+0

csv文件在jar文件中。在根。但仍然得到緩解。 java.lang.NullPointerException at java.io.Reader。 (Reader.java:78) at java.io.InputStreamReader。 (InputStreamReader.java:72) at java.util.Scanner。 (Scanner.java:608)' – Harinath

+0

其工作正常,我只是把'/'這樣的文件名。 「/us_postal_codes.csv」。謝謝@tamas rev – Harinath

+0

@Harinath根據你的回答更新了代碼:) –

4

Test.jar的\ us_postal_codes.csv(文件名,目錄名或卷 標籤語法不正確! )

建議使用

System.getProperty("user.dir") // to get the current directory, if the resource is in the project folder 

getResourceAsStream("/us_postal_codes.csv") // if its inside a jar 
+0

感謝它的工作。小變化getResourceAsStream(「/ us_postal_codes.csv」) – Harinath

+0

@海狸:我的壞,改進 – nullpointer

+0

@海狸:如果這解決了你的問題,你可以請選擇它作爲答案 – nullpointer

1

您應該使用getResourceAsStream。例如:

public void test3Columns() throws IOException 
{ 
    InputStream is = getClass().getResourceAsStream("3Columns.csv"); 
    InputStreamReader isr = new InputStreamReader(is); 
    BufferedReader br = new BufferedReader(isr); 
    String line; 
    while ((line = br.readLine()) != null) 
    { 
    CSVLineTokenizer tok = new CSVLineTokenizer(line); 
    assertEquals("Should be three columns in each row",3,tok.countTokens()); 
    } 
    br.close(); 
    isr.close(); 
    is.close(); 
} 

ClassLoader.getResource方法不用於在.jar歸檔中搜索文件。