Groovlet + Tomcat:“无法解析类” 导入库时
我在 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 contains < code>MyLibraryClass 位于 shared/lib
中,它由 tomcat 通过 catalina.properties
中的以下内容加载
shared.loader=...,${catalina.base}/shared/lib/*.jar,...
: href="http://groovy.codehaus.org/Groovlets" rel="nofollow noreferrer">用户指南 在我的应用程序的 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>
这是 groovlet 的代码, MyGroovlet.groovy
:
import com.mycompany.mypackage.MyLibraryClass
MyLibraryClass.someStaticMethod()
我的 groovlet 已部署到 WEB-INF/groovy/MyGroovlet.groovy
,根据 GroovyServlet API。
当我访问 http://localhost:8080/myapplication/MyGroovlet.groovy 时,前面描述的错误将写入我的应用程序日志中。
我需要通过某种方式显式声明 GroovyServlet 的运行时类路径吗? 我尝试将库 jar 移动到多个位置,包括 WEB-INF/lib 并将实际的 MyLibraryClass.class 文件移动到 WEB-INF/classes< /code>,但没有运气。
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:
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
:
shared.loader=...,${catalina.base}/shared/lib/*.jar,...
My groovlets are mapped as described in the user guide in my application's 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
:
import com.mycompany.mypackage.MyLibraryClass
MyLibraryClass.someStaticMethod()
My groovlet is deployed to WEB-INF/groovy/MyGroovlet.groovy
, per the GroovyServlet API.
When I visit http://localhost:8080/myapplication/MyGroovlet.groovy
, the error described previously is written to my application logs.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在使用 Eclipse 的 Groovy 插件。 在 war 文件中导出 Groovlet 也可以。
当我导出基于 Groovlet 的应用程序时,这个有用的插件会将 .groovy 文件放入 /WEB-INF/classes 目录(在类路径中)。 当我在 Tomcat 服务器中部署 war 文件时它就起作用了。
希望这有帮助。
问候。
I'm using Groovy plugin for Eclipse. Exporting Groovlets in a war file does also work.
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.
Hope that this helps.
Regards.
我犯的一个愚蠢的错误是,我需要在加载复制到 WEB-INF/lib 的 jar 之前重新加载 web 应用程序(即重新启动整个 Tomcat 服务器,或者仅从 Tomcat 管理器重新加载特定应用程序)。 在 Tomcat/webapps/ 目录中动态编辑 .groovy 文件并看到页面的更新立即让我感觉一切都会自动加载,但 jar 却不是这样。 这让我很抓狂,直到我意识到发生了什么事。
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.