首页 > 解决方案 > 错误:Runtime() 在 java.lang.runtime 中有私有访问权限

问题描述

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

我尝试使用 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.





        }
    }


    }

标签: javabuttonruntimeexecprocessbuilder

解决方案


来自Java 文档

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

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

所以不要new Runtime()尝试Runtime.getRuntime()


推荐阅读