2006年12月19日星期二

【转】Tomcat 5.5无法编译Java 1.5语法JSP的问题

Apache Jakarta Tomcat 5.5.x (我的是tomcat5.)在安装过后,用默认配置,无法自动编译带有 Java 1.5 语法的 JSP,经过多方求证,问题是由其自带的 eclipse 编译器造成的。文档 http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jasper-howto.html 中说:
The Java compiler from Eclipse JDT in included as the default compiler. It is an advanced Java compiler which will load
all dependencies from the Tomcat class loader, which will help tremendously when compiling on large installations with
tens of JARs. On fast servers, this will allow sub-second recompilation cycles for even large JSP pages. This new compiler will be updated to support the Java 5 syntax
as soon as possible.

Apache Ant, which was used in previous Tomcat releases, can be used instead instead of the new compiler by simply
removing the common/lib/jasper-compiler-jdt.jar file, and placing the ant.jar file from the latest Ant distribution in the common/lib folder. If you do this, you also
need to use the "javac" argument to catalina.sh.

由此可见,这个编译器 common/lib/jasper-compiler-jdt.jar 是不支持 Java 5 语法的,而 Apache Ant 工程中的编译器可以替代它解决这个问题,具体方法如下:
将/common/lib/common/lib/jasper-compiler-jdt.jar 删除
将 ant-1.6.x 发布的 ant.jar 拷贝到 /common/lib/
修改 /conf/web.xml 文件,找到并修改如下内容,增加 compilerSourceVM 和 compilerTargetVM 两个设置:
<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>compilerSourceVM</param-name>
        <param-value>1.5</param-value>
    </init-param>
    <init-param>
        <param-name>compilerTargetVM</param-name>
        <param-value>1.5</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>


Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK
org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler(CompilerAdapterFactory.java:105)
org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:924)
org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:757)
org.apache.jasper.compiler.AntCompiler.generateClass(AntCompiler.java:216)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

原因就是tomcat无法找到JDK,把%JAVA_HOME%/lib/tools.jar也移到%TOMCAT_HOME%/common/lib,问题解决。 但是这步操作完后运行JSP时出现了一个错误(不出错的话那就是大吉大利了):

没有评论: