How to implement the following linux command in Java
You can run it as a process within Java as shown below, but remember this will only work in Linux or Mac environments.
Process p = Runtime.getRuntime().exec("grep \"Exception\" /home/admin/logs/common-error.log |sort |uniq -c |sort -nr
");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
System.out.println(output.toString());
Executing linux command in java and store the output in variable
First, don't use Runtime.exec. Always use ProcessBuilder, which has much more reliable and predictable behavior.
Second, everything in your string is being passed as a single command, including the vertical bars. You are not passing it to a shell (like /bin/bash), so piping commands to one another won't work.
Third, you need to consume the command's output as it is running. If you call waitFor()
and then try to read the output, it may work sometimes, but it will also fail much of the time. This behavior is highly operating system dependent, and can even vary among different Linux and Unix kernels and shells.
Finally, you don't need grep
or wc
. You have Java. Doing the same thing in Java is pretty easy (and probably easier than trying to invoke a shell so piping will work):
ProcessBuilder builder =
new ProcessBuilder("asterisk", "-rx", "ss7 linestat");
builder.inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE);
Process p = builder.start();
int freeE1s = 0;
try (BufferedReader buf =
new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = buf.readLine()) != null) {
if (line.contains("Idle")) { // No need for 'grep'
freeE1s++; // No need for 'wc'
}
}
}
p.waitFor();
System.out.println(freeE1s);
As of Java 8, you can make it even shorter:
ProcessBuilder builder =
new ProcessBuilder("asterisk", "-rx", "ss7 linestat");
builder.inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE);
Process p = builder.start();
long freeE1s;
try (BufferedReader buf =
new BufferedReader(new InputStreamReader(p.getInputStream()))) {
freeE1s = buf.lines().filter(line -> line.contains("Idle")).count();
}
p.waitFor();
System.out.println(freeE1s);
How to run linux command from java
Only a shell understands pipes, you can invoke the shell with the command you want to run:
exec(new String[]{"/bin/sh", "-c", "rpm -qa | grep \"Uploader\" | xargs rpm --queryformat \"%{VERSION}\" -q"});
How do i run a Linux terminal cmd from a java program
You can use the below command format to run your Linux command.
Runtime r = Runtime.getRuntime();
Process p = r.exec(yourcmd);
Please go through Running unix command from Java and Unable to run Unix command in Java-Stackoverflow
Hope you get your answers here.
Related Topics
Android Listview Selected Item Stay Highlighted
Why Does Arrayindexoutofboundsexception Occur and How to Avoid It in Android
Firestore - Merging Two Queries Locally
Clicking the Back Button Twice to Exit an Activity
Android - Save Image into Gallery
Set Margins in a Linearlayout Programmatically
Detecting a Long Press in Android
Manifest Merger Failed:Attribute Application@Appcomponentfactory Cant Solve This
Android Custom Row Item for Listview
How to Set Selected Item of Spinner by Value, Not by Position
Firestore - How to Structure a Feed and Follow System
How to Execute Dex: Multiple Dex Files Define
How to Parse JSON Parsing Using Gson in Android
Sending Arrays with Intent.Putextra