wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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: 38046
    
  22
Why are you using "sudo"? That is not usually necessary.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Trying to run a program in linux from java
 
Similar Threads
Compile and create instance of a new java program from existing java program
Question About Usin FTP Dos Command in JAVA
Calling script
execute exe from cmd line
Different reults of execution of jar from terminal and by double clicking