julian
julian

Reputation: 129

accessing jar-files from tomcat

he there,

been stuck for a while now. What im trying to do comes down to this:

sources:

        InputStream in = ClassLoader.getSystemResourceAsStream("json2/json.js");

when i run it inside the tomcat, (in==null).

for diagnosis i improvised the following:

        File fu = new File(new URI(this.getClass().getClassLoader().getResource("").toString()));
        String[] l = fu.list();
        for (int i = 0; i < l.length; i++) {
            System.out.println(i+"||"+l[i]);
        }

with the classic java-class it only produces my main-class-file. in tomcat it shows me the content of the "tomcat/lib"-directory.

any ideas? would be greatly appreciated...


EDIT

one detail that i forgot to add (and that really grinds my gears):

SParser.class.getClassLoader().getParent().getResource("")

comes up with a null-ptr. wtf? im not accessing a particular ressource, still no result.

Upvotes: 1

Views: 2080

Answers (2)

BalusC
BalusC

Reputation: 1108722

Do not use getSystemResourceAsStream(). Use getResourceAsStream(). On a Tomcat webapp environment, the system classloader has no knowledge of Tomcat/lib nor the webapp libraries.

Also, when grabbing the ClassLoader, you should preferably grab the context class loader of the current thread.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("/json/json.js");

Upvotes: 6

Tommi
Tommi

Reputation: 8608

Put the JAR file in the WEB-INF/lib directory of your application.

Upvotes: 0

Related Questions