java远程执行批处理文件

发布于 2024-09-17 19:13:13 字数 168 浏览 2 评论 0原文

我想执行位于服务器远程的bat文件 \\testserver\someFolderName\test.bat。 我正在使用进程生成器并想使用以下命令更改目录 procbuilder.directory(....),但未能成功。

任何帮助表示赞赏。谢谢

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.

Any help is appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一梦等七年七年为一梦 2024-09-24 19:13:13

过去,我用 PSExec 快速而肮脏地完成了这

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

In the past I've done it quick and dirty with 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.

煞人兵器 2024-09-24 19:13:13

我不认为您可以为 ProcessBuilder 执行 UNC 路径,但在任何情况下这并不重要。

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

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

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

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

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:

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).

似最初 2024-09-24 19:13:13

这是我们当前正在使用的工作代码:

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();
}

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();
}
谈场末日恋爱 2024-09-24 19:13:13

它也可以在 java 中运行,如下所示:

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

it also works in java as below:

Process p1 = Runtime.getRuntime().exec("cmd.exe /C pushd \\yourserver\yourfolderpath && yourexecutable.bat && popd");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文