如何更改 TOMCAT 使用的 Java 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4072260/
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-14 11:16:27  来源:igfitidea点击:

How to change Java version used by TOMCAT?

javajsptomcattomcat5.5java-home

提问by LGAP

I have Java 1.6 and Tomcat 5.5 installed on my system.

我的系统上安装了 Java 1.6 和 Tomcat 5.5。

But Tomcat 5.5 accesses Java 1.5 and hence as the outcome I get the error Bad version number in .class filewhile executing java code with JSP.

但是 Tomcat 5.5 访问 Java 1.5,因此结果我在Bad version number in .class file使用 JSP 执行 Java 代码时遇到错误。

How can I change the Tomcat version to Java 1.6?

如何将 Tomcat 版本更改为 Java 1.6?

UPDATE

更新

I tried changing the JVM that the tomcat5w.exe is pointing to the version 1.6 and now I am out of the Bad version in .class fileerror. But now, I get the following error.

我尝试更改 tomcat5w.exe 指向版本 1.6 的 JVM,现在我没有出现Bad version in .class file错误。但是现在,我收到以下错误。

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:498)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NullPointerException
myfirst.SearchLink.checkURL(SearchLink.java:20)
org.apache.jsp.Test_jsp._jspService(Test_jsp.java:52)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

What might be the root cause?

根本原因可能是什么?

采纳答案by Bozho

When you open catalina.sh / catalina.bat, you can see :

打开 catalina.sh/catalina.bat 可以看到:

Environment Variable Prequisites

JAVA_HOME Must point at your Java Development Kit installation.

环境变量先决条件

JAVA_HOME 必须指向您的 Java Development Kit 安装。

So, set your environment variable JAVA_HOMEto point to Java 6. Also make sure JRE_HOMEis pointing to the same target, if it is set.

因此,将您的环境变量设置JAVA_HOME为指向 Java 6。JRE_HOME如果已设置,请确保指向相同的目标。

Update: since you are on Windows, see herefor how to manage your environment variables

更新:由于您使用的是 Windows,请参阅此处了解如何管理您的环境变量

回答by Jalkin

In Eclipse it is very easy to point Tomcat to a new JVM (in this example JRE6). My problem was I couldn't find where to do it. Here is the trick:

在 Eclipse 中,很容易将 Tomcat 指向一个新的 JVM(在本例中为 JRE6)。我的问题是我找不到在哪里做。这是诀窍:

  1. On the ECLIPSE top menu FILEpull down tab, select NEW, -->Other
  2. ...on the New Server: Select A Wizardwindow, select: Server-> Server... click NEXT
  3. . on the New Server: Define a New Serverwindow, select Apache> Tomcat 7 Server
  4. ..now click the line in blue and underlined entitled: Configure Runtime Environments
  5. on the Server Runtime Environmentswindow,
  6. ..select Apache, expand it(click on the arrow to the left), select TOMCAT v7.0, and click EDIT.
  7. you will see a window called EDIT SERVER RUNTIME ENVIRONMENT: TOMCAT SERVER
  8. On this screen there is a pulldown labeled JREs.
  9. You should find your JRE listed like JRE1.6.0.33. If not use the Installed JREbutton.
  10. Select the desired JRE. Click the FINISHbutton.
  11. Gracefully exit, in the Server: Server Runtime Environmentswindow, click OK
  12. in the New Server: Define a new Serverwindow, hit NEXT
  13. in the New Server: Add and RemoveWindow, select apps and install them on the server.
  14. in the New Server: Add and RemoveWindow, click Finish
  1. 在 ECLIPSE 顶部菜单FILE下拉选项卡上,选择NEW, --> Other
  2. ...在New Server: Select A Wizard窗口中,选择:Server-> Server... 单击NEXT
  3. . 在New Server 上:Define a New Server窗口,选择Apache> Tomcat 7 Server
  4. ..现在单击蓝色和下划线标题为:配置运行时环境的行
  5. 在“服务器运行时环境”窗口中,
  6. ..选择Apache,展开它(点击左边的箭头),选择TOMCAT v7.0,然后点击EDIT
  7. 您将看到一个名为EDIT SERVER RUNTIME ENVIRONMENT: TOMCAT SERVER的窗口
  8. 在这个屏幕上有一个标有JREs的下拉菜单。
  9. 您应该会发现您的 JRE 像JRE1.6.0.33一样列出。如果不使用已安装的 JRE按钮。
  10. 选择所需的 JRE。单击完成按钮。
  11. 优雅退出,在Server:Server Runtime Environments窗口,点击OK
  12. New Server: Define a new Server窗口中,点击NEXT
  13. 新服务器:添加和删除窗口中,选择应用程序并将它们安装在服务器上。
  14. 新服务器:添加和删除窗口中,单击完成

