I have the code segment:
我有代码段:
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();
}
in a program that used to work, but stopped for some reason. If I run cat /etc/*-release
from terminal I get results, but from java I get null. What is missing?
曾经工作的程序,但由于某种原因停止了。如果我运行cat / etc / * - 从终端发布我得到结果,但是从java我得到null。什么不见了?
Fixed it.
修复。
I removed the verbose mode line which was directing the output to console instead of the line variable, and changed the format of the cmd string:
我删除了详细模式行,它将输出定向到控制台而不是行变量,并更改了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();
}
I must have changed it and forgot. But for my own info, whats the difference between the two cmd strings shown above, as well as if I passed it in as just a string vs. string[]. It's my understanding that a string may not always work, but a string[] will. but the /bin/sh -c part I'm not sure about, just saw it online before in other S.O. threads.
我必须改变它并忘了。但是对于我自己的信息,我看到上面显示的两个cmd字符串之间的区别,以及我是否仅将其作为字符串与字符串[]传递。我的理解是字符串可能并不总是有效,但字符串[]会有效。但/ bin / sh -c部分我不确定,只是在其他S.O.之前在网上看过它。线程。
2 个解决方案
#1
1
Inside String you can provide your script which is you are running through terminal
在字符串内,您可以提供正在通过终端运行的脚本
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);
}
}
I have checked my programme it running on my eclipse, you can use my programme,I got output like this:
我检查了我在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
Process exited with return code 0 You can see here process completed 16
流程退出并返回代码0您可以在此处看到流程已完成16
#2
0
It looks like Java isn't expanding the asterisk:
看起来Java没有扩展星号:
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;
}
}
#1
1
Inside String you can provide your script which is you are running through terminal
在字符串内,您可以提供正在通过终端运行的脚本
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);
}
}
I have checked my programme it running on my eclipse, you can use my programme,I got output like this:
我检查了我在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
Process exited with return code 0 You can see here process completed 16
流程退出并返回代码0您可以在此处看到流程已完成16
#2
0
It looks like Java isn't expanding the asterisk:
看起来Java没有扩展星号:
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;
}
}