Link to home
Start Free TrialLog in
Avatar of opike
opike

asked on

Getting error message "Unable to compile class for JSP: "

I'm trying to use a user defined class in a jsp file but I'm getting the error message "unable to compile class for jsp". I packaged the .class file into a file called test.jar and put the jar file in the
/var/lib/tomcat6/webapps/examples/WEB-INF/lib

directory.

This is the path to the jsp page I'm trying to execute:
/var/lib/tomcat6/webapps/examples/jsp/JSPEssbase2/essbasedatasource.jsp
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

do you have the tools.jar in tomcat6/commons/lib directory?
Avatar of opike
opike

ASKER

These are the only lib directories I have currently:


/var/lib/tomcat6/webapps/examples/jsp/JSPEssbase2/WebContent/WEB-INF/lib
/var/lib/tomcat6/webapps/examples/jsp/WebContent/WEB-INF/lib
/var/lib/tomcat6/webapps/examples/WEB-INF/lib

I tried putting test.jar in all of them.
SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of opike

ASKER

There is some strange behavior in that usually I get the "Unable to compile class..." error message, but occasionally I get the following error message:

 
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP
	org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:156)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.apache.jasper.JasperException: Unable to load class for JSP
	org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:630)
	org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:144)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.ClassNotFoundException: org.apache.jsp.jsp.JSPEssbase2.essbasedatasource_jsp
	java.net.URLClassLoader$1.run(URLClassLoader.java:217)
	java.security.AccessController.doPrivileged(Native Method)
	java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:134)
	org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
	org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:628)
	org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:144)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Open in new window

your jsp is not compiling, make sure you have an import directive for the external class you are using
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you JSP file from IDE like eclipse some its version may not look into .../WEB-INF/lib/ directory, so add your test.jar to project's build path using "Add External Jars" option and try. Hope this may work out.

Perhaps you want to try to place this class without jar like in this simple example:

http://forums.devshed.com/java-help-9/problem-when-use-bean-class-with-jsp-38680.html

-----------------------------------------------------

I keep been class in mybean package in

c:\tomcat\webapps\Root\Web-inf\classes\mybean\somebean.class

and save jsp file in
c:\tomcat\webapps\Root\jsp\sec1\test.jsp

and I type url in http://localhost:8080/jsp/sec1/test.jsp

This work correct. and all directory such as \Root\jsp, \Root\news, \Root\webboard must use \Root\Web-inf\classes\mybean to keep class file.
----------------------------------------------------------------------------------
Are you using any IDE like eclipse or netbeans. if yes then do you see any error reported for the jsp file.

i doubt the problem is with the container. do a clean and build of your project and also clear the work folder of tomcat and then deploy again.

it is not giving error for any other class but the page class itself so it is the container which i guess is unable to locate it due to some internal error.
too many WEB-INF/lib folders. are these all different applications?
Avatar of opike

ASKER

I was using the default package for the .class files I was trying to reference.

So I created a package com.pikefin.essbase, recompiled and then created a new jar file test1.jar (attached with jar extension changed to txt).
 test1.txt

I put this new jar file in the folder
/var/lib/tomcat6/webapps/examples/jsp/JSPEssbase2/WebContent/WEB-INF/lib

cleaned out all the previous test.jar jar files and also put this import statement in my jsp file:
<%@ page import="com.pikefin.essbase.PopulateSpreadsheet" %>

Now I get this error message:

"org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 7 in the generated java file
Only a type can be imported. com.pikefin.essbase.PopulateSpreadsheet resolves to a package"

So I think I might have screwed up creating the jar file but as far as I can tell it looks ok.
Try to put just classes without jarring like I mentioned above - you'll at least
not have to worry about whether it has to do with creating jar
Avatar of opike

ASKER

I tried putting all the unjar'd class files in this directory:

/var/lib/tomcat6/webapps/examples/jsp/JSPEssbase2/WebContent/WEB-INF/classes/com/pikefin/essbase

removed the test1.jar file from the WEB-INF/lib directory and bounced tomcat, but I still get the error message:

"An error occurred at line: 7 in the generated java file
Only a type can be imported. com.pikefin.essbase.PopulateSpreadsheet resolves to a package"
PopulateSreadsheet is the name of your class ?
It for some reason believes that it is the name of a package
Do you really think you should put your classes in such a long path?

If you web app is  called examples then it should all start in ...examples/WEB-INF/classes - do you really need this ../jsp/... and so on
Compare with this simple exmaple I posted earlier.
Avatar of opike

ASKER

Did you see a specific problem with the path that I generated or is this just grasping at straws with the package length?

Anyways, I took your suggestion and created a shorter package name. So now the classes reside in:
/var/lib/tomcat6/webapps/examples/jsp/JSPEssbase2/WebContent/WEB-INF/classes/pikefin

and the import statement is:

<%@ page import="pikefin.PopulateSpreadsheet" %>

Bounced tomcat.

Still getting the same "Only a type can be imported..." error.

