2

I've trying to start learning Java and already in stuck with the easiest possible program (http://introcs.cs.princeton.edu/java/11hello/)

So I created HelloWorld.java with

public class HelloWorld { 
   public static void main(String[] args) { 
      System.out.println("Hello, World");
   }
}

Compiled it with D:\tmp\java>javac HelloWorld.java (all passed fine, without errors)

And tried to run compiled .class:

D:\tmp\java>java HelloWorld.class
Error: Could not find or load main class HelloWorld.class

I have

D:\tmp\java>javac -version
javac 1.7.0

and cannot get why such trivial example doesn't work :-S

zerkms
  • 249,484
  • 69
  • 436
  • 539

3 Answers3

7

You should run it as java HelloWorld (without .class extension).

Crozin
  • 43,890
  • 13
  • 88
  • 135
3

Remove the .class when running the program.

java HelloWorld

Good luck on your coding journey!

Perception
  • 79,279
  • 19
  • 185
  • 195
0

many instance we forgot to close and reopen the command prompt after editing the environement varriable . 1. In the environement varriable -create system variable called JAVA_HOME set the JAVA_HOME value upto bin folder of your JAVA directory 'C:\Program Files\Java\jdk1.7.0_04\bin'.3.Edit path in the system variable and add ';%JAVA_HOME%'. 4. close the control panel and close the command prompt and reopen and compile and run.donot bother about class path.

you can test the functioning by typing javac

For the first time create file helloworld.java the folder c:\users\documents\helloworld.java in note pad and type the following

class helloworld 
{

public static void main(String [] args )
 {
System.out.println("Welcome Helloworld");
 }
}

after saving click command prompt and type

for compiling c:\users\documents\ javac helloworld.java

for running c:\users\documents\ java helloworld

  • 2
    Welcome to stackoverflow. This question was already answered over a year ago. Best to avoid reviving old threads unless the response adds something significant over all previous responses. – Leigh Jun 15 '12 at 09:02