0

How to run command-line or execute external application which is hosted in a remote windows machine using java.

AKR
  • 359
  • 1
  • 5
  • 11

1 Answers1

2

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.

Community
  • 1
  • 1
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • Couldn't access rhost: Access is denied. While executing the psExec manually. – AKR May 24 '16 at 13:19
  • I don't know much about that utility , was just explaining that you will need a tool like that. Searching for that error tells me that user ( local or specified in command ) must have admin rights on remote machine. – Sabir Khan May 25 '16 at 09:22