Error:Runtime() has private aaccess in java.lang.runtime
Error:Runtime() has private aaccess in java.lang.runtime
我正在尝试执行此操作:当我单击按钮时 "search" 命令提示符 windows 打开。
我尝试使用 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 docs:
Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
An application cannot create its own instance of this class.
所以不要 new Runtime()
试试 Runtime.getRuntime()
我正在尝试执行此操作:当我单击按钮时 "search" 命令提示符 windows 打开。
我尝试使用 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 docs:
Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
An application cannot create its own instance of this class.
所以不要 new Runtime()
试试 Runtime.getRuntime()