File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Writing java wrapper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing java wrapper class" Watch "Writing java wrapper class" New topic
Author

Writing java wrapper class

neha priya
Ranch Hand

Joined: Jul 03, 2010
Posts: 62
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
Bartender

Joined: Oct 14, 2005
Posts: 18654
    
    8

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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
neha priya
Ranch Hand

Joined: Jul 03, 2010
Posts: 62
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

Joined: Jul 03, 2010
Posts: 62
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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

Joined: Jul 09, 2008
Posts: 55

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Writing java wrapper class