0

I am writing webApp that use jsp file to call java class directly to get results.

The JSP file is like:

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

In java classes the files are accessed through:

BufferedReader br = new BufferedReader(new FileReader("WebContent/StopWords/stop-words-english1.txt"));

and

private InputStream modelInputT = new FileInputStream("WebContent/OpenNLP_Models/en-token.bin");

The tomcat cannot find the referenced files and someone said use getResourceAsStream, but that is for servlets. I call the java class directly without any servlets.

private InputStream modelInputT = = this.getClass().getClassLoader().getResourceAsStream("WebContent/OpenNLP_Models/en-token.bin");

This cause java class cannot find the file as well. Need help and how to make changes to these java classes?

GeorgeG
  • 11
  • 1
  • 6
  • Where is WebContent directory Inside you WAR ? Looks like your relative path is wrong. Other option is to use absolute path to the file. – Bimalesh Jha Mar 03 '14 at 12:25
  • getResourceAsStream works only if the resource you are trying to load is inside in the classpath, that is inside a jar or WEB-INF/classes in your case. If the resource is there, so you should check the package/folder structure which contains it: WEB-INF/classes/OpenNLP_Models/en-token.bin implies getResourceAsStream("OpenNLP_Models/en-token.bin");. – robermann Mar 03 '14 at 12:26
  • @BimaleshJha I changed into '/Users/XX/XX/Eclipse/App/WebContent/StopWords/stop-words-english1.txt' still not working on Mac – GeorgeG Mar 03 '14 at 12:29
  • @robermann I don't have any classes in web-inf and the en-token.bin is located in a folder within WebContent... How to make changes... – GeorgeG Mar 03 '14 at 12:31
  • Yours isn't it a web app? Anyway, check your classpath in any case: if it does not contain the resource, you must use a file system path. – robermann Mar 03 '14 at 12:34
  • new java.io.File(".").getAbsoluteFile() gives you the current execution directory path. – robermann Mar 03 '14 at 12:35

1 Answers1

1

The tomcat cannot find the referenced files ...

That is because the pathnames are incorrect unless the JVM's current directory is the parent directory of the "WebContent" directory. When you use FileInputStream to open a file, relative pathname are resolved relative to the current directory of the JVM when it was launched.

... and someone said use getResourceAsStream, but that is for servlets.

No. That's not correct either. That method is not "for servlets". The purpose of that method is to open a resource that is on the class / classloader's classpath. If your "WebContent/StopWords/stop-words-english1.txt" is in the webapp's "/WEB-INFO/classes" or in a JAR file in "/WEB-INFO/lib", then getResourceAsStream will find it.


In your case, it seems like you are talking about the "WebContent" directory that corresponds to the default servlet.

In that case, read this Q&A - How can I get real path for file in my WebContent folder?.

So if you are trying to access those files from within a JSP, it seems as if you should be writing this:

  new FileReader("WebContent/StopWords/stop-words-english1.txt")

as this:

  new FileReader(getServletContext().getRealPath(
          "/StopWords/stop-words-english1.txt"))
Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I don't have 'classes' folder in WEB-INF, So I created one. and use this.getClass().getClassLoader().getResourceAsStream("/WebContent/WEB-INF/classes/OpenNLP_Models/en-token.bin"); to access the file. And this failed. The JSP file was used to call the java class only. The getServletContext won't work in java class. It has to be in servlet. – GeorgeG Mar 03 '14 at 13:18
  • @GeorgeG - You are not making much sense. 1) If you put "OpenNLP_Models/en-token.bin" into "/WebContent/WEB-INF/classe‌​s", then the pathname you should give to `getResourceAsStream` is "/OpenNLP_Models/en-token.bin". 2) If you need to open the file in some class that isn't a Servlet, then change your code so that you call `getServletContext().getRealPath` in a servlet method and pass the resulting pathname to the class that needs it. (Duh!) – Stephen C Mar 03 '14 at 14:07
  • 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