Harry_J
Harry_J

Reputation: 33

Compiling Java in Cmd and Running it

When I try to run compiled .class java program with this command

java main.class

I get this error

Error: Could not find or load main class main.class

When I compile the program with

javac main.java

It compiles nicely with no errors and gives me a .class file

What am I doing wrong?

I tried editing system variables to no avail.

Upvotes: 1

Views: 153

Answers (3)

Dhaval
Dhaval

Reputation: 1076

let me explain you from very basic

.class files are created when you compile a .java file

javac is a command to compile a java code i.e. .java file

whereas java requires precompiled files. that can me a "NAME" of a class which has main method in it or it can be an entire .jar file.

so in your case the command which you are looking for is "java main".

Upvotes: 0

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

When you run the java command it expects the fully qualified name of a class that has a main method (application entry point) not a class file name.

So it should be

java Main

Where Main is the fully qualified name of a class of the same name, residing in the default package in this case.

You may want to take a look at the documentation of the java command.

Upvotes: 3

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

You may simply write this to run the program:-

java main

Remove the .class

Upvotes: 0

Related Questions