0

I want to redirect the output of a command "ps -e > /home/workspace/MyProject/List.txt" to JTable. Is there any way of doing it? If yes, how?

PS: I want this command to be executed continuously until user exits and for JTable to get updated every time this command executes.

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

I have used watchservice API to monitor changes in MyProject directory.

So after so many edits, I came to realize that the command doesn't get executed with run.exec() When I execute it from terminal, it goes well but when I run it inside my Java program, no file is created. Can anyone tell me what am I doing wrong with it? I'm using Ubuntu 12.04.

Ingila Ejaz
  • 399
  • 7
  • 25
  • 1
    *"Is there any way of doing it?"* Yes, there is. Should I put that as an answer? [What have you tried?](http://www.whathaveyoutried.com/) – Andrew Thompson Dec 02 '12 at 08:00
  • @AndrewThompson yes please. I'll mark it if it works for me :( – Ingila Ejaz Dec 02 '12 at 08:03
  • why should you write it in a file, instead of you can directly is in table. – vels4j Dec 02 '12 at 14:49
  • I thought there wasn't any way to redirect the output to JTable, that's why I redirected the output to a .txt file and read the .txt file again to form JTable. If there's a way to do it, please let me know :( @vels4j – Ingila Ejaz Dec 02 '12 at 15:01
  • 1
    See also this previous question on the same topic: [JTable reading text files from second line](http://stackoverflow.com/questions/13643907/jtable-reading-text-files-from-second-line) – trashgod Dec 02 '12 at 15:43
  • Copied from edit by @mKorbel to a (now deleted) answer, see also [I/O](http://docs.oracle.com/javase/tutorial/essential/io/index.html) & [Creating a Table Model](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data). – Andrew Thompson Dec 03 '12 at 06:13

1 Answers1

1

Tested with windows Tasklist command, you try with linux command.

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();
 }
}
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • Possible things to look at. 1) `ProcessBuilder` (makes it easier to merge both `out` and `err` streams) 2) Consume both streams. 3) Break the command into a `String[]` (not that it seems necessary with this command, but can confound when using paths with spaces) -- +1 for your detailed answer. :) – Andrew Thompson Dec 03 '12 at 04:35
  • 1
    @AndrewThompson Thanks. I just roughly worked against this comment *"The question is obviously overly broad. Why not vote to close it instead of giving a correct, but unhelpful answer?"* – vels4j Dec 03 '12 at 06:09
  • Good call. Now deleted. ;) – Andrew Thompson Dec 03 '12 at 06:14