如何在Java中运行Linux命令?

chi*_*esh 41 java runtime.exec

我想创建两个文件的差异.我尝试在Java中搜索代码,但没有找到任何简单的代码/实用程序代码.因此,我想如果我能以某种方式从我的java代码运行linux diff/sdiff命令并使其返回存储diff的文件,那么它会很棒.

假设有两个文件fileA和fileB.我应该能够通过我的java代码将他们的diff存储在一个名为fileDiff的文件中.然后从fileDiff获取数据将没什么大不了的.

pax*_*blo 47

您可以使用它java.lang.Runtime.exec来运行简单的代码.这样Process您就可以直接读取其标准输出,而无需将输出临时存储在磁盘上.

例如,这是一个完整的程序,将展示如何做到这一点:

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) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

编译和运行时,它输出:

line: ./
line: ../
line: .classpath*
line: .project*
line: bin/
line: src/
exit: 0
Run Code Online (Sandbox Code Playgroud)

正如所料.

您还可以获得过程标准错误的错误流,以及过程标准输入的输出流,令人困惑.在这种情况下,输入和输出是相反的,因为它是过程输入到此过程(即过程的标准输出).

如果要从Java合并进程标准输出和错误(而不是2>&1在实际命令中使用),则应该查看ProcessBuilder.


mor*_*ort 24

看一下Runtime.exec()方法:http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec( java.lang.String)


小智 16

您还可以编写shell脚本文件并从java代码中调用该文件.如下所示

{
   Process proc = Runtime.getRuntime().exec("./your_script.sh");                        
   proc.waitFor();
}
Run Code Online (Sandbox Code Playgroud)

在脚本文件中编写linux命令,一旦执行完毕,就可以在Java中读取diff文件.

这种方法的优点是您可以在不更改Java代码的情况下更改命令.


cod*_*ict 12

您不需要将diff存储在第3个文件中,然后从中读取.相反,您可以使用 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);
}
Run Code Online (Sandbox Code Playgroud)


b10*_*n1k 6

尝试使用unix4j.它是关于java中运行linux命令的库.例如,如果你得到一个命令:cat test.txt | grep"星期二"| sed"s/kilogram/kg/g"| 在这个程序中排序将变为:Unix4j.cat("test.txt").grep("Tuesday").sed("s/kilogram/kg/g").sort();


YoK*_*YoK 5

Runtime run = Runtime.getRuntime();  
//The best possible I found is to construct a command which you want to execute  
//as a string and use that in exec. If the batch file takes command line arguments  
//the command can be constructed a array of strings and pass the array as input to  
//the exec method. The command can also be passed externally as input to the method.  

Process p = null;  
String cmd = "ls";  
try {  
    p = run.exec(cmd);  

    p.getErrorStream();  
    p.waitFor();

}  
catch (IOException e) {  
    e.printStackTrace();  
    System.out.println("ERROR.RUNNING.CMD");  

}finally{
    p.destroy();
}  
Run Code Online (Sandbox Code Playgroud)


Sar*_*dra 5

您可以从WindowsLinux的 java 调用运行时命令。

import java.io.*;

public class Test{
   public static void main(String[] args) 
   {
            try
            { 
            Process process = Runtime.getRuntime().exec("pwd"); // for Linux
            //Process process = Runtime.getRuntime().exec("cmd /c dir"); //for Windows

            process.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
               while ((line=reader.readLine())!=null)
               {
                System.out.println(line);   
                }
             }       
                catch(Exception e)
             { 
                 System.out.println(e); 
             }
             finally
             {
               process.destroy();
             }  
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.. :)