Tomcat找不到直接放在classes文件夹下的类 [英] Tomcat can't find class that is placed directly under classes folder

查看:687
本文介绍了Tomcat找不到直接放在classes文件夹下的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSP:

<%@ page import="foo.*" %>
<html>
    <body>
        The page count is:
        <%=Counter.getCount()%>
    </body>
    </html>

我在包中有 Counter foo 存储于:


C:\ apache-tomcat-6.0。 32 \webapps\God \WEB-INF \classes

C:\apache-tomcat-6.0.32\webapps\God\WEB-INF\classes

并且容器可以从其包中找到该类 foo

And the container could find the class from its package foo.

但是当我尝试直接在 \ WEB-下添加一些其他类文件时INF\classes 而不是在任何特定的包中,例如 foo ,然后容器找不到该类。

But when I try to add some other class file directly under \WEB-INF\classes and not in any specific package such as foo, then container can't find that class.

这是怎么造成的,如何解决?

How is this caused and how can I solve it?

推荐答案

默认包中的类是在包中本身不可见的类。只要您想将类导入到包中的另一个类中,必须将该类放入包中。从技术上讲,当编译JSP文件时,容器将自动生成一个 .class 文件,该文件本身就在一个包中。因此,您无法从JSP中的默认包中导入类。

Classes in the default package are not visible to classes which are by itself inside a package. You must put the class in a package whenever you want to import it in another class which is by itself inside a package. Technically, when JSP files are compiled, the container will autogenerate a .class file which is by itself inside a package. So you cannot import classes from the default package in the JSP.

因此,无论何时您希望能够在任何地方重用类,都必须将其置于具体的包,不在默认包中。作为练习,自己创建两个类,一个在包内,另一个不在包内。现在,在带有包的那个内部,尝试 import 并使用没有包的那个。你会发现这是不可能的,代码也不会编译。 servletcontainer在引擎盖下遇到完全相同的问题。

So, whenever you want to be able to reuse a class anywhere, it has to be placed in a concrete package, not in the default package. As an exercise, create two classes yourself, one which is inside a package and other which is not inside a package. Now, inside the one with package, try to import and use the one without package. You'll see that it's not possible and the code won't compile. The servletcontainer encounters exactly the same problem "under the hoods".

  • The Java Tutorial - Lesson: Packages

无关具体问题:编写原始Java代码JSP文件是糟糕的做法。请考虑学习并使用 servlet

Unrelated to the concrete problem: writing raw Java code in JSP files is a poor practice. Consider learning and using servlets.

这篇关于Tomcat找不到直接放在classes文件夹下的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