I suggest to compare with installation of Tomcat which comes with its examples.
examples is the name of app just inside webbapps.
Look say at calculator.  It uses class cal.TableBean
So this class is in webapps/examples/WEB-INF/classes/cal/ - just check - you probably have the same, as it comes with any Tomcat.
So just by analogy all your classes belonging to application "examples" should have root folder in /WEB-INF/classes
in examples/jsp/ you can put .jsp pages, but classes for jsp resde in the same place as for servlets  - in ../WEB-INF/classes
just in /var/lib/tomcat6/webapps/examples/WEB-INF/classes/ should start your top folder , say pikefin
As I understand it, all jsp for your web application have top folder in ...webapps/app_name - together with the .html pages - jsp are similar to .html
and all classes for .jsp should be in ...webapps/app_name/WEB-INF/classes together with the servlet classes, if your application
uses not just .jsp but also precompiled servlets.
From your post above: https://www.experts-exchange.com/questions/26802355/Getting-error-message-Unable-to-compile-class-for-JSP.html#34832758

The error message  
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 7 in the generated java file
Only a type can be imported. com.pikefin.essbase.PopulateSpreadsheet resolves to a package

means that it's likely that com.pikefin.essbase.PopulateSpreadsheet was at some declared as a package name.  When you have removed .jar files -- have you bounced the webapp as well?  Because if you made an error in a class -- ie, had some java class that you declared with that as a package name -- and you haven't reloaded the webapp, that incorrect class name is still in the webapp classpath.

Do you actually have code which is not the Google Gdata library?  Because if you just need to include the gdata jars in your webapp, you can do that.
Avatar of opike

ASKER

I'm only using the unjar'd class files now. The only jar files that exist under the
/var/lib/tomcat6
directory are these 2:

[ollie@devdataload tomcat6]$ find . -name "*.jar" -print
./webapps/examples/WEB-INF/lib/taglibs-core.jar
./webapps/examples/WEB-INF/lib/taglibs-standard.jar

I tried creating another directory under examples to see if maybe the WebContent folder was the cause of the problem. These are the files that I have under this new directory:

 
[ollie@devdataload jsp]$ ls -Rp JSPEssbase3/
JSPEssbase3/:
essbasedatasource.jsp  META-INF/  WEB-INF/

JSPEssbase3/META-INF:
MANIFEST.MF

JSPEssbase3/WEB-INF:
classes/  lib/

JSPEssbase3/WEB-INF/classes:
pikefin/

JSPEssbase3/WEB-INF/classes/pikefin:
BatchSample$CellAddress.class  Logs.class
BatchSample.class              PopulateSpreadsheet.class
CustomBufferedWriter.class     SkipLoadException.class
DBFunctions.class              TestException.class
EssbaseConnect.class           UtilityFunctions.class

JSPEssbase3/WEB-INF/lib:

Open in new window


But I still get the same error message.
Avatar of opike

ASKER

I've bounced tomcat before each test.

I'm using other code besides the google Gdata library.
clear tomcats work directory and restart tomcat

does it run ok under eclipse?
is your JSPEssbase3 folder immediately under webapps - ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of opike

ASKER

@objects:

I cleared everything out of this folder:
/var/cache/tomcat6/work/Catalina/localhost

and restarted Tomcat. Hopefully that was the right directory. However I still get the error.

I've been running the jsp outside of eclipse. I haven't yet figured out how to run it from within eclipse.
Avatar of opike

ASKER

@mrcoffee:
for_yan suggested that my package name might be "too long" so I shortened it too just pikefin. I did that at the same time I exploded the jar file.

I'll try to go back and use the jar again.
No, no I didn't suggest that your pacjkage too long
I suggested that you should have the top of your clases in webbapps/exapmples/WEB-INF/classes
Do you have in the examples folder original jsp's which come with Tomcat
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

This is my suggestion - I attached two zips - cal_jsp.zip and cal_classes.zip.

This is example from Tomcat distribution - calendar  application using jsp.

Unzip first one, rename cal_jsp folder to cal, go inside the folder and rename two .txt files back to .jsp files (EE does not allow to upload jsp) and move this folder into .../webapps/examples/jsp

Unzip the second one, rename folder cal_classes to cal, and move this folder to ../webapps/examples/WEB-INF/classes

Then try to run

http://localhost:8080/jsp/cal/login.html

and let us know if it works for you.

This experiment will tell us a lot about why you may have problems.



cal-jsp.zip
cal-classes.zip
Sorry just one correction - after you place them as I described start it this way:

http://localhost:8080/examples/jsp/cal/login.html

in  the previous message I forgot "examples" - the name of the application after http://localhost:8080/
Avatar of opike

ASKER

Well hooray, I got past that error and now on to the next problem :).

I went with for_yan's suggestion of moving the class files to the WEB-INF/classes directory under examples so the full directory looked like this:

/var/lib/tomcat6/webapps/examples/WEB-INF/classes/pikefin

Awarded some points to the others since I appreciate everyone's effort
Great! I'm happy it worked.
And of course you were right to distribute the points - it was certainly collaborative effort.
Congrats on getting it working.

Notes:  There was no need to change the class path for your classes.  It is standard to have com, org, or whatever domain before your company domain, so com.pikefin was more standard naming practice.  Nothing in your errors had anything to do with the length of your class names, which were correct anyway.

It is often easier to debug classpath problems from the classes directory.  However, since the classes directory is simply an extraction of the jar, you know that it doesn't make a fundamental difference.  The first possibility is that your jar was not in the webapp's classpath.  Or, it is also possible that there is a naming problem in your classes that is resolved when the classes are in a classes directory -- since that can be treated slightly differently by the classloader from  jars.  There could be duplicate classes, or another jar with classes with the same names as the classes you named.  

If you have duplicate names in a Java classpath, the JVM will find the first jar in its list with that class name, and only return classes from within that jar.  It's best to find out where your duplicates are (if you have any) and remove them, because it can be very hard to debug when that happens.