2015-05-29 43 views
1

當我嘗試運行這個基準java8-lambda-benchmark在java程序中加載一個csv文件

使用COMAND:

java -cp build/jar/LambdaMicrobench.jar lambdademo.LambdaAvgExtraParallel 50000 

我得到以下錯誤:

lambdademo/load_employees.csv: Not found in jar 
java.lang.NullPointerException 
at lambdademo.EmployeeFile.loadEmployeeList(Unknown Source) 
at lambdademo.LambdaAvgExtraParallel.main(Unknown Source) 
Exception in thread "main" java.lang.NullPointerException 
at lambdademo.EmployeeFile.loadEmployeeList(Unknown Source) 
at lambdademo.LambdaAvgExtraParallel.main(Unknown Source 

文件EmployeFile.java如下:

public class EmployeeFile { 

private static final String EMPLOYEE_FILE = "lambdademo/load_employees.csv"; 

public LinkedList<EmployeeRec> loadEmployeeList() { 
    LinkedList<EmployeeRec> employeeList = new LinkedList<>(); 
    BufferedReader br = null; 
    try { 
     URL fileURL = getClass().getClassLoader().getResource(EMPLOYEE_FILE); 
      if (fileURL == null) { 
        System.out.println("resource is null"); 
      } 
      InputStream in = fileURL.openStream(); 
      br = new BufferedReader(new InputStreamReader(in));    
       } catch (Exception e) { 
     System.out.println(EMPLOYEE_FILE + ": Not found in jar"); 
     e.printStackTrace(); 
    } 

    try { 
     String line; 
     while ((line = br.readLine()) != null) {  
      String[] rec = null; 
      rec = line.split(","); 
      employeeList.add(new EmployeeRec(rec[0], rec[1], rec[2], 
        rec[3], rec[4], rec[5])); 
     } 
    } catch (IOException e) { 
     System.out.println("Error reading " + EMPLOYEE_FILE); 
     e.printStackTrace(); 
    } 
    return employeeList; 
} 

現在我打印的價值fileURL我得到空。 csv文件與項目的來源位於同一文件夾內。

+1

喲喲你創建'LambdaMicrobench.jar'(例如使用IDE,Maven,螞蟻......)?你可以確認'.csv'是否在正確的位置,在'.jar'(一個'.jar'是'.zip'文件,只需使用7zip/unzip/tar或類似的工具來解壓它的內容) ? – Pyranja

+0

我用ant編譯:'ant -buildfile build.xml' – Bionix1441

回答

1

getResource()方法使用類路徑。在你的示例類路徑中,你只有jar,所以無法找到jar外的任何東西。

您可以使用您選擇的壓縮工具打開jar文件並將.csv文件放入,或者更簡單的解決方案將csv文件所在的目錄添加到java -cp <directory>:build/jar/LambdaMicrobench.jar lambdademo.LambdaAvgExtraParallel 50000的類路徑中。

請注意,如果您使用的是windows,則:分隔符需要爲;

+0

csv文件在這個文件夾下src/lambdademo但是使用這個命令我仍然得到和以前一樣的錯誤'java -cp src/lambdademo:build/jar/LambdaMicrobench .jar lambdademo.LambdaAvgExtraParallel 50000' – Bionix1441

+0

只需使用'src','lambdademo'部分似乎已經在'EMPLOYEE_FILE'中定義了。 – Kayaman

2

當我使用jar -xvf build/jar/LambdaMicrobench.jar 我得到這個輸出

created: META-INF/ 
inflated: META-INF/MANIFEST.MF 
created: lambdademo/ 
inflated: lambdademo/Constants.class 
inflated: lambdademo/EmployeeFile.class 
inflated: lambdademo/EmployeeRec.class 
inflated: lambdademo/LambdaAvgExtraParallel.class 
inflated: lambdademo/LambdaAvgExtraSerial.class 
inflated: lambdademo/LambdaAvgParallel.class 
inflated: lambdademo/LambdaAvgSerial.class 
inflated: lambdademo/OldSchoolAvg.class 
inflated: lambdademo/OldSchoolAvgExtra.class 
inflated: load_employees.csv 

這些都是正確的,因爲項目文件的結構是:

--LambdaMicrobench 
----load_employees.csv 
----lambdademo 
------*.class 

解決的辦法是改變load_employees.csv代替lambdademo/load_employees.csv

那場,
private static final String EMPLOYEE_FILE = "load_employees.csv"; 
+1

是的,它應該。 – Kayaman

+1

如果csv在jar的根目錄下,那麼路徑應該是String EMPLOYEE_FILE =「load_employees.csv」;爲什麼你參考文件夾,如果它不在裏面? –