javajavacclassnotfoundexception

How to solve the issue resulting in output of the error message "class not found wrong class name"?


I have the following Java code in a file:

package hello_project;

public class Hello {

    public static void main(String[] args) {
        System.out.println("Hello world");

    }
}

The code works when I run:

java Hello

But the code doesn't work when I use:

javac Hello.java
java Hello

The error message is:

class not found wrong class name

Solution

  • > type Hello.java
    package hello_project;
    public class Hello {
        public static void main(String[] args) {
            System.out.println("Hello world");
        }
    }
    
    > javac -d . Hello.java
    
    > dir hello_project
     ...
    
     Directory of C:\...\hello_project
    
    06/14/2024  02:12 PM    <DIR>          .
    06/14/2024  02:12 PM    <DIR>          ..
    06/14/2024  02:12 PM               429 Hello.class
                   1 File(s)            429 bytes
                   2 Dir(s)  15,829,270,528 bytes free
    
    > java -cp . hello_project.Hello
    Hello world