Search code examples
javacommand-linejavac

Java program compiling error


I started learning Java today. I installed jdk8, wrote a simple "Hello World" program and then executed this on command line:

c:\java>java hello.java

But it shows this error:

Error: could not find or load main class hello.java

Then I also tried doing this:

c:\java>javac hello.java

Now it shows:

'javac' is not recognized ass an internal or external command,operable program or batch file

How to solve this and compile the Java program?


Solution

  • Firstly You need to Set JAVA_HOME variable.

    You can set JAVA_HOME variable in your Windows as

    1. Right click My Computer and select Properties.

    2. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1.8.0_05.

    Then you need to compile .java file

    Compile java program by

    c:\java>javac Hello.java 
    

    It generate '.class` file. To run it use

    c:\java> java Hello  // Not Hello.class
    

    Where Hello.class is your class file name.