简体   繁体   English

我无法通过命令提示符运行基本程序,但我可以通过 IDE 运行它

[英]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.我无法通过命令提示符 (Windows 10) 运行任何 Java 程序,但我可以通过我正在使用的 IDE (IntelliJ) 运行它们。 I am currently testing it using a very basic JFrame program which I will provide below.我目前正在使用一个非常基本的 JFrame 程序测试它,我将在下面提供。 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"我可以使用 javac 命令很好地编译 .java 文件,但是每当我尝试使用 java 命令运行它时,我都会收到错误消息:“错误:无法找到或加载主类 JavaTestProgram.java 导致: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.我尝试在系统设置下修复 PATH 变量并尝试使用不同的 IDE 和文本编辑器。

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 .根据提供的错误消息,看起来是因为您正在尝试使用JavaTestProgram.java的类名。 The class name should be simply JavaTestProgram .类名应该只是JavaTestProgram

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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