Here is the Java I try to run from command line:
package my;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Login extends JFrame{
JLabel l1,l2,l3;
JTextField t1;
JPasswordField t2;
JButton b1;
Login(){
Font f=new Font("Arial",Font.BOLD,24);
l1=new JLabel("my.Login Page");
l1.setFont(f);
l2=new JLabel("UserName");
t1=new JTextField();
l3=new JLabel("Password");
t2=new JPasswordField();
b1=new JButton("my.Login");
l1.setBounds(70,40,200,40);
l2.setBounds(70,100,100,20);
t1.setBounds(70,120,200,30);
l3.setBounds(70,170,100,20);
t2.setBounds(70,190,200,30);
b1.setBounds(170,240,100,30);
add(l1);
add(l2);
add(t1);
add(l3);
add(t2);
add(b1);
setLayout(null);
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.out.println(t1.getText());
System.out.println(String.valueOf(t2.getPassword()));
}
});
}
public static void main(String[] args){
Login login=new Login();
}
}
with the following commands:
javac my\Login.java
java my.Login
Earlier I was able to run it when I wrote Java code in IntellIJ even in IDE and in command line, too. However, now I only can run swing Java code in IntellIJ.
When I try to run it in command line, I get the following:
Error: Could not find or load main class my.Login
Is there any mode to be able run swing codes from command line? Why do I get error when I try to run this code in command line? I have Java 8 in my computer, Java is added to PATH.
The issue is the line of code:
package my;
When running your program, you probably didn't specify the filepath of the package root. Assuming your filepath to the package is: \folder\my\Login.java
, you need to change directory to \folder
in the terminal before running the compiling and executing commands.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.