Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing java wrapper class

 
neha priya
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to execute a service remotely.To achieve this i run the command PsExec.exe from command prompt.However instead of executing the command manually from the command line i would like to write some java wrapper class that would internally invoke the PsExec.Exe command and fetch the desired result.Kndly help as how do i write such a java class?

Regards
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use a ProcessBuilder object. Read the API documentation carefully while you're writing the code. And if you run into problems, you may find the answer in this classic article.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code:
And since you're going to be using Process, you need to read When Runtime.exec() won't. In this case you get off easy; if you redirect the error stream to the output stream (using the appropriate method from ProcessBuilder) everything will be sent to the same buffer which you are emptying when reading from p.getInputStream().

Edit: beaten to it by Paul.
 
neha priya
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.Presently i am just trying PsExec.exe to login to the remote machine and ping my machine from there.I run the following command from command prompt:-




I am able to ping in this manner.Please help how do i do the same using ProcessBuilder so that i don't have to use type the command manually on command prompt?

Regards.
 
neha priya
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wrote the following java class according to the suggestions provided by you all.It tries to login to the remote machine and then ping the local system:-

However i am not sure if the code is working or not?Because when i print the line read from the inputstream it gives NULL.
kindly help me to solve this problem.

Regards.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neha priya wrote:

I see two problems here:
1) you use \\, but that is only one \ in the Java String because \ is the escape character for Strings. To put two backslashes in a Java String you must add 4 in code: "\\10.22.22.33"

2) the ProcessBuilder constructor accepts multiple Strings, but it must have one String per command line argument. In your example PsExec is started with one long argument. Try the following:
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neha,

Were you able to connect using above approach?

I am also stuck with same issue, so please suggest if you found a way
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic