java - 错误: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.
}
}
}
解决方案
来自Java 文档:
每个 Java 应用程序都有一个 Runtime 类的实例,它允许应用程序与运行应用程序的环境进行交互。当前运行时可以从 getRuntime 方法中获取。
应用程序无法创建自己的此类实例。
所以不要new Runtime()
尝试Runtime.getRuntime()
推荐阅读
- r - DT:根据 R 闪亮应用程序中另一列的 selectinput 动态更改列值
- mysql - 清除datagridview并添加新值vb.net mysql
- ghost-blog - 面对上述问题。⚠️保存失败!其他人正在编辑这篇文章。有什么解决办法吗?
- flutter - Flutter 如何将图像文件保存到 Firebase 实时数据库
- opencv - 在释放并删除它后,我正在使用 cv2 捕获网络摄像头,它仍然可以访问摄像头
- gnupg - gpg:没有可用的密钥服务器
- core-data - 从过滤的提取请求中求和双倍?
- reactjs - React 应用程序实时加载公共图像,但不在 localhost 中
- json - 使用 jq 将新元素添加到现有 JSON 到特定列表元素
- python - 收到“未提供身份验证凭据。” Vue类星体中的错误