简体   繁体   English

从JDeveloper中的项目读取json文件-Oracle MAF项目

[英]Read json file from project in JDeveloper - Oracle MAF project

I am trying to read a json file into an Oracle MAF project written in JDeveloper 12c. 我正在尝试将json文件读入用JDeveloper 12c编写的Oracle MAF项目中。 In this kind of project, we have two main folders: ApplicationController and ViewController. 在这种项目中,我们有两个主要文件夹:ApplicationController和ViewController。 My java class is in the ApplicationController folder, and my resource is also there, in public_html folder. 我的Java类位于ApplicationController文件夹中,而我的资源也位于public_html文件夹中。 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 因此,java类的根基如下所示: \\ myProject \\ ApplicationController \\ adfmsrc \\ application \\ MyJavaClass.java

The json file is rooted like this: \\myProject\\ApplicationController\\public_html\\json\\myJsonFile.json json文件的根目录如下所示: \\ 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: 在我的java类中,我添加了如下函数,以便从项目中读取文件:

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(); 第java.lang.NullPointerException行错误: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. Utils类是Java类,其中包含上面的方法。

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. 在文件夹\\ myProject \\ ApplicationController \\ adfmsrc中,我添加了一个名为“ json”的新文件夹,并在其中添加了文件。

I went to project in JDeveloper, right click on it, then selected "Project Properties". 我去了JDeveloper中的项目,右键单击它,然后选择“项目属性”。 Click on "Compiler". 点击“编译器”。 On the field that has the label "Copy File Types to Output Directory:" I added the ".json" extension. 在带有标签“将文件类型复制到输出目录:”的字段上,我添加了“ .json”扩展名。

After that, I called in the function above, like this: Utils.loadJSONFromProject("../json/myJsonFile.json"); 之后,我像上面这样调用上面的函数: Utils.loadJSONFromProject(“ ../ json / myJsonFile.json”); and it worked like a charm. 它就像一种魅力。

Thank you and I hope my answer will be helpful for others, too. 谢谢,我希望我的回答对其他人也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM