how do I compile and run a java program?

to compile i believe you would run javac file.java and it should compile and to run it you would just use java file.class
and that should do it i believe :) you will have to locate these 2 files on your system javac and java i but not sure. this is of course if you are just running a command line program and not an applet :)
http://www.iam.ubc.ca/guides/javatut99/native1.1/stepbystep/

i have always just used text pad to do all my java stuff in cause you can set it up to compile and run your program :)

This link may help you to understand how to compile and run java programs.
The only thing is I cant find which windows operating system its for, probably windows 98, 2000. However it can be easily adapted to windows xp!

LINK

Ok, so I found two lcations of this javac and every time i click on either one of them a window flashes by and disappears? How the heck are you supposed to run javac? How do you compile? The link doesn't help. It assumes you know how to compile. Sorry, I'm new to Java and I just wanna learn how to compile.

open your command prompt.
Change your directory to wherever your programs are...
Eg if they are in C:\java, then:
cd c:\java ENTER
Then javac fileName.java ENTER
That line compiles it, and creates a .class file,
Next... to execute the program...
java fileName.java ENTER

If you used an IDE like netbeans 5.0 or something else, you don't have to bother with this, but they can be confusing to start with, so I recommend just playing with notepad and command prompt till you get the hang of basic java.

OK, I got as far as getting into the C:\java folder, but there is no javac found so it stops there. I'm sorry. I'm a total newb here.

I ran the code javac MyFirstApp.java ENTER like you said, and I get an error saying it's not a command. HELP!!

I made a little error above,,,
you just need to do java fileName to execute.

Ok, try this...
cd c:\java ENTER
SET CLASSPATH= ENTER
javac fileName.java ENTER
java fileName ENTER

That might work, but your JDK needs to be set up correctly, the command prompt needs to know the path to the JDK, you can do this by modifying the AUTOEXEC.BAT file.
Let me know how it goes with the commands I mentioned first.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Compaq_Administrator>cd C:\java

C:\java>SET CLASSPATH=

C:\java>javac HelloWorldApp.java
'javac' is not recognized as an internal or external command,
operable program or batch file.

C:\java>java HelloWorldApp.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/java

C:\java>java HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

Still nothing. The official javasun site had a different way of resetting the path though. I don't understand either way...heh

Post the code your trying to compile, the last couple lines in your last post look to be issues with your code

/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

For what it's worth, go to http://www.jgrasp.org and download their very easy to use GUI-based editor. There are not a lot of bells and whistles, but it is free and is a decent enough compiler.

Additionally, the actual Java compiler should be in a directory called jdk1.5.0_08\bin. This is providing that you have JDK installed correctly.

You are getting this error message:

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

because the compiler is not in the directory that you are trying to run it from. Try this:

cd c:\java\jdk1.5.0_08\bin

c:\java\jdk1.5.0_08\bin>javac HelloWorldApp.java

c:\java\jdk1.5.0_08\bin>java HelloWorldApp

I followed the above steps and got the proper output:

Hello World!

I've installed just about every link I can find off of JavaSun's website and JGrasp still says I havent installed the right environment.

ok THIS WILL WORK FOR SURE.
delete everything and go to this download page and download the j2sdk1.4.2_12 version for windows.
http://java.sun.com/j2se/1.4.2/download.html
Then download this version.
J2SE v 1.4.2_12 SDK includes the JVM technology[IMG]http://java.sun.com/images/pixel.gif[/IMG]The J2SE Software Development Kit (SDK) supports creating J2SE applications. More info...[IMG]http://java.sun.com/images/pixel.gif[/IMG]Download J2SE SDK[IMG]http://java.sun.com/images/pixel.gif[/IMG]Installation Instructions ReadMe ReleaseNotes
Sun License Third Party Licenses

i am assuming you know how to install anyways after installing you need to know where your j2sdk1.4.2_12 folder is. just for instance lets assume you have it in c drive. go to start and then run command prompt window. enter cmd. you should have a path something like this
c:\documents and settings\chris
well do this first
cd..
after that you will have c:\\documents and settings
well do the same thing one last time so then you are in your c:\
cd..
now you will have the screen something like this
C:\
i am assuming your j2sdk folder is in c drive so you will do this next
cd j2sdk1.4.2_12 (then press enter)
now you will see your command prompt like this
c:\j2sdk1.4.2_12
then just type one last command.
cd bin
now your command prompt looks like this
c:\j2sdk1.4.2_12\bin
Thats it BOYyYyyyYyy You got it. The next thing you need to do is type your code in a notepad and make sure MAKE SURE YOU SAVE IT IN THE BIN FOLDER WHICH IS IN J2SDK1.4.2_12 and save it as a .java file e.g :) (HelloWorldApp.java) in the bin folder.
Now go to your command prompt i hope it is still in bin directory
type this
javac HelloWorldApp.java
no errors HURRAY !!!!
then type java HelloWorldApp
Have fun BoYyYyyYy

