• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to run a program in linux from java

 
Brad Sturgis
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using "sudo"? That is not usually necessary.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic