Computer Programming web Web programming Tips



Start Java Programming - learn Java code

By Sergey Skudaev



Here you will learn how to install Java Development Kit, set PATH and CLASSPATH environment variables, create java application, compile java source code and run java application.

1. Download Java SE Development Kit 6u16 Download Java SE Development Kit

3. Set PATH, CLASSPATH and JAVA_HOME environment variables   Environment Variables



The dot in classpath indicates that the Java class file generated after you compile java source code will be located in the same directory as *.java source files. The "classes" directory is not created during Java SDK installation. I created it later. You can create any directory for java classes wherever you like.


The "C:\j2sdk1.4.2_08\bin" path points to J2SDK location on my PC. On your PC path to J2SDK and its version may be different. So use your path, not mine.

JAVA_HOME is not required to run a java application, but it may be useful later if you decide to install Tomcat server to develop and run java application for the web.

You have to create environment variables. Please read how to create environment variables tutorial. Hover mouse over a thumbnail to see a large image.

image1

On my PC I set the path environment variable for user and system. Only then I could start javac.exe in command prompt from any directory.

Create a Java application source code.

To test configuration on your PC, create a test directory on your C drive. Open Notepad or TextPad and copy the following Java code.

import java.lang.*;

public class greeting
{

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

}



Save the file as greeting.java in the test directory. In Java language, the source file name always must be the same as a public class name declared in the file.



Compile the java source code.

Open command prompt. Usually, it is located in accessories menu. Command prompt displays a path to "Windows" directory (in windows 98, Me) or "C:\Documents and Settings\user>" directory, or some other directory.

Type cd and two dots cd.. and press enter.

Repeat the procedure until you get to root directory C:> Then type cd test and press enter.

You will get in C:\test> directory where your greeting.java file is located.

You can check it by typing C:\test>dir command. All files of test directory will be displayed.

Type C:\test>javac greeting.java and press enter. The source file will be compiled and greeting.class file appears in the test directory. If you mistype the code, errors will be displayed. Check if your spelling is correct.

Run java application

If no error display, type C:\test>java greeting   

You should not type file extension to run java class. Press enter to start java application.

The message "Hello World!" displays in command prompt.

image1

Congratulation! You configure your PC correctly and can start to develop more sophisticated Java Application.

The ASCII Art Java application prints flowers in wase composed from ASCII characters.
Ther is a source code:ASCIIart.java

//Java Art.  Copyright by Sergey Skudaev 2007
public class ASCIIart
{

   public ASCIIart()
   {

   }

public void print()

System.out.println("                                                 ");
System.out.println("                                                 ");
System.out.println("                                                 ");
System.out.println("                                                 ");
System.out.println("                        @ @ @             ");
System.out.println("                      @ @ @ @           ");
System.out.println("                        @ @ @             ");
System.out.println("                            \\|/                 ");
System.out.println("                            | | |                ");
System.out.println("                            | | |                ");
System.out.println("                            | | |                ");
System.out.println("                            | | |                ");
System.out.println("                          (      )              ");
System.out.println("                           ^^^^^               ");
System.out.println("                                                "); System.out.println("                     ASCII Vase          "); System.out.println("                                                "); System.out.println("                                                "); } public static void main(String[] args) { ASCIIart art = new ASCIIart(); art.print(); } }


image1

Application output.

My eBooks on Amazon.com

US    UK    BR    CA
US    UK    BR    CA
US   UK   BR   CA