0

I am using tomcat as a web server to deploy my webApp.

The jsp file calls one of the java class to retrive a list of content. The jsp code is like:

 <%
    String queryKey = request.getParameter("id");
    int jobID = Integer.parseInt(queryKey);
    out.println(jobID);
    ArrayList<Integer> myTopList = JobRecByBoWJaccard.topJobsByBoW(jobID);
%>

In my java class, I used a txt file as reference to remove stop words. The code in java class is like:

public TermsExtraction() {
    try {
        BufferedReader br = new BufferedReader(new FileReader(
                "WebContent/StopWords/stop-words-english1.txt"));
        for (String line; (line = br.readLine()) != null;) {
            this.stopWords.add(line.trim());
        }
        // System.out.println(this.stopWords);
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This file was called within the class's constructor.

But, when the tomcat is loaded, the error message was given as:

java.io.FileNotFoundException: WebContent/StopWords/stop-words-english1.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at java.io.FileReader.<init>(FileReader.java:58)
    at demoApp.BOW.TermsExtraction.<init>(TermsExtraction.java:28)
    at com.demoApp.jaccardRec.JobInput.inputJobForTermExtract(JobInput.java:21)
    at com.demoApp.jaccardRec.JobRecByBoWJaccard.topJobsByBoW(JobRecByBoWJaccard.java:24)
    at org.apache.jsp.Result_jsp._jspService(Result_jsp.java:116)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
    etc...

It seems that the file been referenced canot be found by tomcat. HELP...

GeorgeG
  • 11
  • 1
  • 6
  • 1
    Same Question?: http://stackoverflow.com/questions/1768270/how-do-i-access-a-text-file-from-within-my-war – pL4Gu33 Feb 28 '14 at 15:50
  • That's a relative path. Which directory are you executing from, and does the path exist from that point? If you're on a non-Windows system, paths and filenames may be case-sensitive; check that too. – keshlam Feb 28 '14 at 15:50
  • @keshlam I changed into '/Users/XX/XX/Eclipse/App/WebContent//StopWords/stop-words-english1.txt' still not working – GeorgeG Feb 28 '14 at 15:57
  • What operating system? – keshlam Feb 28 '14 at 15:58
  • @keshlam mac maverick – GeorgeG Feb 28 '14 at 15:59
  • Hi, get the file as stream using context object. check the similar issue here http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream – Naveen Ramawat Feb 28 '14 at 16:00
  • use this **/StopWords/stop-words-english1.txt** rather **_WebContent/StopWords/stop-words-english1.txt_** – Ashish Ratan Mar 01 '14 at 17:06
  • @NaveenRamawat context seems only works in servlet. I am calling java class directly through jsp page without any servlet. How to change the java class to use getResourceAsStream? – GeorgeG Mar 03 '14 at 12:27
  • @GeorgeG Just posted an answer with code. I hope it would be helpful. – Naveen Ramawat Mar 03 '14 at 13:14
  • It solved by using InputStream modelInput = this.getClass.getResourceAsStream("en-token.bin"); And add the en-token.bin to the same package of the java class. – GeorgeG Mar 03 '14 at 17:01

3 Answers3

1

If you want to get the ServletContext outside the servlet than create a ContextListner class and inside this class store the context object when it being initialize and after you can get it anywhere in application using a static method. I am pasting an example of ContextListener

package com.y;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class ContextListener implements ServletContextListener {
    private static ServletContext servletContext;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        servletContext = arg0.getServletContext();
    }

    public static ServletContext getServletContext(){
        return servletContext;
    }
}

Here you also have to give the entry of this listner in to the web.xml like below

<listener>
    <listener-class>com.y.ContextListener</listener-class>
</listener>

And now you can read any file using the getResourceAsStream method of this Context as I mentioned in my earlier comment.

Naveen Ramawat
  • 1,425
  • 1
  • 15
  • 27
  • It solved by using InputStream modelInput = this.getClass.getResourceAsStream("en-token.bin"); And add the en-token.bin to the same package of the java class. – GeorgeG Mar 03 '14 at 16:59
0

I think you need to pass the absolute path.

sergiu
  • 389
  • 1
  • 7
  • I changed into '/Users/XX/XX/Eclipse/App/WebContent//StopWords/stop-words-english1.txt' still not working. – GeorgeG Feb 28 '14 at 15:58
  • It should be the tomcat directory. ../tomcat/webapps/App...but you don't need to hardcode the path. Try ServletContext.getResourceAsStream("yourfile") or String path = context.getRealPath("/")+"yourfile"; – sergiu Feb 28 '14 at 16:05
  • try { ServletContext context; BufferedReader br = new BufferedReader(context.getResourceAsStream("/StopWords/stop-words-english1.txt")); for (String line; (line = br.readLine()) != null;) { this.stopWords.add(line.trim()); } // System.out.println(this.stopWords); br.close(); } catch (IOException e) { e.printStackTrace(); } – GeorgeG Feb 28 '14 at 16:12
0

You could try with "./StopWords/stop-words-english1.txt" - the "WebContent" might be redundant. Also you can get the current working directory using the below command and construct your path accordingly.

System.getProperty("user.dir"));
TR1
  • 323
  • 1
  • 9