1

I am novice at Java deployment and now using JSON file to store some data and use new FileReader(file name) to call the JSON and parse and do further processing.

Problems:

1) It shows file not found, if I provide only file name in new FileReader(file name)

Object obj = parser.parse(new FileReader("file.json"));

JSONArray arr = (JSONArray)obj;

Iterator i = arr.iterator();

Here is the stacktrace:

java.io.FileNotFoundException: file.json (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at java.base/java.io.FileReader.<init>(Unknown Source)
at com.login.servlet.CheckAvailability.doGet(CheckAvailability.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Unknown Source)

If I provide the absolute path it works fine but I would like to provide relative path. So that I don't have to keep changing paths on localhost and deployed server.

2) I am working on local and trying to deploy on server. for that I make .war file. and upon deploying its not showing those JSON files

Here are the screenshots attached for visualization.

look out for the file.json file on local host

look at the deployed folder where there is no file.json file (after making war file)

I tried looking at the questions but could not find the exact solution or one I can understand.

Please explain considering I am a novice.

ArifMustafa
  • 3,691
  • 4
  • 34
  • 44

1 Answers1

0

On an application server, the default path is either the path from which the server was started (i.e. the folder you were in when you launched the server) or the bin folder. So the directory and the files should be in it. A quick way to check what path you're in is printing or logging the result of f.getAbsolutePath(), this will tell you what folder the server is defaulting to.

You're opening the file normally (new File(...)) and not as a resource and therefore the resource path or class path are not used.

When dealing with application servers it is best to avoid using the default path as it changes easily. Either have a system property or equivalent that defines where the files will be searched for or, if you're shipping the file you want to read together with the application, put it inside the WAR and open it as a resource:

YourClass.class.getResourceAsStream("/...");

or

YourClass.class.getResource("/...");
Rohan Kadu
  • 1,151
  • 8
  • 19