aspose file tools*
The moose likes Beginning Java and the fly likes Trying to run a program in linux from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to run a program in linux from java" Watch "Trying to run a program in linux from java" New topic
Author

Trying to run a program in linux from java

Brad Sturgis
Greenhorn

Joined: Mar 29, 2009
Posts: 1
From Terminal, I can run a command line program in ubuntu. The following would be the command line:
sudo /home/wksadmin/Private/MyProgram.bin hostname username password &. The program requires "programName with args hostname username password &.

I wrote the following that works but I don't want to call the program from a bash file:

I need to prompt first for username, then password, then insert it into the line below but when I tried to write the line directly, it doesn't run.

I want to run Process proc = Runtime.getRuntime().exec("sudo /home/wksadmin/Private/myprogram.bin hostname username password &");


How would I go this. Help would be appreciated.

Thanks.
Sona Patel
Ranch Hand

Joined: Mar 30, 2009
Posts: 75
Hi

Try like this

String[] cmd = {"/bin/bash", "-c", "sudo /home/wksadmin/Private/MyProgram.bin" , "hostname", "username", "password" };
Runtime.getRuntime().exec( cmd );

Refer below link
http://mindprod.com/jgloss/exec.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Why are you using "sudo"? That is not usually necessary.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
There may be an easier way. I found I have a directory in ~ called java, and that has StringEqualityTest.class in. So I wrote a .sh file
[Campbell@localhost java]$ java StringEqualityTest
true,truetrue[Campbell@localhost java]$ gedit StringEqualityTest.sh
[Campbell@localhost java]$ ./StringEqualityTest.sh
bash: ./StringEqualityTest.sh: Permission denied
[Campbell@localhost java]$ chmod +x StringEqualityTest.sh
[Campbell@localhost java]$ ./StringEqualityTest.sh
true,truetrue
And this is what the .sh file looks like
cd ~/java
java StringEqualityTest
Note the chmod +x instruction which appears to be essential.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to run a program in linux from java