Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java运行命令时的不同输出_Java_Shell - Fatal编程技术网

从java运行命令时的不同输出

从java运行命令时的不同输出,java,shell,Java,Shell,我有以下脚本: #!/bin/bash ID=$PPID read PID < <(exec ps -o ppid= "$ID") top -cbn 1 -p $PID grep -f <(pstree -cp $PID | grep -Po '\(\K\d+'| sed -re 's/$/ /g' | sed -re 's/^/^\\s\*/g' ) <(top -cbn 1) 注意命令的输出,它显示了完整的命令。但是如果我从java运行相同的程序,输出会截断命令名,

我有以下脚本:

#!/bin/bash
ID=$PPID
read PID < <(exec ps -o ppid= "$ID")
top -cbn 1 -p $PID
grep -f <(pstree -cp $PID | grep -Po '\(\K\d+'| sed -re 's/$/ /g' | sed -re 's/^/^\\s\*/g' ) <(top -cbn 1)
注意
命令的输出,它显示了完整的命令。但是如果我从java运行相同的程序,输出会截断命令名,我不知道为什么

截断发生在19字符之后。 或用不同的词 每行在80个字符后截断

如果我使用Java运行相同的程序,下面是输出

top - 16:13:52 up  6:10,  6 users,  load average: 0.16, 0.16, 0.06
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.9%us,  0.6%sy,  0.0%ni, 98.0%id,  0.4%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4152800k total,  1913364k used,  2239436k free,    91560k buffers
Swap:  1048568k total,        0k used,  1048568k free,  1103952k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
16750 builder   40   0 1206m 257m  15m S  0.0  6.3   0:26.32 /opt/ibm/java-i386-

16750 builder   40   0 1206m 257m  15m S  1.9  6.3   0:26.33 /opt/ibm/java-i386-
16943 builder   20   0  2608 1008  748 R  1.9  0.0   0:00.02 top -cbn 1         
16918 builder   20   0  554m  14m 6060 S  0.0  0.4   0:00.10 /opt/ibm/java-i386-
16934 builder   20   0  4976 1120  992 S  0.0  0.0   0:00.00 /bin/bash /home/bui
16941 builder   20   0  4976  508  376 S  0.0  0.0   0:00.00 /bin/bash /home/bui
16942 builder   20   0  4388  888  608 S  0.0  0.0   0:00.00 grep -f /dev/fd/63
运行该命令的java文件是

public class InformationFetcher {
    public static void main(String[] args) {
    InformationFetcher informationFetcher = new InformationFetcher();
    try {
        Process process = Runtime.getRuntime().exec(
            informationFetcher.getFilePath());
        InputStream in = process.getInputStream();
        printInputStream(in);
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

    private void processInformation(InputStream in) {
    topProcessor.process(in);
    }

    private static void printInputStream(InputStream in) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuffer outBuffer = new StringBuffer();
    String newLine = System.getProperty("line.separator");
    String line;
    while ((line = reader.readLine()) != null) {
        outBuffer.append(line);
        outBuffer.append(newLine);
    }
    System.out.println(outBuffer.toString());
    }

    public String getFilePath() {
    return this.getClass().getResource("/idFetcher.sh").getPath();
    }

}

我猜你已经调试了你的程序,并检查了
是否包含整行?@Tom是的,先生,我做得很好。一项小型研究得出的信息是,
BufferedReader
类中的标准行长度设置为80个字符。您应该检查如何增加该设置。May bis可以帮助您:这里,top的手册页(version procps ng version 3.3.8)说(for-w switch):
在没有参数的情况下使用时,top将使用COLUMNS=和LINES=环境变量格式化输出
。您是否尝试过设置环境变量
或传递
-w NNN
public class InformationFetcher {
    public static void main(String[] args) {
    InformationFetcher informationFetcher = new InformationFetcher();
    try {
        Process process = Runtime.getRuntime().exec(
            informationFetcher.getFilePath());
        InputStream in = process.getInputStream();
        printInputStream(in);
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

    private void processInformation(InputStream in) {
    topProcessor.process(in);
    }

    private static void printInputStream(InputStream in) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuffer outBuffer = new StringBuffer();
    String newLine = System.getProperty("line.separator");
    String line;
    while ((line = reader.readLine()) != null) {
        outBuffer.append(line);
        outBuffer.append(newLine);
    }
    System.out.println(outBuffer.toString());
    }

    public String getFilePath() {
    return this.getClass().getResource("/idFetcher.sh").getPath();
    }

}