Bionix1441
Bionix1441

Reputation: 2319

loading a a csv file in a java program

When I try to run this benchmark java8-lambda-benchmark.

Using the comand:

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

I get the following error:

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

The file EmployeFile.java is the following:

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;
}

Now I print the value of fileURL I get null. The csv file is within the same folder as the sources for the project.

Upvotes: 1

Views: 159

Answers (2)

Bionix1441
Bionix1441

Reputation: 2319

when I use jar -xvf build/jar/LambdaMicrobench.jar I get this output

 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

These are correct as the project file structure is:

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

The solution is to change that field,load_employees.csv instead of lambdademo/load_employees.csv

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

Upvotes: 2

Kayaman
Kayaman

Reputation: 73538

The getResource() method works with the classpath. In your example classpath, you have only the jar, so whatever is outside of the jar can't be found.

You can either open up the jar with the zip tool of your choice and put the .csv file in, or an easier solution add the directory where the csv file is into the classpath with java -cp <directory>:build/jar/LambdaMicrobench.jar lambdademo.LambdaAvgExtraParallel 50000.

Note that the : separator needs to be ; if you're on windows.

Upvotes: 1

Related Questions