在Windows上为tomcat设置用户环境变量 [英] Setting User Environment Variables for tomcat on Windows

查看:157
本文介绍了在Windows上为tomcat设置用户环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows 2012上运行tomcat 8.

I am running tomcat 8 on windows 2012.

我有一个Webapp,需要设置用户环境变量才能运行.

I have a webapp that needs an user environment variable to be set in order to run.

我可以启动tomcat并成功检索变量,如下所示:

I can start tomcat and retrieve the variable successfully like this:

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_65
set CATALINA_HOME=C:\TOOLS\apache-tomcat-8.0.30

set CATALINA_BASE=C:\Users\ULUser\tomcat\myapp
set MY_VAR=%CATALINA_BASE%
set PATH=%CATALINA_ROOT%\bin;%PATH%
set CATALINA_OPTS="-Djava.security.properties=%CATALINA_BASE%\conf\java.security.properties"

%CATALINA_HOME%\bin\catalina.bat run

但是当我作为Windows服务运行时,无法使用System.getenv("MY_VAR");

But when I run as a windows service I cannot retrieve the MY_VAR variable using System.getenv("MY_VAR");

Google的搜索没有帮助,因为所有页面都显示了如何设置Java和Tomcat变量,而不是如何设置用户变量.

Googling has not helped because all the pages show how to set the Java and Tomcat variables, but not how to set user variables.

我无法为整个机器设置它,因为我有多个tomcat实例,每个实例都需要一个不同的环境变量.

I cannot set it for the entire machine because I have several instances of tomcat and each one needs a different environment variable.

我希望使用一种方法,该方法包括在tomcat配置中而不是Windows配置中设置变量.

I would prefer a method that involves setting the variable in the tomcat configuration rather that with windows configuration.

我的替代方法是在tomcat中使用context.xml文件,并使用检索上下文设置的逻辑来检索值.

My alternative is to use context.xml file in tomcat and retrieve the value with logic that retrieves context settings.

我最初将此内容发布在超级用户上,但有人建议将stackoverflow用作此问题的更好位置.如果我在这里得到了很好的答案,我将从超级用户中删除它.

I originally posted this on superuser, but it was suggested that stackoverflow is a better place for this question. If I get good answers here I will delete it off of superuser.

推荐答案

有同样的问题,我已经设法解决了.

Had the same problem and I've just managed to solve it.

在管理员命令提示符下,执行以下命令:

From an admin command prompt do the following command:

tomcat8 //US//YourServiceName ++Environment varname=value

您可以通过使用分号(;)或哈希(#)分隔变量来设置多个变量.文档非常清楚,但是很难相信发生了任何事情,因为任何地方都没有视觉指示,这意味着如果运行:

You can set more than one variable by separating them with a semicolon (;) or a hash (#). The documentation is quite clear, but it's just difficult to believe anything's happened, because there's no visual indication anywhere, meaning if you run:

tomcat8w //ES//YourServiceName 

要获取GUI配置工具,您不会在任何地方看到环境变量,但是如果您运行该服务,它将对其进行选择.这就像隐藏的魔法.不知道为什么他们没有将其包含在配置面板中的某个位置.

to get the GUI config tool you will not see the environment variable anywhere, but if you run the service it will pick it up. It's like hidden magic. Don't know why they haven't included it somewhere in the config panel.

这篇关于在Windows上为tomcat设置用户环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