38272

accessing jar-files from tomcat

Question:

he there,

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

<ul><li>I have developed a little library which i am deploying in form of a jar-file. it contains a file i need to access, which is located in "json2/json.js" (yep, doin jscript/rhino things)</li> <li>I import the jar-file into a classical java-project, it works fine. File is found and read</li> <li>I import the SAME jar-file into tomcat-project which looks as follows: <i></i></li> <li>a .jsp that calls a java-class</li> <li>inside the java-class i call my library, which is located in the tomcat/lib-directory</li> <li>now when i try to access the file i cant (null-pointer) </li> </ul>

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...

<hr />

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.

Answer1:

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");

Answer2:

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

Recommend

  • ClassNotFoundException during Deserialization of a just-serializaed class
  • Proper way of using LocationManager in Codename one
  • Property Editor not registered with the PropertyEditorManager: error on custom tag invokation
  • CoffeeScript, Node.js, MongoDB and JasperReports, is it possible?
  • java.lang.IncompatibleClassChangeError
  • Getting derby database connection from Java (in eclipse)
  • No handler found for portlet request: mode 'view', phase 'RENDER_PHASE'
  • Initialize specific subscript item in array by c++ [duplicate]
  • Converting a string as a nodelist in xsl
  • how do i fix broken encoding in my webapp?
  • How to get proper special character from string in java?
  • how access value of array list on Struts framework by properties file
  • Hadoop NoSuchMethodError apache.commons.cli
  • Remove HTTP Response headers in Java
  • Spring - java.lang.NumberFormatException: For input string
  • getResource() to a file on runtime
  • Handle Authentication and Authorization in jax-rs webservice with cxf
  • How to provide hyperlink in email pointing to a specific method inside gwt app (but not main page)
  • Type mismatch: cannot convert from Connection to Connection
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • Spring MVC redirect with custom http headers
  • Is it mandatory to have a doGet or doPost method?
  • insert a picture into database(sqlite) with java code. what should i do?
  • XNU incudes in Kext
  • “mvn clean generate-source” could not resolve dependencies
  • In Java, how can I construct a File from a resource?
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Read text file that is not in the main package in a runnable jar
  • Object and struct member access and address offset calculation
  • How to assign byte[] as a pointer in C#
  • Installed module is empty
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • PHP: When would you need the self:: keyword?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Java static initializers and reflection
  • How to get Windows thread pool to call class member function?
  • LevelDB C iterator
  • How can i traverse a binary tree from right to left in java?