Reputation: 359
How to run command-line or execute external application which is hosted in a remote windows machine using java.
Upvotes: 0
Views: 8992
Reputation: 10142
In addition to Java, you need a tool that can connect to remote Windows machine and execute commands . PsExec
is one such utility.
Refer link psexec.exe for more details.
In above link, utility usage is mentioned as below and cmd
is the command that you wish to execute and arguments are arguments to that command.
Usage: psexec [\\computer[,computer2[,...] | @file]][-u user [-p psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-a n,n,...] cmd [arguments]
then in Java , you can use something like ,
Process p = Runtime.getRuntime().exec(String[] cmdarray, String[] envp, File dir);
you can populate cmdarray
as , cmdarray[0]="cmd.exe"
, cmdarray[1]="/C"
to launch command prompt on your local machine then cmdarray[2]="psexec command String"
( this String you have to construct as per usage described above ) .
you can download and install utility if its missing on your system.
you will be required to use third party APIs if planning to use SSH
connectivity.
Hope it helps !!
Credit goes to Programmatically connecting to remote systems
See this SO question too.
Upvotes: 2