Search code examples
javaclassloader

Can not find or load main class - classloader issue


I know this question has been asked before, but I’ve read through most of them and still can’t figure out my problem.

I’ve compiled the below code into "HelloWorld.class" located in the directory shown below.

package helloworld;

    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello World");
        }
    }

enter image description here

But I cannot get the .class file to run using the Java command on Window Command Prompt:

enter image description here

I believe my PATH and CLASSPATH are set correctly (as shown below). But I still can’t find the class. Any ideas why?

enter image description here

enter image description here


Solution

  • The name of the class is not HelloWorld. It goes like:

     java helloworld.HelloWorld
    

    and you have to call that from the directory above helloworld.

    In other words: the package name is part of the class name. When you invoke java; you have to provide that full name; and the classpath needs to point to the directory that contains that package.