9/03/2013

How To Use Command Prompt To Run A Simple Java Program

How To Use Command Prompt To Run A Simple Java Program

Head First Java 2nd Edition Book Kindle And Paperback We are going to write the most basic of Java programs that will output: Welcome to Java! on the command prompt.

You can either use Text Editor to write your programs which comes on your computer or download Notepad++ which offers more features and help when writing programs!

The Tutorial:

Start by opening up Text Editor on your computer.

To do this, right click your desktop screen and select New, then select Text Document

Change the name of the file to Welcome_to_Java.java and drag the file in to your java bin folder

Now we need to show the file extensions, to do this follow this simple tutorial:

>>> How To Change .txt Files To .java By Showing Windows File Name Extensions

Now go in to your java bin folder and locate your Welcome to Java file which probably has two file extensions and looks like this: Welcome_to_Java.java.txt or Welcome_to_Java.txt.java
We want to get rid of the .txt and keep the .java, do this by erasing the .txt from the file so you end up with Welcome_to_Java.java

Click Yes on the prompt box that pops up about renaming the file
Now your file should say Welcome_to_Java.java

Let's write the program!

Open up your Welcome_to_Java.java file which should be blank and type this in:

public class Welcome_to_Java {
  public static void main(String[] args) {
    //display message welcome to java! on the console
    System.out.println("Welcome to Java!");
  }
}

Now Save your file!

Let's break down the program:

Line 1: class is a reserved word for programming and the term public class let's the program know the name for the class and in our case the file Welcome_to_Java (always make sure the public class name exactly matches the name of your file!)
Line 2: This is the main method and where the program is executed and begins compiling
Line 3: This is a comment line, any time you type // the words following the two brackets will not show up in the program and they are to help the programs creator or others understand how the program works, use comments often!
Line 4: This shows our string statement which is a sequence of characters that will be generated and in our case it's Welcome to Java!
Line 5 and 6: Closing brackets

Let's run the program!

Open up the command prompt shortcut on your desktop that is set up to read files from your java bin folder

Where the blinking cursor is proceeding the folder location type in javac Welcome_to_java.java

First Tutorial How To Use Command Prompt To Run A Simple Java Program


If the program has no errors it will look like this:

Second Tutorial How To Use Command Prompt To Run A Simple Java Program


If you have any errors the command prompt will tell you where they are and how to fix them (Remember that everything is case sensitive, make sure you are typing in the correct name of the file)

Now let's run the program and output what our program does, in this case it will output Welcome to Java!

Where the blinking cursor is type in java Welcome_to_Java

If you did it correctly it will output Welcome to Java! and look like this:

Third Tutorial How To Use Command Prompt To Run A Simple Java Program


You're done, you have successfully completed your first of many Java programs!

No comments:

Post a Comment


Copyright © justinwoodie.com ©