java - Problem loading resources from classpath -
in web application running in local tomcat, trying load folder /folder
in tomcat/webapps/myproject/web-inf/folder
it:
inputstream realpath = getclass().getclassloader().getresourceasstream("/folder");
which returns null
. piece of code supposed load resources classpath, if not wrong in path folder in.
anyway, moved folder different paths, such tomcat/webapps/myproject/web-inf/classes/folder
or tomcat/webapps/myproject/web-inf/lib/folder
same result.
did miss something? in advance.
regarding on answers (thanks), edit question have tryed, 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 folder
path tomcat/webapps/myproject/web-inf/classes/folder
the problem /web-inf not in classpath of war file.
/web-inf/classes in classpath; jars in /web-inf/lib.
if put file need /web-inf/classes war class loader should able find it.
you cannot read directory way. must file.
update:
anyway, moved folder different paths, such tomcat/webapps/myproject/web-inf/classes/folder or tomcat/webapps/myproject/web-inf/lib/folder same result. did miss something?
yes, you've missed 2 things:
- your folder should go in 1 place - under /web-inf/classes
- you cannot access folder using getresourceasstream() , read contents. doesn't work way. can read 1 individual file way.
Comments
Post a Comment