简体   繁体   中英

I can't run a basic program through the command prompt, but I can run it through an IDE

I cannot run any java programs through the command prompt (Windows 10) but I can run them through the IDE (IntelliJ) I am using. I am currently testing it using a very basic JFrame program which I will provide below. I am able to compile the .java file just fine with the javac command but whenever I try and run it using the java command, I get the error: "Error: Could not find or load main class JavaTestProgram.java Caused by: java.lang.ClassNotFoundException: JavaTestProgram.java"

I have tried to fix the PATH variable under system settings and tried using different IDE's and text editors.

import java.awt.FlowLayout;

import javax.swing.*;

public class JavaTestProgram {

    public static void main(String s[]) {

        JFrame frame = new JFrame("JFrame Example");

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        JLabel label = new JLabel("This is a label!");

        JButton button = new JButton();
        button.setText("Press me");

        panel.add(label);
        panel.add(button);

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

Based on the error message provided, it looks like it's because you're trying to use a class name of JavaTestProgram.java . The class name should be simply JavaTestProgram .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM