0

我想将命令“ps -e > /home/workspace/MyProject/List.txt”的输出重定向到JTable. 有什么办法吗?如果是,如何?

PS:我希望此命令连续执行,直到用户退出,并且JTable每次执行此命令时都更新。

 runtime.exec("ps -e > /home/workspace/MyProject/List.txt");

我使用 watchservice API 来监视 MyProject 目录中的更改。

所以经过这么多的编辑,我开始意识到命令没有被 run.exec() 执行当我从终端执行它时,它运行良好,但是当我在我的 Java 程序中运行它时,没有创建文件。谁能告诉我我做错了什么?我正在使用 Ubuntu 12.04。

4

1 回答 1

1

用windowsTasklist命令测试,你用linux命令试试。

public class ProcessListTable {

  private String GetProcessListData() {
    Process p;
    Runtime runTime;
    String process = null;
    try {
        System.out.println("Processes Reading is started...");

        //Get Runtime environment of System
        runTime = Runtime.getRuntime();

        //Execute command thru Runtime
        // use appropriate command for linux "ps"
        p = runTime.exec("tasklist /FO CSV /nh");

        //Create Inputstream for Read Processes
        InputStream inputStream = p.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


        ArrayList<String> taskEntries = new ArrayList();
        String line = bufferedReader.readLine();
        while (line != null) {
            taskEntries.add(line);
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();

        MyTableModel myTableModel = new MyTableModel();
        myTableModel.update(taskEntries);
        JTable table = new JTable(myTableModel);
        JFrame frame = new JFrame("TaskList");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);


        System.out.println("Processes are read.");
    } catch (IOException e) {
        System.out.println("Exception arise during the read Processes");
        e.printStackTrace();
    }
    return process;
}

 public class MyTableModel extends AbstractTableModel {

    String[] columnName = new String[]{"Image Name", "PID", "Session Name", "Session#", "Mem Usage"};
    String[][] valueA;

    public void update(ArrayList<String> taskEntries) {
        valueA = new String[taskEntries.size()][columnName.length];
        int size = taskEntries.size();
        for (int i = 0; i < size; i++) {
            String entry = taskEntries.get(i);
            String[] splitValues = entry.split(",");
            for (int j = 0; j < splitValues.length; j++) {
                String v = splitValues[j];
                v = v.replaceAll("\"", "");
                // mem contains "," so added mem usage at the end
                if (j >= 5) {
                    valueA[i][4] = valueA[i][4] + v;
                } else {
                    valueA[i][j] = v;
                }
            }
        }
    }

    @Override
    public int getRowCount() {
        return valueA.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnName[column];
    }

    @Override
    public int getColumnCount() {
        return columnName.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return valueA[rowIndex][columnIndex];
    }
 }

 public static void main(String[] args) {
    ProcessListTable gpl = new ProcessListTable();
    gpl.GetProcessListData();
 }
}
于 2012-12-02T16:11:09.897 回答