【问题标题】:Error:Runtime() has private aaccess in java.lang.runtime错误:Runtime() 在 java.lang.runtime 中有私有访问权限
【发布时间】:2019-11-29 11:33:23
【问题描述】:

我正在尝试执行此操作:当我单击“搜索”按钮时,命令提示符窗口打开。

我尝试使用ProcessBuilder,没有出现错误,但是没有用。你能帮帮我吗?

package sys.tool;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//Создаем поля
public class MainWindow extends JFrame{
    private JLabel lcn = new JLabel("Enter computer name:");
    private JTextField icn = new JTextField("", 5);
    private JButton search = new JButton("Search");
    private JLabel lun = new JLabel("Enter user name:");
    private  JTextField iun = new JTextField("", 5);
    private JLabel empty = new JLabel("");


    public MainWindow (){
        super("SysAdminTool");
        this.setBounds(100, 100, 700 , 90);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        Container container = this.getContentPane();
        container.setLayout(new GridLayout(3, 2 , 1, 1));
        container.add(lcn);
        container.add(icn);

        container.add(lun);
        container.add(iun);

        container.add(empty);
        search.addActionListener(new SearchEventListener());
        container.add(search);
    }

    class  SearchEventListener implements ActionListener{
        public void actionPerformed (ActionEvent e){

           Runtime rt = new Runtime();
           rt.exec(new String[]{"cmd.exe", "/C","start"}); \\Here a make an event for button, to open cmd when I click the button.





        }
    }


    }

【问题讨论】:

    标签: java button runtime exec processbuilder


    【解决方案1】:

    来自Java docs

    每个 Java 应用程序都有一个 Runtime 类的实例,它允许应用程序与运行应用程序的环境进行交互。当前运行时可以通过getRuntime方法获取。

    应用程序无法创建自己的此类实例。

    所以不要使用new Runtime() 试试Runtime.getRuntime()

    【讨论】:

    • 谢谢回复,我是开始学java的,我的功底不高。据我了解,如果我想使用运行时 - 我必须在所有情况下都编写 Runtime.getRuntime ,不是吗?
    • 是的,没错。 Java 不允许您创建新的 Runtime 对象,因此您必须使用 getRuntime() 方法来获取 Java 自动创建的对象。
    • 好吧,这是个有趣的错误。非常感谢您的帮助。
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2018-11-29
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多