I am trying to make this action: when I click the button "search" command prompt windows is open.
I tried to use ProcessBuilder, no errors appeared, but it didn't work. Could you help me please?
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.
}
}
}
From the 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.
So instead of new Runtime()
try Runtime.getRuntime()