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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 32675
    
    4
Why are you using "sudo"? That is not usually necessary.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Trying to run a program in linux from java
 
Similar Threads
Question About Usin FTP Dos Command in JAVA
execute exe from cmd line
Calling script
Different reults of execution of jar from terminal and by double clicking
Compile and create instance of a new java program from existing java program