-1
我必須從本地機器執行遠程機器上存在的批處理文件。如何在java中遠程執行批處理文件?
我通過FTP連接到遠程計算機,並嘗試執行批處理文件remotely.but不能夠做到這一點也並不getiing任何異常。下面是我的代碼
public static void main(String[] args) throws SocketException, IOException,
InterruptedException {
static String server = "192.168.2.133";
static int port = 21;
static String user = "kamal";
static String pass = "12345";
static FTPClient ftpClient;
// TODO Auto-generated method stub
ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory("/");
System.out.println(ftpClient.isConnected());
// ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println("ftpClient.printWorkingDirectory() "
+ ftpClient.printWorkingDirectory());
String[] str = ftpClient.listNames(ftpClient.printWorkingDirectory());
for (String string : str) {
System.out.println(string);
}
// ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory().concat("demo"));
System.out.println("ftpClient.printWorkingDirectory() "
+ ftpClient.printWorkingDirectory());
String psCommand = "C:/Users/kamlesh.kumar/Downloads/PSTools/PsExec.exe //"
+ server + " -u " + user + " -p " + pass;
psCommand = psCommand + " " + "MEF.bat";
// psCommand = psCommand + " " + commandToRunOnRemoteMachine + " " +
// parameters;
String[] cmd = new String[5];
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[2] = psCommand;
cmd[3] = "";
cmd[4] = "";
// Run remote command
// ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory().concat("\\\\192.168.2.135\\demo\\demo"));
// System.out.println(ftpClient.printWorkingDirectory());
File f = new File(ftpClient.printWorkingDirectory());
Process p = null;
try {
p = Runtime.getRuntime().exec(cmd, null, f);
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
int exitStatus = 0;
try {
exitStatus = p.waitFor();
System.out.println("exitStatus in try " + exitStatus);
} catch (Exception e) {
System.out.println("exitStatus" + exitStatus);
System.out.println(e.toString());
e.printStackTrace();
} finally {
System.out.println("exitStatus in finally " + exitStatus);
}
if (exitStatus == 0) {
System.out.println("Done");
}
}
你不能用FTP遠程執行文件。看看使用微軟的PSEXEC實用程序。 – Squashman
ftp僅用於文件傳輸。最常用的遠程執行工具是PSexec,但從[java](http://forum.sysinternals.com/faq-common-pstools-issues_topic15920.html)調用時會遇到問題。您可以檢查SCHTASKS或WMIC幫助,以瞭解如何在沒有第三方工具的情況下遠程執行腳本。 – npocmaka
您的問題的一個答案是RMI。你甚至將它包含在標籤中。那你爲什麼試圖使用FTP? – EJP