I want to execute a bat file located remotely on开发者_如何学Go 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
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.
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).
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();
}
it also works in java as below:
Process p1 = Runtime.getRuntime().exec("cmd.exe /C pushd \\yourserver\yourfolderpath && yourexecutable.bat && popd");
精彩评论