user1832583
user1832583

Reputation:

Running Java Programs on the Command Prompt

I have done a lot of researching on this concept but I can't seem to run a java program on the command prompt. Let's say we had a simple program like this:

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

On the command prompt I tried:

javac Hello_World.java

But I get:

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

So I compiled it on BlueJ and then did this:

java Hello_World.java

But it said "cannot load or find main class Hello_World"!

I am currently using Windows 7, and made the programs on Notepad++ and BlueJ (to compile). Any suggestions? Thanks!

Upvotes: 0

Views: 3046

Answers (3)

Suzan Cioc
Suzan Cioc

Reputation: 30097

'javac' is not recognized as an internal...

means OS does not know where javac program is located. Either add it to PATH or run explicitly

 my\path\to\file\javac Hello_World.java

Compiling will convert *.java to *.class

Hello_World.class file should be located according to it's package directive. Since you have no one, in your case it should be located in the same directory you will run java.

To run your class specify it's name not file name

java Hello_world

looking for the class is essential part of launching and occurs by rules.

Upvotes: 0

someone
someone

Reputation: 6572

javac' is not recognized ..

comes when you haven't point your java bin directory to your path environment variable. Because bin directory is the place where javac.exe exist.

To do it.

1) right click on mycomputer property
2) go to Advance system settings.
3) go to environment variable.
4) In system variable click on path
5) go to edit mode and provide your path to java bin directory.

in my case it is C:\Program Files\Java\jdk1.7.0_01\bin;

Upvotes: 1

Jatin
Jatin

Reputation: 31724

This explains in detail what you have to do to set class path. Primarily you need to set your environment variables so that your shell finds the right directory containing javac to compile your program

Upvotes: 3

Related Questions