Tutorials : Brush Up on Basics :

Ben Black Ben Black has been programming Java for a year. His motto is once you have learned the basics, you have conquered the language. He is the main webmaster of the Java resource site Cookie Nest - www.cookienest.com.

Java Basic - Compiling & Running

If you have chosen to use an IDE to write Java programs then all the tools needed for you to run, edit and compile the code are supplied so you wouldn't need to use these techniques of compiling and running, but you should read it any way since it shows you how to compile and run your first Java program.

There are two different sort of programs you can make in Java, applications and applets. Applets are the ones which run in your browser. Applications are more like programs you run in Windows and have their own window environment, unlike applets which run in the same window environment as the browser. I'll show you how to run both an applet and application in different ways.

I'll start with the application example.

Hello World Application Source Code

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

 

Hello World Application Bat File Code

javac hello_world.java
java hello_world

 

Download Files

What you have just seen is the ultimate program that any programmer would be happy to write; it is the Hello World application. Follow these simple steps.

  • Select the hello world application source code (or download it) and paste it in to your text editor.
  • Save the file as hello_world.java.
  • Select the hello world application bat file code and paste it in a new page of your text editor.
  • Save this file as hello_world.bat (or in fact anything, but remember to save it as bat file), into the same folder as hello_world.java file.
  • Run hello_world.bat.

And before your eyes (cross fingers) you should see this happen.



You have just ran your first Java program. But if it didn't work and all the files were correctly typed up and in the same folder (if you did type it up yourself, try downloading them and then trying again), then you might of got this error.



This means that you haven't set up your path correctly in the autoexec.bat file. Go back to Getting Started for information about setting your path correctly.

If it did work I'll better explain how and why it works. The bat file is just a quick and easier way of compiling and running your file, you could open up MS-DOS Prompt (Start --> Programs --> MS-DOS Prompt) and type in.

javac hello_world.java
RETURN

java hello_world
RETURN

But that means navigating your way to the correct directory where the java file is and you would have to do it every time you modify the java file. So instead the bat file does it for you. (Note: To edit the bat file, right click on it and select Edit from the menu).

So what is the bat file doing. Firstly it is compiling hello_world.java by using the java compiler, javac. When it compiles the file it creates a class file; in this case it is hello_world.class. Class files are the files which contain the bytecode needed for someone to see your program. The java file is the actual Java code which you can see and edit, but the compiled version, the class file is bytecode which only the JVM (Java Virtual Machine) can read. This means that you can just provide the class file (but if you're a nice kind person you'll also provide the java file, so that other programmers can see what you have done).

Once you have created the class file you can run it, using the program java. You shouldn't put the extension on the end of your file. For example, this wouldn't work.

java hello_world.class

Nor...

java hello_world.java

Instead you should put.

java hello_world

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.