2

I am to develop a Task Manager for Linux and is in the initial stages. First I implemented it in Java Eclipse in the Windows platform, and it works just fine. It doesn't work in the Linux platform. I've used a combination of Java/Swing.

"tasklist.exe" in windows works perfectly. "ps-aux" in linux doesnt work.

Code:

package test.t100.t001;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import javax.swing.*;

public class TabbedPaneDemo extends JPanel {

    private static final long serialVersionUID = 1L;
    Integer i;

    JTextArea output = new JTextArea();

    public static void main(String args[]) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
                JFrame frame = new JFrame("TabbedPaneDemo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    private String getDetails() throws IOException {
        //fn();
        String line;
        String result = "";
        PrintStream p;
        Process p1 = Runtime.getRuntime().exec("tasklist.exe");
        // read from a process
        BufferedReader input = new BufferedReader(
                new InputStreamReader(p1.getInputStream()));
        while ((line = input.readLine()) != null)
        {
            //System.out.println(line);
            output.append(line + "\n");
            result += line+"\n";
            //p.println (line);
            //textarea.setVisible(true);    
        }
        //msgBox(result);
        //p.close();
        input.close();   

        return result;
    }

    public TabbedPaneDemo() {
        super(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();
        ImageIcon icon = createImageIcon("images");


        JComponent panel1 = makeTextPanel("tasklist");
        tabbedPane.addTab("tasks", icon, panel1,
                "ta");
        // add it to something!
        panel1.add(new JScrollPane(output));
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        JComponent panel2 = makeTextPanel("windows");
        tabbedPane.addTab("wins", icon, panel2,
                "wi");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);


        add(tabbedPane);//`enter code here`
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        try {
            String s = getDetails();
            output.setText(s);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }


    public static void msgBox(String msg) {
        javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
                null, msg, "WindowsUtils",
                javax.swing.JOptionPane.DEFAULT_OPTION);
    }


    protected JComponent makeTextPanel(String text)
    {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
celebrate celeb
  • 75
  • 1
  • 3
  • 10
  • In comments on your [last question](http://stackoverflow.com/q/10783123/418556) I recommended you do this from the command line 1st and implement the recommendations of 'When Runtime.exec() won't". Do you have a version for Linux working on the command line? Your current code ***still*** does not indicate you understand the perils working with processes .. :( – Andrew Thompson May 31 '12 at 07:20

2 Answers2

7

I am assuming that by Java Swings you mean Java Swing. Java is cross platform so unless you are using some external libraries (which have platform specific calls), you should be able to compile and run on any platform.

That being said, according to this previous SO post, ps -ef will get you all of the tasks running, so it seems that you could be using the wrong call.

I am assuming that the Linux platform you are trying to execute this code on has some sort of GUI, and is not one of those console based distributions.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
6

Since you seem to be ignoring my advice to tidy up how the Process is created and the output streams consumed, I'll try a different tack.

Does Java Swing work on Linux?

Yep, sure does. Here's your proof (complete with source for each).

  1. Set Location by Platform Example
  2. Nested Layout Example
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433