简体   繁体   English

java - 缺少主类

[英]java - missing main class

I'm trying to run one of the very first examples from the book "Head First Java"; 我正试图运行“Head First Java”一书中的第一个例子;

public class MyFirstApp {
public static void main (String[] args){
    System.out.println("I Rule!");
    System.out.println("The Worlds!");
}
}

"javac" created a .class file from the .java file - but "java" complains about a "missing main class" when trying to run the .class file (i also tried java -cp . "..." with the same result): “javac”从.java文件创建了一个.class文件 - 但是“java”在尝试运行.class文件时抱怨“缺少主类”(我也试过java -cp。“...”同样结果):

C:\>java \hfj\MyFirstApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: \hfj\MyFirstApp/class

Caused by: java.lang.ClassNotFoundException: \hfj\MyFirstApp.class
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: \hfj\MyFirstApp.class.  Program will exit.

You need to run it as 你需要运行它

javac MyFirstApp.java
java MyFirstApp

From the directory where MyFistApp.java lives. 从MyFistApp.java所在的目录。

'javac' calls the compiler - to that you need to pass your .java file. 'javac'调用编译器 - 你需要传递.java文件。

'java' will run the compiled code - to that you pass the name of your compiled file, but without any extension: "java MyFirstApp" 'java'将运行已编译的代码 - 您传递已编译文件的名称,但没有任何扩展名:“java MyFirstApp”

Specifying the full path of the file should work when you are not in that directory. 当您不在该目录中时,指定文件的完整路径应该有效。 But are you in the directory that holds the javac and java programs? 但是你在拥有javac和java程序的目录中吗? If not, those may also need absolute paths if you have not put them on your PATH variable. 如果没有,那么如果你没有将它们放在你的PATH变量上,它们也可能需要绝对路径。

What ts the package name? 套餐名称是什么? Maybe you have something like 也许你有类似的东西

package org.test; package org.test;

in your header? 在你的标题?

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

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