I have used a method "System.getenv();" to get a value of user defined environment variable in jar application. I have made the jar application to run from window service. But when i try to start the service, it is not getting user defined environment variable's value and showing null pointer exception. I have tried with System variable name in "System.getenv("JAVA_HOME");" method, its working fine by getting the respective value. What is the error with User variable in envirinment variables. Should i do anything in the code?
我使用了一种方法“System.getenv();”在jar应用程序中获取用户定义的环境变量的值。我已经使jar应用程序从窗口服务运行。但是当我尝试启动服务时,它没有获得用户定义的环境变量的值并显示空指针异常。我在“System.getenv(”JAVA_HOME“)中尝试使用系统变量名称;”方法,它通过获取相应的值工作正常。环境变量中User变量的错误是什么。我应该在代码中做什么吗?
3 个解决方案
#1
I confirm that Windows service runs by default as "Local System Account", which does not access user environment.
我确认Windows服务默认运行为“本地系统帐户”,它不访问用户环境。
You can rather use a user account for the service to Log On.
您可以使用该服务的用户帐户登录。
alt text http://www.adobe.com/devnet/contribute/articles/cps_autodeploy/fig06.gif
替代文字http://www.adobe.com/devnet/contribute/articles/cps_autodeploy/fig06.gif
But be aware that your service will not be aware of environment variable change until your computer reboot! (See This Microsoft technote or this SO question):
但请注意,在您的计算机重启之前,您的服务不会意识到环境变量的变化! (请参阅此Microsoft技术说明或此SO问题):
[
your service]
will inherit its environment from the Services.exe process.
The Services.exe process receives the environment settings for the Local System account when Windows starts.
Because the Services.exe process does not use Windows Messaging, when it receives messages that indicate that a value has changed after Windows starts, the Services.exe process does not update its environment settings.
You cannot force a service that is running to acknowledge a dynamic change to the environment of the Local System account.[您的服务]将从Services.exe进程继承其环境。当Windows启动时,Services.exe进程接收本地系统帐户的环境设置。由于Services.exe进程不使用Windows Messaging,因此当它在Windows启动后收到指示值已更改的消息时,Services.exe进程不会更新其环境设置。您无法强制运行的服务确认对本地系统帐户的环境进行动态更改。
That may not be a problem for JAVA_HOME
(which does not change often), but could be for other dynamically set environment variable.
这可能不是JAVA_HOME(它不经常更改)的问题,但可能是其他动态设置的环境变量。
If this becomes an issue (like, for instance, if you can not start your service with a given user account), you could embed in your java program a run as command in order to query the registry and get the current value of your environment variable
如果这成为一个问题(例如,如果您无法使用给定的用户帐户启动服务),您可以在您的java程序中嵌入run as命令以查询注册表并获取您的环境的当前值变量
String commands [] = new String [] {
"CMD.EXE",
"/C",
"RUNAS /savecred /user:"
+ username
+ " " + "regedit.exe"
};
Runtime.getRuntime().exec(commands);
with username = the login of the user currently connected (if nobody is connected, this all section of your program based on USER environment variables needs to be skipped)
with username =当前连接用户的登录名(如果没有人连接,则需要跳过基于USER环境变量的程序的所有部分)
You will then be able to query the window registry, reading directly the "User variables" stored in it (they are kept in HKEY_CURRENT_USER\Environment
).
然后,您将能够查询窗口注册表,直接读取存储在其中的“用户变量”(它们保存在HKEY_CURRENT_USER \ Environment中)。
#2
Windows services run on behalf of the system, not a particular user. I believe you can configure the service to run as a specific user, at which point you may get the relevant environment variable - but it would be a lot better to configure the application with a properties file or something similar.
Windows服务代表系统运行,而不是特定用户。我相信您可以将服务配置为以特定用户身份运行,此时您可能会获得相关的环境变量 - 但使用属性文件或类似内容配置应用程序会更好。
#3
A windows service gets the user environment of the user the service runs as.
Windows服务获取服务运行的用户的用户环境。
#1
I confirm that Windows service runs by default as "Local System Account", which does not access user environment.
我确认Windows服务默认运行为“本地系统帐户”,它不访问用户环境。
You can rather use a user account for the service to Log On.
您可以使用该服务的用户帐户登录。
alt text http://www.adobe.com/devnet/contribute/articles/cps_autodeploy/fig06.gif
替代文字http://www.adobe.com/devnet/contribute/articles/cps_autodeploy/fig06.gif
But be aware that your service will not be aware of environment variable change until your computer reboot! (See This Microsoft technote or this SO question):
但请注意,在您的计算机重启之前,您的服务不会意识到环境变量的变化! (请参阅此Microsoft技术说明或此SO问题):
[
your service]
will inherit its environment from the Services.exe process.
The Services.exe process receives the environment settings for the Local System account when Windows starts.
Because the Services.exe process does not use Windows Messaging, when it receives messages that indicate that a value has changed after Windows starts, the Services.exe process does not update its environment settings.
You cannot force a service that is running to acknowledge a dynamic change to the environment of the Local System account.[您的服务]将从Services.exe进程继承其环境。当Windows启动时,Services.exe进程接收本地系统帐户的环境设置。由于Services.exe进程不使用Windows Messaging,因此当它在Windows启动后收到指示值已更改的消息时,Services.exe进程不会更新其环境设置。您无法强制运行的服务确认对本地系统帐户的环境进行动态更改。
That may not be a problem for JAVA_HOME
(which does not change often), but could be for other dynamically set environment variable.
这可能不是JAVA_HOME(它不经常更改)的问题,但可能是其他动态设置的环境变量。
If this becomes an issue (like, for instance, if you can not start your service with a given user account), you could embed in your java program a run as command in order to query the registry and get the current value of your environment variable
如果这成为一个问题(例如,如果您无法使用给定的用户帐户启动服务),您可以在您的java程序中嵌入run as命令以查询注册表并获取您的环境的当前值变量
String commands [] = new String [] {
"CMD.EXE",
"/C",
"RUNAS /savecred /user:"
+ username
+ " " + "regedit.exe"
};
Runtime.getRuntime().exec(commands);
with username = the login of the user currently connected (if nobody is connected, this all section of your program based on USER environment variables needs to be skipped)
with username =当前连接用户的登录名(如果没有人连接,则需要跳过基于USER环境变量的程序的所有部分)
You will then be able to query the window registry, reading directly the "User variables" stored in it (they are kept in HKEY_CURRENT_USER\Environment
).
然后,您将能够查询窗口注册表,直接读取存储在其中的“用户变量”(它们保存在HKEY_CURRENT_USER \ Environment中)。
#2
Windows services run on behalf of the system, not a particular user. I believe you can configure the service to run as a specific user, at which point you may get the relevant environment variable - but it would be a lot better to configure the application with a properties file or something similar.
Windows服务代表系统运行,而不是特定用户。我相信您可以将服务配置为以特定用户身份运行,此时您可能会获得相关的环境变量 - 但使用属性文件或类似内容配置应用程序会更好。
#3
A windows service gets the user environment of the user the service runs as.
Windows服务获取服务运行的用户的用户环境。