简体   繁体   中英

java - missing main class

I'm trying to run one of the very first examples from the book "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):

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.

'javac' calls the compiler - to that you need to pass your .java file.

'java' will run the compiled code - to that you pass the name of your compiled file, but without any extension: "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? If not, those may also need absolute paths if you have not put them on your PATH variable.

What ts the package name? Maybe you have something like

package org.test;

in your header?

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