4

Hi i am running java app from jar file. like following java -cp test.jar com.test.TestMain . in the java app i am reading csv file. which is throwing below exception.

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.

code causes to exception is

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())) {

thanks.

Harinath
  • 105
  • 1
  • 4
  • 12

3 Answers3

3

test.jar!\us_postal_codes.csv (The filename, directory name, or volume label syntax is incorrect)

Would suggest using

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

and

getResourceAsStream("/us_postal_codes.csv") // if its inside a jar
Naman
  • 27,789
  • 26
  • 218
  • 353
2

Based on the stack trace below we can see that the Scanner cannot find the file:

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

By the way, where is the file? If it's in the jar, then you can use TestMain.class.getResourceAsStream() - Scanner has an InputStream constructor too:

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)) {
    //...
}
//...
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
  • csv file is in jar file. in the root. but still getting exection. `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 May 17 '16 at 08:03
  • HI its working fine, i just put '/' to file name like this. "/us_postal_codes.csv". thanks @tamas rev – Harinath May 17 '16 at 08:09
1

You should use getResourceAsStream. This is example:

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 method is not used to search files in .jar archives.

hubot
  • 393
  • 5
  • 18