That's all. Interesting, only steps 7-10 seem to matter, and they will change the JRE used on all servers you have previously defined to use TOMCAT v7.0. The rest of the steps are just because I can't find any other way to get to the screen except by defining a new server. Does anyone else know an easier way?

就这样。有趣的是,似乎只有第 7-10 步很重要,它们会将您之前定义的所有服务器上使用的 JRE 更改为使用 TOMCAT v7.0。剩下的步骤只是因为我找不到任何其他方式来进入屏幕,除非定义一个新服务器。有没有其他人知道更简单的方法?

回答by paiego

On Linux, Tomcat7 has a configuration file located at:

在 Linux 上,Tomcat7 有一个配置文件位于:

/etc/sysconfig/tomcat7

... which is where server specific configurations should be made. You can set the JAVA_HOME env variable here w/o needing to create a profile.d/ script.

...这是应该进行服务器特定配置的地方。您可以在此处设置 JAVA_HOME 环境变量,而无需创建 profile.d/ 脚本。

This worked for me.

这对我有用。

回答by Nick Grealy

If you use the standard scripts to launch Tomcat (i.e. you haven't installed Tomcat as a windows service), you can use the setenv.bat file, to set your JRE_HOME version.

如果您使用标准脚本来启动 Tomcat(即您还没有将 Tomcat 安装为 Windows 服务),您可以使用 setenv.bat 文件来设置您的 JRE_HOME 版本。

On Windows, create the file %CATALINA_BASE%\bin\setenv.bat, with content:

set "JRE_HOME=%ProgramFiles%\Java\jre1.6.0_20"

exit /b 0

在 Windows 上,创建文件%CATALINA_BASE%\bin\setenv.bat,内容为:

set "JRE_HOME=%ProgramFiles%\Java\jre1.6.0_20"

exit /b 0

And that should be it.

应该就是这样。

You can test this using %CATALINA_BASE%\bin\configtest.bat (Disclaimer: I've only checked this with a Tomcat7 installation).

您可以使用 %CATALINA_BASE%\bin\configtest.bat 测试它(免责声明:我只在 Tomcat7 安装中检查过)。

Further Reading:

进一步阅读:

回答by Michael

There are several good answers on here but I wanted to add one since it may be helpful for users like me who have Tomcat installed as a service on a Windows machine.

这里有几个很好的答案,但我想添加一个,因为它可能对像我这样在 Windows 机器上安装 Tomcat 作为服务的用户有所帮助。

Option 3 here: http://www.codejava.net/servers/tomcat/4-ways-to-change-jre-for-tomcat

此处的选项 3:http: //www.codejava.net/servers/tomcat/4-ways-to-change-jre-for-tomcat

Basically, open tomcatw.exe and point Tomcat to the version of the JVM you need to use then restart the service. Ensure your deployed applications still work as well.

基本上,打开 tomcatw.exe 并将 Tomcat 指向您需要使用的 JVM 版本,然后重新启动服务。确保您部署的应用程序仍然可以正常工作。

回答by Hazim

You can change the JDK or JRE location using the following steps:

您可以使用以下步骤更改 JDK 或 JRE 位置:

  1. open the terminalor cmd.
  2. go to the [tomcat-home]\bindirectory.
    ex: c:\tomcat8\bin
  3. write the following command: Tomcat8W //ES//Tomcat8
  4. will open dialog, select the java tab(top pane).
  5. change the Java virtual Machine value.
  6. click OK.
  1. 打开终端cmd
  2. 转到[tomcat-home]\bin目录。
    前任:c:\tomcat8\bin
  3. 编写以下命令: Tomcat8W //ES//Tomcat8
  4. 将打开对话框,选择java tab(top pane)
  5. 更改 Java 虚拟机值。
  6. 单击“确定”

note:in Apache TomEE same steps, but step (3)the command must be: TomEE //ES

注意:在 Apache TomEE 中的步骤相同,但步骤 (3) 中的命令必须是:TomEE //ES

回答by test

test open the termenal or cmd. go to the [tomcat-home]\bin directory. ex: c:\tomcat8\bin write the following command: Tomcat8W //ES//Tomcat8 will open dialog, select the java tap(top tap). change the Java virtual Machine value.

测试打开终端或cmd。转到 [tomcat-home]\bin 目录。例如:c:\tomcat8\bin 写入以下命令: Tomcat8W //ES//Tomcat8 将打开对话框,选择java tap(top tap)。更改 Java 虚拟机值。