Skip to content

Head first Java development

2010 May 20
tags:
by Richard Knop

I’ve been learning Java lately (by reading the official Java tutorials which are fantastic) and I have decided to write a simple tutorial for others who like to jump head first to developing with Java without having to read lots of theory on its architecture, OOP principles and so on.

You will need few things in order to develop Java applications:

  • Java SE JDK (download here)
  • IDE (I recommend Eclipse)

You might also need to edit your PATH and CLASSPATH environment variables but more about that later.

Java source code is saved in files with a *.java extension. Once you compile a java file, a compiled *.class file is created. Java compiler outputs platform-neutral Java bytecode which is a bit different than *.exe executable files you are most likely used to. To run a class file you need Java Virtual Machine (JVM) installed on your machine. You should already have that if you downloaded and installed the JDK linked above.

Let’s write your first Java application then.

Open Eclipse and go to File -> New -> Java Project. Call it “Hello World”, for example. Once you have created a project, right click on “src” folder in the left part of Eclipse called Navigator and select New -> Class. Call it “HelloWorld” as well and write this code in the editor that appears next to the Navigator:

  1. public class HelloWorld {
  2.  
  3.     public static void main(String[] args) {
  4.         System.out.println("Hello World");
  5.     }
  6.  
  7. }

That’s it. The above Java application will simple print “Hello World” and do nothing more but you have to start somewhere :)

To compile and run the application, select Run -> Run As -> Java Application. The program output will appear in the bottom part of Eclipse under Console tab.

PATH & CLASSPATH

If you want to be able to compile and run Java applications from Windows command line, you will need to add/edit PATH and CLASSPATH environment variables first. In Windows 7/Vista, right click on Computer and select Properties. Go to Advanced system settings and click on Environment Variables button in bottom left corner. A window similar to the picture bellow should appear.

You need to add (or change if it already exists) CLASSPATH to your user variables and PATH to both user and system variables. Set PATH variable to a bin directory of your JDK installation (i.e. C:\Program Files (x86)\Java\jdk1.6.0_20\bin) and set CLASSPATH variable to a directory where you store compiled class files of your Java applications (you can see in the picture I set it to D:\java-projects\HelloWorld\bin). You can set multiple paths separated by semicolon. You might also want to set a system variable JAVA_HOME to a JDK installation directory (i.e. C:\Program Files (x86)\Java\jdk1.6.0_20).

Now you should be able to compile and run Java applications from Windows command line. To compile a java file, type:

javac D:\java-projects\HelloWorld\src\HelloWorld.java

To run the compiled class file, type:

java HelloWorld

If you haven’t set the CLASSPATH variable, you could use an optional cp parameter to specify it in cmd:

java -cp D:\java-projects\HelloWorld\bin HelloWorld

You can find more about PATH and CLASSPATH variables here.

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS