简体   繁体   中英

Why can't Tomcat/JSP see these classes?

I'm trying to write a web app using JSP and Java on a Tomcat 7.0.14, and part of it involves connecting with a MySQL server using Hibernate .

Here's a basic view of my directory structure:

apache-tomcat-7.0.14
|- webapps
   |- ROOT
     |- WEB-INF
     |   |- classes
     |   |   |- some-package-path
     |   |      \- foo.java
     |   |      \- foo.class
     |   |- lib
     |      \- hibernate3.jar
     \- file_giving_issues.jsp

Now, "file_giving_issues.jsp", as you might have guessed, is the file giving me issues. It can't see the classes contained in the various hibernate jar files. I used the following statement:

<%@ page import="org.hibernate.Session" %>

Here, Tomcat tells me,

Only a type can be imported. org.hibernate.Session resolves to a package.

This, by the way, is not true; Session is definitely a type, not a package, and it's in that hibernate3.jar . More specifically, it exists here:

WEB-INF/lib/hibernate3.jar/org/hibernate/Session.class

But I figured I'd play ball and just import everything from org.hibernate, like so:

<%@ page import="org.hibernate.*" %>

Even more bafflingly, Tomcat now tells me,

Session cannot be resolved to a type.

So, I'm at a bit of a loss. I'm not sure if it's a problem with the way Tomcat is configured, or with my class directory structures, or if I don't understans the way JARs work, or what. From what I understand, the JAR files in WEB-INF/lib should always be on Tomcat's Classpath, and those files in WEB-INF/classes/some-package-path work fine, so I'm lost.

Any help would be appreciated.

Only a type can be imported. XXXXX resolves to a package.

The above usually means that the container is not able to find the class file.

Can you see whether you are able to import other jars or classes? I mean, to debug; create a simple hello world JSP and import some of your custom classes and see whether you can work with it.

Also, confirm that the Session class is indeed there in one of the jars in your lib directory.

Do you have any other JARs in WEB-INF/lib, except hibernate? I had a similar problem: tomcat didn't see JARs in WEB-INF/lib, but worked fine with the same files in the global lib directory.

In my case, I removed some unused legacy files from WEB-INF/lib, and it suddenly started working.
It seems, tomcat won't load some files from WEB-INF/lib (for instance, it complains about any file with javax.servlet classes), and will silently ignore others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM