-1

I am trying to read javascript file contents from Java code. Here is my code:

 String javascript = "";
 URL url = getClass().getResource("/elementController.js");
 System.out.println(url.toString());
 try {
     Scanner sc = new Scanner(new File(url.getPath()));
     while (sc.hasNext()) {
         javascript += sc.nextLine() + "\n";
     }
     sc.close();

     } catch (Exception e) {
         e.printStackTrace();
     }

System.out.println prints the following: file:/C:/Users/J%c3%bcesse/IdeaProjects/JavaFxProject/target/classes/elementController.js

As a result of this code execution I get the following stacktrace:

java.io.FileNotFoundException: C:\Users\J%c3%bcesse\IdeaProjects\ThesisProject\target\classes\elementController.js (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at gui.WebViewWindow$MyBrowser.<init>(WebViewWindow.java:82)
at gui.WebViewWindow.display(WebViewWindow.java:58)

But when I go to the directory where the javascript file resides, I can see that it is there. Picture of the directory:

Directory structure

I don't know why I am getting this error, even though the file exists there. Any suggestions?

user207421
  • 305,947
  • 44
  • 307
  • 483
Omaja7
  • 129
  • 1
  • 3
  • 11

2 Answers2

2

The character ü is getting converted to %c3%bc in URL format. When you try to use that path as a regular file path for file input, however, instead of opening URL input stream, the reverse decoding of %c3%bc doesn't happen, and the file names don't match, hence the FileNotFoundException.

kshetline
  • 12,547
  • 4
  • 37
  • 73
  • Altough the @EJP answer was a solution I also upvote this one too, because of explanation of conversion of unusual characters. Thank you for your help. – Omaja7 Apr 18 '18 at 23:35
1

A URL is not a filename, and a resource is not a file. You can't use FileInputStream on it for both reasons. Once the resource is packaged into your JAR file the whole scheme will collapse. You need to use the resource's input stream:

URL url = getClass().getResource("/elementController.js");
System.out.println(url.toString());
try {
    Scanner sc = new Scanner(url.openStream());

As you're only reading lines, there is no good reason to use a Scanner actually: you may as well use a BufferReader.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Yeah. This is a good explanation. I managed to get it working by using full path with new File("full path"), but I also wanted to know about packaging (distribution of my jar). This code snippet seems to do the trick. I guess I need to make this file system stuff more clear to me. – Omaja7 Apr 18 '18 at 23:32
  • It's as simple as this: a JAR file isn't a file system. – user207421 Apr 18 '18 at 23:34
  • @EJP With the path api, it is. – killjoy Apr 19 '18 at 00:17
  • @killjoy And with the API the OP is using it isn't. – user207421 Apr 19 '18 at 00:51