用Java显示BASH结果

我试图在Java生成的外壳中运行BASH脚本,然后
用JTextArea显示所述BASH脚本的结果.

这是(应该)发生魔法的课程.

import java.io.IOException;

public class Bobsors {

public static Mainframe frame;

public static void main(String[] args) {

    frame = new Mainframe();
    frame.start();

    run();

}

public static void run() {

    String[] cmd = new String[]{"/bin/sh", "PATH=~/Desktop/bobsors.sh"};
    try {
        Process process = Runtime.getRuntime().exec(cmd);
        frame.setLog(process);
    } catch (IOException e) {
        e.printstacktrace();
    }



}

}

这是我的镜架班.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class Mainframe extends JFrame {

private JPanel contentPane;

public static Mainframe frame;
public static JTextArea log = new JTextArea();

/**
 * Launch the application.
 */
public void start() {
    EventQueue.invokelater(new Runnable() {
        public void run() {
            try {
                frame = new Mainframe();
                frame.setVisible(true);
                frame.setTitle("Bobsors Java Application.");
            } catch (Exception e) {
                e.printstacktrace();
            }
        }
    });
}



public void setLog(Process process) {
    log.setText(process.toString());
}


/**
 * Create the frame.
 */
public Mainframe() {
    setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBorder(new TitledBorder(null, "Shell Log", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    panel.setBounds(7, 50, 434, 217);
    contentPane.add(panel);
    panel.setLayout(null);

    log.setBounds(5, 17, 424, 210);
    log.setEditable(false);
    panel.add(log);

    JLabel lblBobsors = new JLabel("Bobsors");
    lblBobsors.setBounds(12, 12, 70, 15);
    contentPane.add(lblBobsors);

    JLabel lblWorksOnLinux = new JLabel("Works on Linux only");
    lblWorksOnLinux.setBounds(12, 26, 203, 15);
    contentPane.add(lblWorksOnLinux);


}
}

运行时唯一显示内容
java.lang.UNIXProcess@509d5bae”
有谁知道如何正确地做到这一点?

解决方法:

Process不会覆盖从Object继承的toString()方法,因此为什么要显示该值.

相反,您可以使用getInputStream(),它将返回InputStream对象,您可以将其转换为如下字符串:Read/convert an InputStream to a String.

同样不要忘了EDT(事件分配线程)上的任务必须快速完成.如果没有,则会备份未处理的事件,并且用户界面将变得无响应,因此请确保您使用其他线程.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...