在java中运行top命令,在linux中columns=512

问题描述 投票:0回答:1

我正在尝试运行此命令以获取特定格式的顶部输出

COLUMNS=512 top -c -H -n 1 -b | head -n 27 | tail -n 21

使用流程构建器

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

public class CommandReporter {
    public static String runCommand(String command) throws IOException {
        final StringBuilder output = new StringBuilder();

        final Process process = new ProcessBuilder()
                .command(command.split("\\s+"))
                .redirectErrorStream(true)
                .start();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
        }


        return output.toString();
    }
}

当我为此编写测试时

@Test
void testRunTopCommand() throws IOException {
    String result = CommandReporter.runCommand("COLUMNS=512 top -c -H -n 1 -b | head -n 27 | tail -n 21");
    assertNotNull(result); // Note I will write better tests once i get the command to run
}

我明白了

java.io.IOException: Cannot run program "COLUMNS=512": error=2, No such file or directory
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
    at com.amazon.samplelib.CommandReporter.runCommand(CommandReporter.java:14)
    at com.amazon.samplelib.CommandReporterTest.testRunTopCommand(CommandReporterTest.java:19)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)

不确定这里如何进展。有人可以帮忙吗?

java processbuilder
1个回答
0
投票

这似乎有效

final Process process = new ProcessBuilder()
    .command("bash", "-c", command)
    .redirectErrorStream(true)
    .start();

© www.soinside.com 2019 - 2024. All rights reserved.