Blanca Hdez
Blanca Hdez

Reputation: 3563

Problem loading resources from classpath


In a web application running in a local tomcat, I am trying to load a folder /folder which is in tomcat/webapps/myproject/WEB-INF/folder To do it:

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder");  

Which returns null. This piece of code is supposed to load resources from classpath, which is if I am not wrong in the path where my folder is in.
Anyway, I moved my folder to different paths, such as tomcat/webapps/myproject/WEB-INF/classes/folder or tomcat/webapps/myproject/WEB-INF/lib/folder with the same result.
Did I miss something? Thanks in advance.

Regarding on all your answers (thanks), I edit my question with all I have tryed, with the same null result.
A)

 String realSource = getServletContext().getRealPath("/folder");  

B)

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder/fileInFolder"); 

C)

ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
String realSource = servletContext.getRealPath("/folder");

I must say that my folder path is tomcat/webapps/myproject/WEB-INF/classes/folder

Upvotes: 1

Views: 8052

Answers (3)

duffymo
duffymo

Reputation: 308753

The problem is that /WEB-INF is not in the CLASSPATH of a WAR file.

/WEB-INF/classes is in the CLASSPATH; so are all the JARs in /WEB-INF/lib.

If you put the file you need into /WEB-INF/classes the WAR class loader should be able to find it.

You cannot read a directory that way. It must be a file.

UPDATE:

Anyway, I moved my folder to different paths, such as tomcat/webapps/myproject/WEB-INF/classes/folder or tomcat/webapps/myproject/WEB-INF/lib/folder with the same result. Did I miss something?

Yes, you've missed two things:

  1. Your folder should go in one place - under /WEB-INF/classes
  2. You cannot access the folder using getResourceAsStream() and read all the contents. It doesn't work that way. You can only read one individual file that way.

Upvotes: 3

JoseK
JoseK

Reputation: 31371

If you want the full path of the folder within your webapp so that you can use File operations on it, here is a way to do it.

Note: myfolder is parallel to WEB-INF within my webapp

String relativeWebPath = "/myfolder";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);

Now printing out absoluteDiskPath gives me

D:\apache-tomcat-6.0.20\webapps\mywebapp\myfolder

Try listing files in that folder by

File dir = new File(absoluteDiskPath); 

String[] children = dir.list(); 
if (children != null) 
{ 

    for (int i=0; i<children.length; i++) { 
    // Get filename of file or directory 

    String filename = children[i]; 
    System.out.println("filename no " + i + filename);
    } 
}

Upvotes: 1

Suresh Kumar
Suresh Kumar

Reputation: 11767

Remove the leading slash and try i.e.

InputStream realPath = getClass().getClassLoader().getResourceAsStream("folder");  

Upvotes: 0

Related Questions