1

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.

2
  • 6
    Run java main. You don't want the .class. Commented Sep 28, 2013 at 20:38
  • Thanks, That solved everything Commented Sep 28, 2013 at 20:46

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You may simply write this to run the program:-

java main

Remove the .class

1 Comment

Also check this doc for more help:- skylit.com/javamethods/faqs/javaindos.html
0

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.