This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes executing linux commands through java socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "executing linux commands through java socket" Watch "executing linux commands through java socket" New topic
Author

executing linux commands through java socket

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I need to connect th elinux machine through socket and need to execute the linux commands using java when i run the java application from any machine.I tried the following.But i am unable to execute the command when i run the program from windows.But if i run the program from linux,it works well.please correct me where am i wrong and please assist me to do this.



when running the above code,i am getting the following error:

Asterisk Call Manager/1.0
exception happened - here's what I know:
java.io.IOException: Cannot run program "ps": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at RunCommand.main(RunCommand.java:20)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more



Thanks
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
the reason why you are getting this exception is because java is trying to execute the command in the same jvm [windows] itself, and not the remote linux machine as you intended to/wanted to. You might want to get your hands on any of the free ssh libraries available on the net [trilead, is a personal favourite of mine... or even openssh]. Once you have this library [a jar basically], you can invoke the apis provided [create a session with the IP and port of the remote linux box] , and execute the commands.... trust me, it is pretty simple.

Dawn.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

As you have said that in linux its working find and not in windows, the reason is windows does not have any command as ps.


Swastik
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: executing linux commands through java socket
 
Similar Threads
Unix With Java....
How to connect to Linux using Java code?
Reg:Executing Commands
java program run external command on Unix
Pesky Runtime method