Java - missing main class

I'm trying to run one of the 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 the .class file from the .java file, but "java" complains about the "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. 
+4
source share
3 answers

You need to run it like

 javac MyFirstApp.java java MyFirstApp 

From the directory in which MyFistApp.java lives.

+6
source

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

"java" will run the compiled code - you will be given 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 this directory. But are you in the directory where javac and java programs are stored? If not, they may also need absolute paths if you did not put them in your PATH variable.

+1
source

What is a package name? Perhaps you have something like

org.test package;

in the title?

0
source

Source: https://habr.com/ru/post/1301855/


All Articles