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?
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:
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 SO threads.
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:
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
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.