1

I tried to run my basic HelloWorld.class file from my terminal.

I use the following input:

Java HelloWorld.class

But it says:

Error: Could not find or load "HelloWorld.class"

I have tried giving it a directory but it doesn't work.

4
  • Please extend your question with the exact compile & running command. Commented Jul 30, 2014 at 4:09
  • remove .class extension Commented Jul 30, 2014 at 4:09
  • Does HelloWorld live in a package? Commented Jul 30, 2014 at 4:09
  • which folder is your HelloWorld in? Commented Jul 30, 2014 at 4:15

6 Answers 6

2

because you didn't compiled or run it successufully.you should use

 javac HelloWorld.java

to complile and

then use

 java HelloWorld

to run it. plz check this tutorial http://introcs.cs.princeton.edu/java/11hello/

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

Comments

2

The class should be (Executable Class should definitely contain the main method with same declaration as below)

Class MyClassName
{
   // Methods here

   public static void main (String args[])
   {
       // Code here
   }
}

To Compile, it should be:

javac MyClassName.java

On successful compilation, MyClassName.class would be generated in your folder.

To run, it should be

java MyClassName

In case your java is in say D:/JavaWorkDir/src, You need to compile and run from the folder D:/JavaWorkDir/src. Also Ensure that your classpath is set appropiately.

Comments

0

You don't run it as

java HelloWorld.class

but

java HelloWorld

There is no need of .class extension.

But note you always have to use fully qualified name. So if your class resides in some package say myPackage then you need to run

java myPackage.HelloWorld

Comments

0

You are receiving this error because you shouldn't include the .class when you run the compiled file.

After you've compiled:

javac HelloWorld.java

run using:

java HelloWorld

(don't do: java HelloWorld.class)

Comments

0

Run it as Java HelloWorld and not like Java HelloWorld.class.
The error Error: Could not find or load "HelloWorld.class" is occuring because:

  1. The class may have not compiled correctly.
  2. The compiled class is not available on the path from where you are trying to run it.
  3. Proper classpath is not set for compiling and running the java classes.

Whenever you write a Java Program named HelloWorld, you must compile it as:

javac HelloWorld.java

Once the HelloWorld.class class file generated in the same directory where you have your java file, compiled by the compiler, you can run it from console as:

java HelloWorld

Comments

0

if u want to try hello world you can also try to run it on the NetBeans application and also on Jdoodle.com

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.