0

I had tested the following methods to execute Linux command from my program

Method 1 : Assign all into a string

String temp1 = "'/"+t2+"/,/"+t1+"/p'";
String command2 = "sed -n "+temp1+" app.log";
Process p1 = Runtime.getRuntime().exec(command2);



Method 2 : use array

String [] command2 = new String []{"sed","-n","'/",t2,"/,/",t1,"/p'", "app.log";
System.out.println("The command2 is : "+Arrays.toString(command2);
Process p2 = new ProcessBuilder(command2).start();



This my reference link for the method 2 but both of the methods not working at all. This is the command I hope to run in the terminal
sed -n '/14:32:54/,/14:33:44/p' app.log

This is a portion of my code for calling the system command, nothing displayed in line2 variable

String [] command2 = new String []{"sed","-n","'/",t2,"/,/",t1,"/p'","stlog.txt"};
Process p2 = new ProcessBuilder(command2).start();
BufferedReader br2 = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line2;
while((line2 = br2.readLine()) != null)
   {
      System.out.println(line2);
   }
yumi
  • 153
  • 8
  • Won't the above execute `sed -n '/ t2 /,/ t1 /p' stlog.txt`, ie with a whole lot of unnecessary spaces in your regex, or even worse, pass in seven parameters to `sed`? `"sed","-n","'/"+t2+"/,/"+t1+"/p'","stlog.txt"` might be one quick solution. – Ken Y-N Jun 01 '17 at 09:38
  • able to provide more explanation on the quick solution ? – yumi Jun 01 '17 at 09:50
  • This is often a PATH problem... Did you try to pass the full path of `sed` (generally `/usr/bin/sed`)? – Serge Ballesta Jun 01 '17 at 09:55
  • definitely not, do it need a full path ? please advise, i am new to Linux – yumi Jun 01 '17 at 10:01

1 Answers1

1

In my case worked:

 ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", "pwd")
                    .directory(new File("some.directory.path"));
 Process process = processBuilder.start();

Or you can sip using ProcessBuilder and just call

String command = "ping www.google.com";
String[] commandArray = {"/bin/bash", "-c", "pwd"}; 
Runtime.getRuntime().exec(commandArray);

"/bin/bash" 0 means that you are going to exec command in bach

"-c" -defines that next param is command

command - any command like "ping www.google.com" or "./script.sh" that you execute with terminal

you should just place your command instead of "ping www.google.com", but as you haven't specified directory - script will be executed from project directory (you can check it by executing "pwd" command that prints current directory). That is why ProcessBuilder is more preferable, as you can indicate execution directory there (replace "some.directory.path" with your dir).

.directory(new File("path/to/some/dir"));
Vitaliy Moskalyuk
  • 2,463
  • 13
  • 15
  • so you mean I need to replace the "pwd" with my "sed" ? – yumi Jun 01 '17 at 09:47
  • i had tested with a short command like "ping www.yahoo.com" or "grep apple app.log", it's works. When the command become more longer, it's cant call out by using the program – yumi Jun 01 '17 at 09:50
  • I mean that you need to pass all 3 params ("/bin/bash", "-c", command), where command = real command you want to execute in terminal – Vitaliy Moskalyuk Jun 01 '17 at 09:50
  • I provided in the post, will try out any others longer command – yumi Jun 01 '17 at 10:00
  • it's show me this error in my terminal "no suitable method found for exec(String,String,String) " when i pass in all three param – yumi Jun 01 '17 at 10:12
  • try String[] cmdAray = {"/bin/bash", "-c", "pwd"}; Runtime.getRuntime().exec(cmdAray) or better just use ProcessBuilder as i described above. Is that works? – Vitaliy Moskalyuk Jun 01 '17 at 10:17
  • It's still having same error. I posted out portion of my code, please visit this link(http://codepad.org/eiTnjm9I) to make sure that we are in the same page. If you want a full program,i can share with you also – yumi Jun 01 '17 at 10:33
  • in row 6 there's no need to add "/bin/bash","-c" , as you have already added it to cmdArry variable. So line 6 should be like "Process p2 = Runtime.getRuntime().exec(cmdArry); " – Vitaliy Moskalyuk Jun 01 '17 at 10:51
  • so here is ProcessBuilder realization http://codepad.org/cfmGSJet , and here is by just process "http://codepad.org/w90VbQvd" but in that case you should be sure in which directory you execute command, as file could be not visible from it, so you need to use full path to file – Vitaliy Moskalyuk Jun 01 '17 at 10:59
  • thank you for your assistance from the others site of the world although we don't know each others. It's works now, have a nice day – yumi Jun 02 '17 at 01:57
  • i've spend some hours making same thing, so it's a pleasure to share my knowledge) have a nice day) – Vitaliy Moskalyuk Jun 02 '17 at 07:15