Java 如何在 Windows 7 上向 tomcat 添加 jvm 选项

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

How to add jvm options to tomcat on windows 7

javatomcat

提问by hellzone

How can I add jvm options to Tomcat on Windows 7?. By the way I am using Tomcat 7. I added my jvm options to first line of catalina.batfile but it didn't work. I think I am doing something wrong. Is there any idea?

如何在 Windows 7 上向 Tomcat 添加 jvm 选项?顺便说一下,我使用的是 Tomcat 7。我将 jvm 选项添加到catalina.bat文件的第一行,但它不起作用。我想我做错了什么。有什么想法吗?

回答by Keerthivasan

Here you have my two cents,

你有我的两分钱,

Use CATALINA_OPTSor JAVA_OPTS. You can set the jvm optionsin either one of these variables in catalina.batfile according to your requirement. Read the comments in catalina.bat file about these variables. You will understand

使用CATALINA_OPTSJAVA_OPTS。您可以根据需要jvm optionscatalina.bat文件中的这些变量之一中设置。阅读 catalina.bat 文件中关于这些变量的注释。你会明白的

EDITafter your comment

在您的评论后编辑

  1. Set the environment variable - project.homein windows environment using this link. Now, your env variable is set
  2. Set the JAVA_OPTS in setenv.bat (you need to create this file)

    set JAVA_OPTS="-Dproject.home"

  1. 使用此链接在 windows 环境中设置环境变量-project.home。现在,您的 env 变量已设置
  2. 在setenv.bat中设置JAVA_OPTS(需要创建这个文件)

    设置 JAVA_OPTS="-Dproject.home"

or

或者

  set JAVA_OPTS="%JAVA_OPTS% -Dproject.home"

for safety purpose. This will prepend the existing JAVA_OPTSwith the new value.Start the server. Always, use UPPERCASE LETTERS, NUMBERS AND UNDERSCORE for environment variables. This is for portability reasons.

出于安全目的。这将在现有的前面加上JAVA_OPTS新值。启动服务器。对于环境变量,始终使用大写字母、数字和下划线。这是出于便携性原因。

回答by MariuszS

I prefer using context.xmlfor tomcat environment variables:

我更喜欢context.xml用于 tomcat 环境变量:

File conf\context.xmlshould looks like this:

文件conf\context.xml应如下所示:

<?xml version='1.0' encoding='utf-8'?>
<Context>

     ...

     <Environment name="project.home" value="C:\Users\myproject" type="java.lang.String"/>

</Context>

After this environment variable project.homeis simply accessible inside your tomcat app.

project.home您的 tomcat 应用程序中可以轻松访问此环境变量之后。

回答by Christopher Schultz

Create the file bin/setenv.bat. If you are using bin/startup.bator bin/catalina.batto start Tomcat, then the setenvscript will be run before performing most other operations. You can set whatever JVM options you want by setting the CATALINA_OPTSenvironment variable.

创建文件bin/setenv.bat。如果您正在使用bin/startup.batbin/catalina.bat启动 Tomcat,那么该setenv脚本将在执行大多数其他操作之前运行。您可以通过设置CATALINA_OPTS环境变量来设置您想要的任何 JVM 选项。

If you are using Tomcat's service launcher from Microsoft Windows' services panel to launch Tomcat then you cannot use this technique. Instead, you'll need to run tomcat7.exewith the appropriate options you can find here.

如果您从 Microsoft Windows 的服务面板使用 Tomcat 的服务启动器来启动 Tomcat,那么您不能使用此技术。相反,您需要tomcat7.exe使用适当的选项运行,您可以在此处找到

Note that you can also set JAVA_OPTSbut JAVA_OPTSwill be used for all JVM processes, including the one launched to request a shutdown of Tomcat. For example, if you want to enable RMI services for Tomcat and you set them in JAVA_HOME, then Tomcat will start up properly but when attempting to shutdown, the shutdown process may fail due to port conflicts. Similarly, if you need a 20GiB heap for Tomcat and you set -Xmsand -Xmxin JAVA_OPTS, you'll end up creating a 20GiB heap for the process that stopsTomcat. So, use CATALINA_OPTSunless you have a very good reason to use JAVA_OPTS.

请注意,您还可以设置JAVA_OPTSJAVA_OPTS将用于所有 JVM 进程,包括启动以请求关闭 Tomcat 的进程。例如,如果您想为Tomcat启用RMI服务,并且您在 中设置它们JAVA_HOME,那么Tomcat会正常启动,但在尝试关闭时,由于端口冲突,关闭过程可能会失败。同样,如果你需要为Tomcat一个20GiB堆和设置-Xms,并-XmxJAVA_OPTS,你最终会创造的过程20GiB堆停止Tomcat的。因此,CATALINA_OPTS除非您有充分的理由使用JAVA_OPTS.