0

我有代码段:

public static void getOS() throws IOException, InterruptedException
   {
       String[] cmd = {"cat", "/etc/*-release"};
       Process proc = rt.exec(cmd);
       VerboseMode.verboseOut(cmd,proc);

       BufferedReader is = new BufferedReader(new InputStreamReader(proc.getInputStream()));
       line = is.readLine();
       OS = line.trim();
   }

在一个曾经可以工作但由于某种原因停止的程序中。如果我cat /etc/*-release从终端运行我会得到结果,但从 java 我会得到空值。什么不见​​了?

解决它。

我删除了将输出定向到控制台而不是行变量的详细模式行,并更改了 cmd 字符串的格式:

public static void getOS() throws IOException, InterruptedException
   {
       String[] cmd = {"/bin/sh","-c","cat /etc/*-release" };
       Process proc = rt.exec(cmd);

       BufferedReader is = new BufferedReader(new InputStreamReader(proc.getInputStream()));
       line = is.readLine();
       OS = line.trim();
   }

我一定是改了然后忘记了。但是对于我自己的信息,上面显示的两个 cmd 字符串之间的区别是什么,以及我是否将它作为字符串与字符串 [] 传递。据我了解,字符串可能并不总是有效,但 string[] 会。但是 /bin/sh -c 部分我不确定,之前在其他 SO 线程中在线看到过。

4

2 回答 2

1

在 String 内部,您可以提供您正在通过终端运行的脚本

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Multipleprocess {

public static void main(String[] args)throws Exception {
int process=0;
String s[]={"/bin/sh","-c","cat /etc/*-release" };
Process p=Runtime.getRuntime().exec(s);
BufferedReader proc=new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader pout=new BufferedReader(new InputStreamReader(p.getInputStream()));
// We read stderror first from String because it spits the progress information 
//into   stderr

for (String s1=proc.readLine(); s1!=null; s1=proc.readLine())
{
process++;
System.out.println("Stderr from p: "+s);
}
for (String s1=pout.readLine(); s1!=null; s1=pout.readLine())       
{
process++;
System.out.println("Stdout from p: "+s);
}

//how many process have completed check here 
System.out.println("process have completed"+process);
// if you need to check whether the command actually returned normally

int returnCode = p.waitFor();
proc.close();
pout.close();

System.out.println("Process exited with return code "+returnCode);
}
}

我已经检查了它在我的 Eclipse 上运行的程序,你可以使用我的程序,我得到这样的输出:

Stderr from p: [Ljava.lang.String;@186d4c1
Stderr from p: [Ljava.lang.String;@186d4c1
Stderr from p: [Ljava.lang.String;@186d4c1
Stderr from p: [Ljava.lang.String;@186d4c1
process have completed16

进程退出并返回代码 0 您可以在此处看到进程已完成 16

于 2013-08-02T19:11:35.253 回答
0

It looks like Java isn't expanding the asterisk:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetOS {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println(getFirstLine("/etc/*-release"));
        System.out.println(getFirstLine("/etc/os-release"));
    }

    public static String getFirstLine(String file) throws InterruptedException, IOException {
        ProcessBuilder b = new ProcessBuilder();
        b.command("cat", file);
        Process proc = b.start();
        proc.waitFor();
        System.out.println(proc.exitValue());

        BufferedReader is = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        return is.readLine();
    }
}

If you wanted to expand it yourself, you could use something similar to:

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class ExpandGlob {
    public static void main(String[] args) throws IOException {
        System.out.println(expandGlob("/etc/*-release"));
    }

    public static List<Path> expandGlob(String pattern) throws IOException {
        Path p = Paths.get(pattern);
        Path dir = p.getParent();

        List<Path> files = new ArrayList<>();
        try (DirectoryStream<Path> s = Files.newDirectoryStream(dir, p.getFileName().toString())) {
            for (Path f : s) {
                files.add(f);
            }
        }
        return files;
    }
}
于 2013-08-02T18:30:44.237 回答