webExplorer
webExplorer

Reputation: 1025

Read a .json file in portlet

I want to read a static json file (for testing) through portlet.

I've placed it in /json folder. So when the portlet is deployed, the folder is available in \webapps folder. The complete path: webapps\myPortlet\json\op.json

In the processAction method -

@Override
public void processAction(ActionRequest request, ActionResponse response)
        throws PortletException, IOException {
    JSONParser parser = new JSONParser();
    try {
            JSONObject jsonObject = (JSONObject) parser.parse(new FileReader
            (request.getContextPath() + "/json/op.json"));

.
.
.
}

I'm getting FileNotFoundException.

Can someone please point me a correct way to access this file.

Upvotes: 1

Views: 1980

Answers (3)

Shivam Aggarwal
Shivam Aggarwal

Reputation: 805

Firstly you should try including the file or the folder altogether in src directory along with the file accessing it.

 URL url = getClass().getResource("op.json");

Use sops to ensure that there are no special characters in the file path(no spaces in folder names,which are substituted for special characters).Hence forth,you can access the file.

Upvotes: 1

yannicuLar
yannicuLar

Reputation: 3133

Most likely you are using separators the wrong way, resulting in a wrong path. You could check this by debugging or even logging.

Try getting the actual file's full path from your computer's explorer, and compare it with the path you are creating on runtime

About using the Separator, you can try something like this:

    String SEPARATOR = System.getProperty("file.separator");

    ServletContext ctxt = FacesService.getHttpServletRequest().getSession().getServletContext();
    String filepath = ctxt.getRealPath("json"+SEPARATOR+"op.json");
    System.out.println("File path = "+ filepath);

Upvotes: 1

Raxa
Raxa

Reputation: 382

First of all you can help yourself with some nice System.out's (but please remove it afterwards, so it does not pollute the logfile).

If you print this information

System.out.println( "request.getContextPath(): " + request.getContextPath() );
System.out.println( "new File(request.getContextPath()) : " + new File(request.getContextPath()).getAbsolutePath()  );

you will find a little surprise. More information you will gain from

System.out.println( "new File() : " + new File().getAbsolutePath()  );

If you really need to load a static file you should ask yourself why you are doing this and what you will get from it. Don't programm some stuff you don't need later on. Maybe you can find a better solution - even a better (automatically) testable solution.

If you really need a static file (like an image) you can place it into a jar near your Class:

@see How to get a path to a resource in a Java JAR file

If you need the content file as some kind of configuration file you should rethink your architecture and find the path to the file from some configurable parameters (like from your web.xml).

Remember: don't build something you don't need. Don't waste your time with something you will remove later on what is integrated in your software. Try to get rid of dependencies - even for testings.

Upvotes: 2

Related Questions