Try your first JAVA program


Try out the following steps:

1. First you need to have the Java Development Kit. You can download the kit on this website-

http://www.oracle.com/technetwork/java/javase/downloads/jdk6u35-downloads-1836443.html

You can download the JDK SE 6, there’s the new version 7, but you can stick with version 6 for now. In the download website, choose which operating system you are using and just follow the instructions on how to download the JDK. I’m using Windows OS, the steps that I’m going to discuss is in windows environment, and the Editor is Notepad.

2. After you downloaded the java development kit, then you are ready to try your first java program.

3. Launch Notepad and then type the following code:

public class FirstProgram{

       public static void main(String[] args){

          System.out.println(“This is to try my first program”);
       }
}

Let’s just try this program and on my next post I’ll explain each line in the code. For now, let me explain what is System.out.println(“This is to try my first program”);

*This is the program statement in java to display a message into the screen.

* The message that you want to display is enclosed in the double quotations and then enclosed inside the parenthesis.

*semicolon is a symbol which means to end the line or terminate the program statement. Program statement is like a sentence then semicolon is like the period in the sentence.

*System.out.println – is the command to display or print a message into an output stream which is the monitor.

* The command might be wordy and very long to type but later on you’ll get used to it. The main idea here is that, it’s readable and the commands are easy to understand.

* Later on, we’ll try more sophisticated programs like programs with graphics components.

4. Save the file with extension name .java like this – FirstProgram.java, it’s very important to remember that the   filename should be the same as your class name – class FirstProgram. Java is case sensitive, so make sure it is exactly the same as your class name on how it was written. For easy access, you can save your program in C: drive, you can create a folder and name it as – javacodes. So, your program should be in C:/javacodes directory.

5. In start menu, select Run, then on the Open box, type cmd to open command prompt.

6. On the command prompt, access your directory where you save your program, type C: then press enter. On C:\ , type cd javacodes (ex. C:\cd javacodes)

7. On the javacodes directory, we’ll set the path where your jdk is located, make sure you know where is jdk is save in your computer, copy the path. To set the path, here’s how:

C:\javacodes>set path=”C:\Program Files\Java\jdk1.6.0_30\bin” 

set path command is to be able to access the compiler and interpreter of java so that your java program in your directory can be compiled and interpreted.

8. Compile your java program using javac command, and here’s how:

C:\javacodes> javac FirstProgram.java

After compiling your .java source code, another file will be created which is .class file. This .class file is called bytecode.

Bytecode can be ready to run and interpreted in any system. So the next step is to run our bytecode file.

9. Run your program using java command, and here’s how:

C:\javacodes> java FirstProgram

So here you’ll notice that the command is java to run your java program and then the filename without the extension name.

10. Wait then you’ll see your output:

This is to try my first program

Additional Information:

If you would like to use an IDE (Integrated Development Environment) system to type and run your java program, here are my recommended links:

To download NetBeans: http://netbeans.org/

To download Eclipse: http://www.eclipse.org/downloads/

For questions and clarifications, don’t hesitate to leave your message in the comment section.

Goodluck!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s