简体   繁体   English

如何从Windows服务调用批处理文件?

[英]How to call a batch file from a windows service?

we have a java application, which is running as windows service. 我们有一个Java应用程序,它作为Windows服务运行。 Actually we need to call a batch file to start windows 'calculator' application. 实际上,我们需要调用一个批处理文件来启动Windows“计算器”应用程序。 But we can't. 但是我们不能。 If we start java application not as a windows service, it is working fine. 如果我们不将Java应用程序作为Windows服务启动,则它工作正常。 I have read a post about the same on Calling a batch file from a windows service and done the below configuration. 我已经阅读了有关从Windows服务调用批处理文件的文章,并进行了以下配置。

"first install the service with giving appropriate path to the batch file or exe file then go to run->services.msc->right click on the service ->properties->logon->check enable service to interact with desktop-make it enable " “首先安装该服务,并提供批处理文件或exe文件的适当路径,然后转到run-> services.msc->右键单击该服务-> properties-> logon->选中启用服务以与桌面进行交互,使其生效启用“

After that we got a partial success, as it first prompts a permission window as below: 之后,我们获得了部分成功,因为它首先会提示如下所示的权限窗口:

May I suggest that instead of opening a batch file, you just open the calc.exe file instead? 我是否可以建议您打开calc.exe文件而不是打开批处理文件?

Runtime.getRuntime().exec("c:\\windows\\System32\\calc.exe", null, new File("c:\\windows\\System32\\calc.exe"));

EDIT: If you still want to run it as a batch file, use this: 编辑:如果您仍然想将其作为批处理文件运行,请使用以下命令:

Runtime.getRuntime().exec("cmd /c start nameOfTheBatchFile.bat");

Please note that the second variable in the exec method is to set up the directory that you want to be calling the program from. 请注意,exec方法中的第二个变量是设置您要从中调用程序的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM