0
我需要执行位于不同机器上的.cmd
文件。我可以访问文件系统,机器,如:\\IP\Location\File.cmd
在不同的机器上执行bat文件并获取输出
我的代码为:
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class GetResponseMain {
//This is Example that display how to get response using java
public static void main(String args[])
{
String filePath = "\\\\255.255.255.0\\Scripts\\ClearCache.cmd";
try {
Process p = Runtime.getRuntime().exec(filePath);
p.waitFor();
InputStream in = p.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = -1;
while((c = in.read()) != -1)
{
baos.write(c);
}
String response = new String(baos.toByteArray());
System.out.println("Response: "+response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
此代码应该打印输出。但它显示.cmd
文件的内容。
我认为,而不是在该机器上执行脚本。它在本地执行它。 那么我该如何解决这个问题?
那么在批处理文件的世界里,我们通常使用微软的PSEXEC工具来在远程计算机上执行代码。 – Squashman
@Squashman你能解释一下吗? – Nivedita
我对java一无所知,所以我无法解释如何使它在java中工作。我只知道如果我需要在远程计算机上执行批处理文件,我使用psexec。 https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx – Squashman