1

I have a maven java ee application

enter image description here

Path to the file is

~/Work/appname/WebContent/resources/json/menu.json

After deploying on glassfish file located here:

~/Tools/glassfish4/glassfish/domains/domain1/applications/appname/resources/json/menu.json

enter image description here In this file stores menu in json format. I need to do basic crud (create, read, update, delete) operations with this file.

I've tried to get file like this:

InputStream inputStream  = ModulMenuUtil.class.getClassLoader().getResourceAsStream("/menu.json");

I get a NullPointerException. I don't know how to get path to this file. Please help me.

Alex Belke
  • 1,873
  • 4
  • 15
  • 23
  • I also try InputStream inputStream = ModulMenuUtil.class.getClassLoader().getResourceAsStream("json/menu.json"); Don't work also. – Alex Belke Oct 06 '17 at 18:29

1 Answers1

1

Make sure your json folder and the file are placed under src/main/resources.

Try the following:

this.getClass().getClassLoader().getResourceAsStream("json/menu.json");

or (not recommended)

Thread.currentThread().getContextClassLoader().getResourceAsStream("json/menu.json");

If both methods don't work you can also try to put the file in the root of your src folder.

See also:

unwichtich
  • 13,143
  • 2
  • 46
  • 60
  • I created a Util class with static methods. And this solution doesn't work for static methods. What can you do with static methods? – Alex Belke Oct 09 '17 at 07:28