3

I am having trouble getting my JSP page to load a Java class which is in a jar file. The message I get appears to indicate a class not found exception:

Jan 6, 2011 12:21:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 72 in the jsp file: /xmlloader.jsp
FactArray cannot be resolved to a type
69:         sourceType = "1";
70: }
71: 
72: FactArray fa = new FactArray();
73: Fact f;
74: 
75: /***********************/

The Type FactArray is one of my classes in a package com.mypackage.fact.FactArray which exists in myjar.jar. myjar.jar is a separate Java project (using NetBeans, but I don't think that's relevant).

I include the package in my JSP as follows:

<%@ page import="com.mypackage.fact.*" %>

I deploy my web site and JSPs into Tomcat 6 as a WAR file. I include myjar.jar in that WAR in WEB-INF/lib but that doesn't work. I tried putting myjar.jar in my tomcat/lib folder, but that doesn't work either.

I have bounced the server several times between changes. I have read a whole bunch of questions on here which say "put it in WEB-INF/lib" but that isn't working, so I'm asking my own question.

Where do I need to put common JAR files so they get picked up by Tomcat?

jmj
  • 237,923
  • 42
  • 401
  • 438
Simon
  • 78,655
  • 25
  • 88
  • 118

2 Answers2

10

Putting in /WEB-INF/lib folder of the deployed webapp should work.

If that doesn't work, then the possible causes are:

  • The JAR doesn't contain the desired class at all (extract with ZIP tool and verify)
  • There's a typo in your import and/or class declaration (mind case sensitivity!)

Unrelated to the problem, using scriptlets is not the best practice. Consider a servlet.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • hmm, that's an interesting comment. Can you say why you think I have a scriptlet (I thought I had a servlet). – Simon Jan 06 '11 at 21:48
  • ...wait, I get it... but it is still interesting. But now I think I have a knowledge gap. Instead of that scriptlet what would I do? – Simon Jan 06 '11 at 21:55
  • Start here: [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) – BalusC Jan 06 '11 at 22:14
  • I also had the same problem with my tomcat. Your solution works for me, thank you –  Apr 04 '20 at 11:42
1

Make sure you have the correct import statements in the code; depending on how you set up your env, place the jars in the webapps/ROOT/lib

user353829
  • 1,244
  • 5
  • 25
  • 38