Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Linux commands in Java?

I want to create diff of two files. I tried searching for code in Java that does it, but didnt find any simple code/ utility code for this. Hence, I thought if I can somehow run linux diff/sdiff command from my java code and make it return a file that stores the diff then it would be great.

Suppose there are two files fileA and fileB. I should be able to store their diff in a file called fileDiff through my java code. Then fetching data from fileDiff would be no big deal.

like image 921
chitresh Avatar asked Aug 04 '10 06:08

chitresh


People also ask

Can we run Linux command in Java?

You can use java. lang. Runtime. exec to run simple code.

What is Java command in Linux?

A Java compiler compiles the source code of a Java program to obtain a bytecode for the particular source code. To compile a Java program source code, the javac command is used. javac FileName.java.


3 Answers

You can use java.lang.Runtime.exec to run simple code. This gives you back a Process and you can read its standard output directly without having to temporarily store the output on disk.

For example, here's a complete program that will showcase how to do it:

import java.io.BufferedReader; import java.io.InputStreamReader;  public class testprog {     public static void main(String args[]) {         String s;         Process p;         try {             p = Runtime.getRuntime().exec("ls -aF");             BufferedReader br = new BufferedReader(                 new InputStreamReader(p.getInputStream()));             while ((s = br.readLine()) != null)                 System.out.println("line: " + s);             p.waitFor();             System.out.println ("exit: " + p.exitValue());             p.destroy();         } catch (Exception e) {}     } } 

When compiled and run, it outputs:

line: ./ line: ../ line: .classpath* line: .project* line: bin/ line: src/ exit: 0 

as expected.

You can also get the error stream for the process standard error, and output stream for the process standard input, confusingly enough. In this context, the input and output are reversed since it's input from the process to this one (i.e., the standard output of the process).

If you want to merge the process standard output and error from Java (as opposed to using 2>&1 in the actual command), you should look into ProcessBuilder.

like image 71
paxdiablo Avatar answered Sep 22 '22 08:09

paxdiablo


You can also write a shell script file and invoke that file from the java code. as shown below

{
   Process proc = Runtime.getRuntime().exec("./your_script.sh");                        
   proc.waitFor();
}

Write the linux commands in the script file, once the execution is over you can read the diff file in Java.

The advantage with this approach is you can change the commands with out changing java code.

like image 39
Naveen Avatar answered Sep 20 '22 08:09

Naveen


You need not store the diff in a 3rd file and then read from in. Instead you make use of the Runtime.exec

Process p = Runtime.getRuntime().exec("diff fileA fileB");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
}
like image 41
codaddict Avatar answered Sep 18 '22 08:09

codaddict