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:
I don't know why I am getting this error, even though the file exists there. Any suggestions?