Home > Enterprise >  Run java class from command line
Run java class from command line

Time:04-01

I have java class file main1.class, which code in main1.java :

package m;

public class main1 {
     
    public static void main(String[] args) 
    {
    System.out.println("*** start ***");    
    }
}

Trying to run

Attempt 1:

java -cp . main1

Got error:

Error: Could not find or load main class main1
Caused by: java.lang.NoClassDefFoundError: m/main1 (wrong name: main1)

Attept 2:

java -cp . m.main1

Got error:

Error: Could not find or load main class m.main1
Caused by: java.lang.ClassNotFoundException: m.main1

CodePudding user response:

Java 11 and above, you can simply do
java filename.java

Till Java 11,
javac filename.java
java filename

CodePudding user response:

javac -d . main1.java
java -cp . m.main1

(Your source not in a package tree, but in the current directory)

  •  Tags:  
  • java
  • Related