I would like to check distro name, but I have got problem with bash executing command. Why this code works ok and print folder content
String cmd[] = {"ls","-a"};
Runtime run = Runtime.getRuntime();
try {
Process proc = run.exec(cmd);
BufferedReader read=new BufferedReader(new InputStreamReader(proc.getInputStream()));
while(read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
But cmd[] = {"cat","/etc/*-release"}; doesnt? It simply doesn’t print anything, neither error nor distro. Ofc. it works in terminal. What is wrong with that?
The reason it works in Bash is that Bash recognizes
/etc/*-releaseas a glob and performs the necessary filename-expansion.Processdoesn’t do that; it just callscatwith the exact argument you specify. (In other words, you’re running the equivalent of the Bash commandcat '/etc/*-release'.)One option, I suppose, is to actually call Bash and let it handle that for you:
but I think it makes more sense to use the Java file-system API to search
/etcfor a file whose name ends in-release, and read that file’s contents normally. (See the Javadoc forjava.io.Fileand the Javadoc forjava.io.FileReader.)