This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes I can't run my first program on command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I can Watch "I can New topic
Author

I can't run my first program on command line

ken wise
Greenhorn

Joined: Jun 30, 2010
Posts: 3
Please HELP me out!
I can't run my first program on command line\command prompt, it keeps giving me the error message below:

'javac' is not recognized as an internal or external command,operation

program or batch files


Below is what i did:

1-I installed notepad++

2-I typed my code into it and saved as C:\ex1

3-I ran my cmd and changed directory to C:\ex1

4-At C:\ex1> I typed "javac Squarer.java" (as seen in the JPG image i sent along)

5-The message i get is -'javac' is not recognized as an internal or external command,operation

program or batch files.-

NB: PLEASE I DON'T KNOW WHERE I WENT WRONG OR WHAT I HAVE TO FIX TO MAKE MY PROGRAM RUN

BECAUSE ALL THE PROGRAMS I RUN ON COMMAND LINE DISPLAY THE SAME ERROR MESSAGE.

Thanks for your HELP!


...I love java.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ken wise wrote:1-I installed notepad++

thats fine.
ken wise wrote:2-I typed my code into it and saved as C:\ex1

you must save a java file with .java extension . so in your case the file name must be "ex1.java"[example FileName.java]

ken wise wrote:3-I ran my cmd and changed directory to C:\ex1

if ex1 is the folder name, then it is fine.

ken wise wrote:4-At C:\ex1> I typed "javac Squarer.java" (as seen in the JPG image i sent along)

with this command you are asking compiler to comiple/verify your source code

ken wise wrote:5-The message i get is -'javac' is not recognized as an internal or external command,operation

but unfortunately, window cant find your compiler[javac] to execute[assume you are working on window platform] you need to add javac.exe location to the window PATH
to set this, right click on your mycomputer icon on your desktop-->advanced tab--> environment variable ---> click PATH variable in system variable and then edit/add java installation path upto bin directory example: C:\Program Files\Java\jdk1.6.0_16\bin
[or]
just mentioned fully qualified name of the javac in command prompt i.e, "C:\Program Files\Java\jdk1.6.0_16\bin\javac" Squarer.java [here path is surrounded by "" because it contains space(Program Files)]

after successful of compilation , run using "C:\Program Files\Java\jdk1.6.0_16\bin\java" Squarer

or java Squarer [if you add java location into the system path]

hth
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Welcome to JavaRanch.

Please read the installation instructions for the JDK, especially step 4 (Update the PATH variable).

And please DON'T WRITE IN ALL CAPS BECAUSE IT LOOKS LIKE YOU ARE SHOUTING!!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Did you install a JDK?

And if you did, did you follow all the instructions, particularly the one about setting the PATH variable?


[EDIT: Beaten to the answer again.]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
You are getting this error because it could not locate the path where java is installed on your machine.
You have to set classpath for this. Your class path should point to the bin folder of your jdk folder.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Rite Sara wrote:You have to set classpath for this. Your class path should point to the bin folder of your jdk folder.

No, that's wrong.

You have to add the bin directory of your JDK installation to the PATH (not the CLASSPATH). The PATH is what the operating system uses to find programs to execute.

CLASSPATH is for Java itself, this is what Java uses to find Java programs (class files and JAR files) to run. Do not add the bin directory of the JDK to the CLASSPATH, that won't solve the problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
To add to what Jesper said, don't do anything with your classpath variable.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Campbell Ritchie wrote:To add to what Jesper said, don't do anything with your classpath variable.

To add to what Jesper and Campbell said, PLEASE don't mess with the classpath.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: I can't run my first program on command line