0

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?

Zoran777
  • 564
  • 2
  • 7
  • 25
sadaf011
  • 7
  • 1
  • 4
  • 2
    Possible duplicate of [javac is not recognized as an internal or external command, operable program or batch file](http://stackoverflow.com/questions/7709041/javac-is-not-recognized-as-an-internal-or-external-command-operable-program-or) – thegauravmahawar Oct 03 '15 at 19:13
  • You have to set jour JAVA_HOME variable to a JDK or add the complete path to `javac`in your commandline – Jens Oct 03 '15 at 19:14

2 Answers2

2

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.

Community
  • 1
  • 1
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
  • The executable `javac` is part of the JDK. So make sure, that you have the JDK installed and the bin folder on your `PATH`. The `java` executable is part of the JRE, which is a smaller distribution. JRE stands for Java Runtime Environment while JDK is the Java Development Kit. – hotzst Oct 03 '15 at 19:23
  • @hotzst Please read OP question carefully, he wrote ` I've installed jdk8`. – ashiquzzaman33 Oct 03 '15 at 19:27
1

you need to set JAVA_HOME variable...have a look here Setting JAVA_HOME

Set JAVA_HOME on a UNIX System

The JDK software is installed on your computer, for example, at /usr/jdk/jdk1.6.0_02. You can change this location.

Set JAVA_HOME.

Korn and bash shells:

export JAVA_HOME=jdk-install-dir
export PATH=$JAVA_HOME/bin:$PATH

Bourne shell:

JAVA_HOME=jdk-install-dir
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH

C shell:

setenv JAVA_HOME jdk-install-dir
setenv PATH $JAVA_HOME/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
Change the permissions to enable you to run the GlassFish ESB Installer.

chmod 755 JavaCAPS.bin

Set JAVA_HOME on a Windows System

The JDK software is installed on your computer, for example, at C:\Program Files\Java\jdk1.6.0_02. You can move the JDK software to another location if desired.

Set JAVA_HOME:

Right click My Computer and select Properties.

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.6.0_02.

also you dont need to write .java when you are going to run your program i.e.

$javac abc.java

$java abc
SSH
  • 1,609
  • 2
  • 22
  • 42