hey fastmike ,
thx it helped me a lot

but there r 2 things i am yet concerned to ask

1) can i run this programs when i save them in sum diff folder ?
there must be sumthing which enables the path where javac
can b enabled
eg in f:\girish\example.java

can i run this program there and not just in bin folder
c:\j2sdk...\bin

2) whenever i tried typing cd j2sdk1.4.2_13
it said invalid folder name
so i renamed it to java2_13
will it harm other programs or jre ?
do i need to make any changes sumwhere also ?

and thx again it helped but wish i could save my programs
in sum oter files

grii_19 do not use this approach, this is considered bad practice to save anything into Java instalation folder. Please follow the link to the tutorial which I gave it to you in another post

Oh man, the deaf leading the blind.

I have no idea where this "save it to the bin folder" stuff got started, but here ye, what rubbish.

I know, I know, now you're going to come back with, but it works and its better than nothing, so whats wrong with it.

Well, if your foot itches you can cut it off, it works, and it's better than nothing, but there is still definately something wrong with it.

hey peter
thx ..... yep i followed ur advice from other forum
and itz working properly now
thankz bro .....

blind leading the deaf ??????????????????????
didnt got wht it meant but no longer saving the files in
bin folder ......
so thank you

The blind leading the deaf is just a "cute" way of saying that the help being given (at least by some) is bound to cause a lot of confusion, misdirection, antics, and probably dangers. Not that they are intended, but they are going to happen (and it will usually by comical).

what you need to do is: you need to add the path, to do that, you right click my computer icon on the desktop, you then go to properties, from there you go to advanced, from advanced you go to Environmental variables, under System variables (in blue color), you double click path. at the end of you path you put a sermi-colon(;), the you add your path, e.g if you using java 6(or jdk 6), you type in: C:\Program Files\Java\jdk1.6.0\bin; then click on OK, OK again, Apply, OK, then go to command, if you class is in c: you need to move to the c drive, this is how it is supposed to look: c:\> then type javac HelloWorldApp.java

Hope this will help

how do I compile and run a java program in notepad

I have saved my source folder is on my J: drive I am using
a USB. Could that be a problem for me, should I put everything
on my C: drive?

commented: zombie master -2

First download the developer kit from the http://java.sun.com/javase/downloads/index.jsp.Select jdk to download.
After downloading it,install the file.
If it is success then open command prompt and type java -version and press enter,if the installation is good then the command prompt is writes the actual java version in your computer...
like : java version "1.6.0_18" etc....
then try to compile your source with the command prompt
like javac HelloWorldApp.java this will create a HelloWorldApp.class file (it is a bytecode )
then write the following command to the command prompt: java HelloWorldApp

then try to compile your source with the command prompt
like javac HelloWorldApp.java this will create a HelloWorldApp.class file (it is a bytecode )
then write the following command to the command prompt: java HelloWorldApp

Will not work as long you do not

  • place the java file in same directory as java.exe which is bad thing to do
  • provide path to java.exe on each compile that is time consuming and error prone
  • set a pat variable in system variables of operating system

and anyway, why post nonsense to a thread that was discussed to death 3 years ago?

Copy the location of your javac.exe file. Right-click on "My Computer" and hit "Properties." Go to the "Advanced" tab and hit "Environment Variables." Then, paste your file location into the following variables (If they do not exist, creatre them. If they have other values, separate them with a semicolon): classpath, include, lib, and path. Once you're done with that, the

javac filename.java

command should work properly.

On the other hand, every time I try it, it gives a giant wall of text and says "100 errors". So, maybe the guy who told me to do that lied, but it made the "'javac' is not recognized as an internal or external command, operable program or batch file" error go away.

@BeefRocks:
seriously ... I would assume that the previous post stating that this was an ancient thread which wasn't in need of reviving was clear enough.
I do assume you noticed this, and, while doing so, you also noticed that thát particular post is over a year old, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.