Groovlet + Tomcat:导入库时“无法解析类”

时间:2022-09-03 22:30:43

I'm having some trouble running a groovy servlet (groovlet) in tomcat that imports a library class. When I don't import anything the groovlet works correctly, but if I do import something that I expect to be on the classpath (I can import the same class successfully in a regular servlet), I see the following error:

我在tomcat中运行一个groovy servlet(groovlet)时遇到了一些麻烦,导致了一个库类。当我不导入任何东西时,groovlet正常工作,但是如果我确实导入了我期望在类路径上的东西(我可以在常规servlet中成功导入同一个类),我看到以下错误:

groovy.util.ScriptException: Could not parse scriptName: /MyGroovlet.groovy
java.lang.RuntimeException: groovy.util.ScriptException: Could not parse scriptName: /MyGroovlet.groovy
    at groovy.servlet.GroovyServlet$1.call(GroovyServlet.java:123)
...
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, /MyGroovlet.groovy: 1: unable to resolve class com.mycompany.mypackage.MyLibraryClass
 @ line 1, column 1.

The jar containing MyLibraryClass is in shared/lib, which is loaded by tomcat by the following in catalina.properties:

包含MyLibraryClass的jar在shared / lib中,由catcatina.properties中的tomcat加载:

shared.loader=...,${catalina.base}/shared/lib/*.jar,...

My groovlets are mapped as described in the user guide in my application's web.xml:

我的groovlet按照我的应用程序的web.xml中的用户指南中的描述进行映射:

<servlet>
    <servlet-name>GroovyServlet</servlet-name>
    <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>GroovyServlet</servlet-name>
    <url-pattern>*.groovy</url-pattern>
</servlet-mapping>

And here's the code for the groovlet, MyGroovlet.groovy:

这里是groovlet的代码,MyGroovlet.groovy:

import com.mycompany.mypackage.MyLibraryClass
MyLibraryClass.someStaticMethod()

My groovlet is deployed to WEB-INF/groovy/MyGroovlet.groovy, per the GroovyServlet API.

根据GroovyServlet API,我的groovlet部署到WEB-INF / groovy / MyGroovlet.groovy。

When I visit http://localhost:8080/myapplication/MyGroovlet.groovy, the error described previously is written to my application logs.

当我访问http:// localhost:8080 / myapplication / MyGroovlet.groovy时,前面描述的错误将写入我的应用程序日志。

Is there some way that I need to explicitly declare the runtime classpath for GroovyServlet? I've tried moving the library jar to several places, including WEB-INF/lib and moving the actual MyLibraryClass.class file to WEB-INF/classes, but with no luck.

有什么方法我需要显式声明GroovyServlet的运行时类路径?我已经尝试将库jar移动到几个地方,包括WEB-INF / lib并将实际的MyLibraryClass.class文件移动到WEB-INF / classes,但没有运气。

2 个解决方案

#1


1  

I'm using Groovy plugin for Eclipse. Exporting Groovlets in a war file does also work.

我正在使用Eclipse的Groovy插件。在war文件中导出Groovlet也很有效。

When I do export my Groovlet-based application, this helpful plugin puts .groovy files in the /WEB-INF/classes directory (in the classpath). And it works when I deploy the war file in my Tomcat Server.

当我导出基于Groovlet的应用程序时,这个有用的插件将.groovy文件放在/ WEB-INF / classes目录中(在类路径中)。当我在Tomcat服务器中部署war文件时,它可以正常工作。

Hope that this helps.

希望这会有所帮助。

Regards.

#2


0  

A stupid mistake I made was that I needed to reload the webapp before the jar I copied into WEB-INF/lib would be loaded (i.e. either restarting entire Tomcat server, or reload just the specific app from the Tomcat manager). Dynamically editing the .groovy files right inside the Tomcat/webapps/ dir and seeing the updates to pages immediately lulled me into the feeling that everything would be auto-loaded, but not so with jars. It was maddening until I realized what was going on.

我犯的一个愚蠢的错误是我需要重新加载webapp才能加载我复制到WEB-INF / lib的jar(即重新启动整个Tomcat服务器,或者从Tomcat管理器重新加载特定的应用程序)。在Tomcat / webapps / dir中动态编辑.groovy文件并看到页面更新立即让我觉得一切都会自动加载,但对于jar不是这样。在我意识到发生了什么之前,它一直很令人抓狂。

#1


1  

I'm using Groovy plugin for Eclipse. Exporting Groovlets in a war file does also work.

我正在使用Eclipse的Groovy插件。在war文件中导出Groovlet也很有效。

When I do export my Groovlet-based application, this helpful plugin puts .groovy files in the /WEB-INF/classes directory (in the classpath). And it works when I deploy the war file in my Tomcat Server.

当我导出基于Groovlet的应用程序时,这个有用的插件将.groovy文件放在/ WEB-INF / classes目录中(在类路径中)。当我在Tomcat服务器中部署war文件时,它可以正常工作。

Hope that this helps.

希望这会有所帮助。

Regards.

#2


0  

A stupid mistake I made was that I needed to reload the webapp before the jar I copied into WEB-INF/lib would be loaded (i.e. either restarting entire Tomcat server, or reload just the specific app from the Tomcat manager). Dynamically editing the .groovy files right inside the Tomcat/webapps/ dir and seeing the updates to pages immediately lulled me into the feeling that everything would be auto-loaded, but not so with jars. It was maddening until I realized what was going on.

我犯的一个愚蠢的错误是我需要重新加载webapp才能加载我复制到WEB-INF / lib的jar(即重新启动整个Tomcat服务器,或者从Tomcat管理器重新加载特定的应用程序)。在Tomcat / webapps / dir中动态编辑.groovy文件并看到页面更新立即让我觉得一切都会自动加载,但对于jar不是这样。在我意识到发生了什么之前,它一直很令人抓狂。