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, edit AUTOEXEC.BAT file, 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 6u16

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

On Windows 98, Me, edit AUTOEXEC.BAT file. Open it in notepad and add the following lines of text in bold

PATH=C:\WINDOWS;C:\ WINDOWS\ COMMAND;C:\j2sdk1.4.2_08\bin;
SET JAVA_HOME=C:\j2sdk1.4.2_08\bin;
SET CLASSPATH=.;C:\ j2sdk1.4.2_08\classes;


The dot in class path indicates that 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 in my AUTOEXEC.BAT file 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 java application, but it may be useful later if you decide to install Tomcat server to develop and run java application for web.

Windows 2000 or Windows XP do not use AUTOEXEC.BAT file. 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

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

Create java application source code

To test configuration on your PC, create a test directory on you 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 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 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 you 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

Subscribe to our mail list to receive free code examples.

Comments

No comments yet.

Add Comment

* Required information
(never displayed)
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
 
1000
What is the sum of 1 + 2 + 3?
 
Enter answer:
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form inputs on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
 
Powered by Commentics