Java Tomcat 在 Eclipse 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20235909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:28:32  来源:igfitidea点击:

Tomcat is not working in Eclipse

javaeclipsemacostomcat

提问by ChrisZ

I couldn't believe I spent several days in solving this issue, maybe partially because I was not that familiar with Mac. OK, just post out my solution diary. Hope it will be helpful to other folks.

我简直不敢相信我花了几天时间解决这个问题,可能部分是因为我对 Mac 不太熟悉。好的,只需发布​​我的解决方案日记。希望它会对其他人有所帮助。

encountered problems:1.The first problem is Tomcat cannot be started from Eclipse, but can be started by startup.sh (locahost:8080 page cannot be loaded) 2.And then, after some attempts, even startup.sh will not work. 3. After fixing the first two problem, the locahost:8080 page can be loaded, but the application can not be loaded. 404 error - resouce not found is shown even though the war folder is correctly placed inside the webapps folder. When Tomcat has reference to the Struts 2 application, some ClassNotFoundException or MethodNotDefinedException will occur.

遇到的问题:1.第一个问题是Tomcat无法从Eclipse启动,但是可以通过startup.sh启动(locahost:8080页面无法加载) 2.然后,经过一些尝试,连startup.sh都无法运行。3、修复前两个问题后,可以加载locahost:8080页面,但无法加载应用程序。404 错误 - 即使 war 文件夹正确放置在 webapps 文件夹中,也会显示未找到资源。当 Tomcat 引用 Struts 2 应用程序时,会发生一些 ClassNotFoundException 或 MethodNotDefinedException。

Causes and Solution to each of the problem.

每个问题的原因和解决方案。

  1. Tomcat cannot be started from Eclipse. The cause is that the setup for Tomcat in Eclipse is not completed. Tomcat in Eclipse is not yet pointed to the configuration folder. Solution: double click on the Tomcat server in Server tab, and a configuration window will come up. In the window, select the Tomcat configuration folder and deployment destination folder.

  2. startup.sh does not work. The cause MAY BE duplicated jar issue. I forgot I had copied the entire Struts 2 jars in /Library/Java/Extensions, which had been originally empty. Solutions: Clear that folder, and startup.sh will work.

  3. Web application cannot be loaded even when Tomcat is working (the previous 2 issues have been fixed) The cause MAY BE that redundant Struts jars are included in the project. I googled to find out the necessary jars for Struts 2, and include them only.

  1. 无法从 Eclipse 启动 Tomcat。原因是Eclipse中Tomcat的设置没有完成。Eclipse 中的Tomcat 尚未指向配置文件夹。解决方法:在Server选项卡中双击Tomcat服务器,会出现一个配置窗口。在窗口中,选择 Tomcat 配置文件夹和部署目标文件夹。

  2. startup.sh 不起作用。原因可能是重复的 jar 问题。我忘了我已经在 /Library/Java/Extensions 中复制了整个 Struts 2 jar,它原本是空的。解决方案:清除该文件夹,startup.sh 将起作用。

  3. 即使 Tomcat 正在工作,也无法加载 Web 应用程序(前两个问题已修复) 原因可能是项目中包含了多余的 Struts jar。我在谷歌上搜索了 Struts 2 所需的 jars,并且只包含它们。

Original post:I have been trying to run a Hello World web application in Eclipse but cannot publish.

原帖:我一直在尝试在 Eclipse 中运行一个 Hello World Web 应用程序,但无法发布。

Here is what I have done:

这是我所做的:

-using Macintosh on OS X 10.8.

- 在 OS X 10.8 上使用 Macintosh。

-downloading Tomcat 7 from Apache, and upzipping the file under some directory.

- 从 Apache 下载 Tomcat 7,并将文件上传到某个目录下。

-also creating a certain environment variables like $CATALINA_BASE and $CATALINA_HOME by using the "export" command.

- 还通过使用“export”命令创建某些环境变量,如 $CATALINA_BASE 和 $CATALINA_HOME。

-In Eclipse, creating a new server as Tomcat 7, pointing it to the unzipped Tomcat directory above, and add the web application(compile error-free) project into Tomcat

-在Eclipse中,新建一个服务器为Tomcat 7,指向上面解压后的Tomcat目录,将web应用(编译无误)项目添加到Tomcat中

-publishing my Web app.

- 发布我的 Web 应用程序。

`

`

Here is what I countered.

这是我反驳的。

As the server in Eclipse is running, I got 404 error that resource is not found. I checked server.xml, the context reference was added. But in Tomcat's webapps folder, there is no war file added. Even if I typed locahost:8080, I still get 404 error instead of the tomcat "default page".

当 Eclipse 中的服务器正在运行时,我收到 404 错误,即找不到资源。我检查了 server.xml,添加了上下文引用。但是在Tomcat的webapps文件夹中,并没有添加war文件。即使我输入了 locahost:8080,我仍然会收到 404 错误而不是 tomcat“默认页面”。

    -
    ——

However, it worked if I started the server with startup.sh, and manually dragged the war file under the webapps folder. Immediately, the war gets unzipped and I can load my static and dynamic pages.

但是,如果我使用startup.sh 启动服务器,并手动将war 文件拖到webapps 文件夹下,它会起作用。战争立即被解压缩,我可以加载我的静态和动态页面。

p.s. Once my problem above is solved, I have more problems now. My startup.sh can no longer start the server now. And Everytime I run it, it seems to erase the value of the environment variables it concerns.

