Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

executing linux commands through java socket

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic