0

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

4
  • Do you have ssh installed on the remote machine? Commented May 24, 2016 at 7:02
  • Y must use kind of protocol, open firewals for this protocol .... question is very broad Commented May 24, 2016 at 7:04
  • @ZeusNet ssh installed but I'm looking for a generic solution. Commented May 24, 2016 at 7:14
  • Then you should have a look at RMI. Commented May 24, 2016 at 7:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Couldn't access rhost: Access is denied. While executing the psExec manually.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.