| 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: 32675
|
|
|
Why are you using "sudo"? That is not usually necessary.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
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.
|
 |
 |
|
|
subject: Trying to run a program in linux from java
|
|
|