Jack Thomson
Jack Thomson

Reputation: 193

cannot run a simple java class

I created a simple java class:

package com.name;

public class Main {
    public static void main (String [] args) {
        System.out.println("I have made it");   
    }
}

and save it in Main.java

Then I did java Main.java, and I can see the Main.class

I tried to run it as:

java Main

java com.name.Main

but I kept getting:

Error: Could not find or load main class Main

My java -version output is:

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

and my $JAVA_HOME environment variable is:

/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home

Upvotes: 0

Views: 29

Answers (1)

Stephen Kitt
Stephen Kitt

Reputation: 2881

Your class is in the com.name package, so the class loader expects to find it in a com/name subdirectory:

mkdir -p com/name
mv Main.class com/name
java com.name.Main

Upvotes: 2

Related Questions