首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java在TaskManager上工作吗?

Java在TaskManager上工作吗?
EN

Stack Overflow用户
提问于 2012-05-31 07:11:19
回答 2查看 5.7K关注 0票数 2

我将为Linux开发一个任务管理器,目前还处于初始阶段。首先,我在Windows平台上的Java中实现了它,它运行得很好。它在Linux平台上不起作用。我使用了Java/Swing的组合。

windows中的"tasklist.exe“功能非常好。linux中的"ps-aux“不起作用。

代码:

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-31 07:28:51

由于您似乎忽略了我关于如何创建Process和使用输出streams的建议,我将尝试另一种方法。

在Linux上工作吗?

是的,当然有。这是你的证明(每一个都有源文件)。

  1. Set Location by Platform Example

  1. Nested Layout Example

票数 6
EN

Stack Overflow用户

发布于 2012-05-31 07:15:59

我假设Java Swings,你是指Java Swing。Java是跨平台的,所以除非您使用一些外部库(这些库具有特定于平台的调用),否则您应该能够在任何平台上编译和运行。

尽管如此,根据this之前的SO post,ps -ef会让您运行所有的任务,因此您可能使用了错误的调用。

我假设您正在尝试执行此代码的Linux平台具有某种GUI,而不是基于控制台的发行版之一。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10828980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档