0

I have problem with paths in my servlet web application. I'm using java to code the business logic. The problem is that when I run the java code as a java application with main method, I use the relative path to data files dataFiles/myData.json and it works fine. However, when I run it using Tomcat I have to put the full path from my hard drive ../../../../Documents/workspace/MyApp/dataFiles/myData.json

My directory structure in workspace is:

MyApp 
 |- src
     |-Pkg1
        |- some java files and servlet files
     |-Pkg2
        |- java files
 |-dataFiles
    |- all data files
 |-WebContent
    |- jsp pages
    |- css
    |- js
    |- WEB-INF
    |- META-INF

The question is: Now I need to create .war file to deploy my application. What is the correct path that I should put before generate war file?

F. Fo
  • 113
  • 6
  • 18
  • If you put your dataFiles folder in your web app's WEB-INF/classes folder, then you could use InputStream dataStream = getClass().getResourceAsStream("/dataFiles/myData.json"); – rickz Apr 28 '16 at 00:07
  • WEB-INF does not have a classes folder. Also, the dataFiles is used inside Java files not the servlet files. – F. Fo Apr 28 '16 at 00:20
  • The classes folder is the standard place to put class files. Are your class files in a jar? Anyway, the method I suggested will work. Just do some googling to see examples. – rickz Apr 28 '16 at 01:22
  • http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream – rickz Apr 28 '16 at 01:25
  • 1
    http://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-configuration-resource-files-in-servlet-based-app – rickz Apr 28 '16 at 01:31
  • http://stackoverflow.com/questions/14089146/file-loading-by-getclass-getresource – rickz Apr 28 '16 at 01:36
  • http://www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading – rickz Apr 28 '16 at 01:39
  • I tried the method you suggested. I put the dataFiles under the classes folder and I use the getResource, but it return null. Do you have any idea why it return null instead of the file URL? – F. Fo Apr 28 '16 at 20:14
  • My code was supposed to create a Stream. Did you try BalusC idea in the second link that I posted? Where is the class file that calls for the resource? – rickz Apr 28 '16 at 21:23
  • Yes I tried it but still shows null. – F. Fo Apr 28 '16 at 21:45
  • I used the post below (n01dea's post) and it works fine. Thank you for your reply and help I really really appreciate it. I can't figure out way this method doesn't work for me. – F. Fo Apr 28 '16 at 22:26

1 Answers1

0

Try this within a Servlet:

ServletContext context = getContext();
URL url = context.getResource("/dataFiles/myData.json");
n01dea
  • 1,414
  • 13
  • 31
  • I'm using the data files inside the java files not the servlet files. `parser.parse(new FileReader(../../../../Documents/workspace/MyApp/dataFiles/myData.json));` – F. Fo Apr 27 '16 at 23:52
  • And just passing the url from a servlet to the java file? Somewhere you need to invoke your java file in your web application : ) – n01dea Apr 28 '16 at 00:01
  • Do you mean it is better if I send the file paths to Java code from servlet when I invoke methods and classes? – F. Fo Apr 28 '16 at 00:16
  • I works for me. I create a global variable in servlet (contextPath) that hold the context real path. Then, I use contextPath variable each time I need to get a file in addition to file path. `ServletContext servletContext = getServletContext(); contextPath = servletContext.getRealPath(File.separator);` – F. Fo Apr 28 '16 at 22:29