ps 解决了上面的问题后,我现在有更多的问题。我的 startup.sh 现在不能再启动服务器了。而且每次运行它,它似乎都抹去了它所关注的环境变量的值。

----------------RESPONSE TO MY HELPER COMMENTERS------------------------------------ to Keerthi Ramanathan: catalina.out content

----------------回复我的帮助评论者----------------------------- ------- 致 Keerthi Ramanathan:catalina.out 内容

Nov 27, 2013 10:26:50 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Nov 27, 2013 10:26:50 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 27, 2013 10:26:50 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 27, 2013 10:26:50 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 800 ms
Nov 27, 2013 10:26:50 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 27, 2013 10:26:50 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Nov 27, 2013 10:26:50 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.47/webapps/docs
Nov 27, 2013 10:26:50 PM org.apache.tomcat.websocket.server.WsSci onStartup
INFO: JSR 356 WebSocket (Java WebSocket 1.0) support is not available when running on Java 6. To suppress this message, run Tomcat on Java 7, remove the WebSocket JARs from $CATALINA_HOME/lib or add the WebSocketJARs to the tomcat.util.scan.DefaultJarScanner.jarsToSkip property in $CATALINA_BASE/conf/catalina.properties. Note that the deprecated Tomcat 7 WebSocket API will be available. 
Nov 27, 2013 10:26:50 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.47/webapps/examples
Nov 27, 2013 10:26:51 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.47/webapps/host-manager
Nov 27, 2013 10:26:51 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.47/webapps/manager
Nov 27, 2013 10:26:51 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.47/webapps/ROOT
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Nov 27, 2013 10:26:51 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1075 ms
java.lang.NoSuchMethodError: org.apache.juli.ClassLoaderLogManager.setUseShutdownHook(Z)V
    at org.apache.catalina.startup.Catalina.start(Catalina.java:719)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-8080"]
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-bio-8009"]
Nov 27, 2013 10:26:51 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-bio-8080"]
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["ajp-bio-8009"]
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-bio-8080"]
Nov 27, 2013 10:26:51 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-bio-8009"]
Exception in thread "Thread-2" java.lang.NoSuchMethodError: org.apache.juli.ClassLoaderLogManager.shutdown()V
    at org.apache.catalina.startup.Catalina$CatalinaShutdownHook.run(Catalina.java:920)

-

——

to Krish

到克里什

By select the radio button "Use Tomcat installation...." I can see a good page when loading localhost:8080

通过选择单选按钮“使用Tomcat安装....”我可以在加载localhost:8080时看到一个很好的页面

By set the deployment path to the webapps folder, I can see the unzipped war folder copied under the webapps folder.

通过将部署路径设置为webapps文件夹,可以看到webapps文件夹下复制的解压war文件夹。

However, loading an application still failed. loading the localhost:8080/HellowWorldStruts2/index.jsp or localhost:8080/HellowWorldStruts2/index.jsp (ignore the typo of additional w, it is a typo along in the project) still gave a 404 error and description The requested resource is not available. However, the index.jsp is right under the war folder.

但是,加载应用程序仍然失败。加载 localhost:8080/HellowWorldStruts2/index.jsp 或 localhost:8080/HellowWorldStruts2/index.jsp(忽略附加 w 的错字,这是项目中的错字)仍然给出 404 错误和说明请求的资源不是可用的。但是,index.jsp 就在 war 文件夹下。

And I got these feedbacks during deployment

我在部署过程中得到了这些反馈

SEVERE: Exception starting filter struts2
java.lang.NoClassDefFoundError: javax/servlet/Filter
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access##代码##0(URLClassLoader.java:58)
    at java.net.URLClassLoader.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:295)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1617)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:142)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1120)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1678)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
    at java.net.URLClassLoader.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 35 more
Nov 28, 2013 12:09:20 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
Nov 28, 2013 12:09:20 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/HellowWorldStruts2] startup failed due to previous errors

--------------------END RESPONSE TO MY HELPER COMMENTERS---------------------------

--------------------结束对我的帮助评论者的回复------------------------ ---

采纳答案by Krishna Tangirala

You can try the following that changes the way Eclipse handles Tomcat.

您可以尝试以下更改 Eclipse 处理 Tomcat 的方式。

  • In Eclipse, go to 'Servers' tab and double-click on the Tomcat instance you have. This opens up a tab
  • In the ab, click on the Server Locations section to expand the view (if collapsed)
  • Select the second radio button that says "Use Tomcat installation (takes control of Tomcat installation)"
  • Also change the deploy path to the path of the webapps directory in your tomcat installation by clicking the Browse button. In my case, it is set to "D:\Apps\apache-tomcat-7.0.47\webapps"
  • Click the "Save" button on the toolbar and restart your Tomcat instance from the Servers tab
  • 在 Eclipse 中,转到“服务器”选项卡并双击您拥有的 Tomcat 实例。这会打开一个选项卡
  • 在 ab 中,单击“服务器位置”部分以展开视图(如果已折叠)
  • 选择第二个单选按钮“使用 Tomcat 安装(控制 Tomcat 安装)”
  • 还要通过单击“浏览”按钮将部署路径更改为您的 tomcat 安装中的 webapps 目录的路径。就我而言,它设置为“D:\Apps\apache-tomcat-7.0.47\webapps”
  • 单击工具栏上的“保存”按钮,然后从“服务器”选项卡重新启动 Tomcat 实例

You can try seeing the tomcat page.

您可以尝试查看 tomcat 页面。

Let me know how it goes.

让我知道事情的后续。