java 远程执行批处理文件java

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

execute batch file remotely java

javaprocessbuilder

提问by user234194

I want to execute a bat file located remotely on server \\testserver\someFolderName\test.bat. I am using process builder and wanted to chande the directory with procbuilder.directory(....), but could not succeed.

我想在服务器上远程执行一个 bat 文件 \\testserver\someFolderName\test.bat。我正在使用流程构建器并希望使用该目录进行更改, procbuilder.directory(....), 但未能成功。

Any help is appreciated. Thanks

任何帮助表示赞赏。谢谢

回答by Andrew

In the past I've done it quick and dirty with PSExec

过去,我用PSExec完成得又快又脏

Just start that from your program as its own process with the required arguments to gain access to the batch on the remote computer.

只需从您的程序作为它自己的进程启动它,并使用所需的参数来访问远程计算机上的批处理。

回答by NagendraPrakash Karri

This is working code that we are using currently:

这是我们目前使用的工作代码:

try {            
   ProcessBuilder launcher = new ProcessBuilder();
   Map<String, String> environment = launcher.environment();
   launcher.redirectErrorStream(true);
   launcher.directory(new File("\\<your remote computer name>\TIERS\DEV1\RP\VISUAL_BASIC\"));

   environment.put("name", "var");
   launcher.command("your.exe");
   Process p = launcher.start(); // And launch a new process

} catch (Exception e){
   e.printStackTrace();
}

回答by Ankit Kachchhi

it also works in java as below:

它也适用于 Java,如下所示:

Process p1 = Runtime.getRuntime().exec("cmd.exe /C pushd \yourserver\yourfolderpath && yourexecutable.bat && popd");

回答by Riaan Cornelius

I don't think you can do UNC paths for the ProcessBuilder, but it doesn't really matter in any case.

我不认为你可以为 ProcessBuilder 做 UNC 路径,但这在任何情况下都无关紧要。

To run a .bat file, you need to run a windows command shell and have that execute the .bat file, and the command shell doesn't support UNC paths... The way around it is to run your command like this:

要运行 .bat 文件,您需要运行 Windows 命令外壳并让它执行 .bat 文件,而命令外壳不支持 UNC 路径......解决它的方法是像这样运行你的命令:

cmd.exe /C "pushd \testserver\someFolderName && test.bat && popd"

Essentially, you're telling the cmd prompt to mount your remote folder as a temporary drive (pushd \testserver\someFolderName), run test.bat and then unmount the temporary drive (popd).

本质上,您是在告诉 cmd 提示符将远程文件夹安装为临时驱动器 (pushd \testserver\someFolderName),运行 test.bat,然后卸载临时驱动器 (popd)。