开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