I am trying to read a json file into an Oracle MAF project written in JDeveloper 12c. In this kind of project, we have two main folders: ApplicationController and ViewController. My java class is in the ApplicationController folder, and my resource is also there, in public_html folder. I tried everything I found online, but nothing seems to work for me...
So the java class is rooted like this: \myProject\ApplicationController\adfmsrc\application\MyJavaClass.java
The json file is rooted like this: \myProject\ApplicationController\public_html\json\myJsonFile.json
In my java class I added a function like the following, in order to read the file from project:
public static String loadJSONFromProject(String filename) {
String json = "";
try {
InputStream is = Utils.class.getResourceAsStream(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (Exception ex) {
ex.printStackTrace();
}
return json;
}
The way I tried to call the method is like this:
Utils.loadJSONFromProject("/json/myJsonFile.json");
Utils.loadJSONFromProject("json/myJsonFile.json");
Utils.loadJSONFromProject("../json/myJsonFile.json");
When I run the project, I get a
java.lang.NullPointerException error on line : int size = is.available();
That means that the file wasn't found. The Utils class is the java class that contains the method from above.
Please help me with this...
Thanks in advance.
I managed to make it work like this:
In the folder \myProject\ApplicationController\adfmsrc I added a new folder called "json" where I added my file.
I went to project in JDeveloper, right click on it, then selected "Project Properties". Click on "Compiler". On the field that has the label "Copy File Types to Output Directory:" I added the ".json" extension.
After that, I called in the function above, like this: Utils.loadJSONFromProject("../json/myJsonFile.json"); and it worked like a charm.
Thank you and I hope my answer will be helpful for others, too.