Linux

  1. Home
  2. Computing & Technology
  3. Linux

Brew Your First Cup of Java on Linux

By Juergen Haas, About.com

Creating Your First Java Application

Your first application, FatCalories, will wait for you to enter two numbers and then compute and print out the result. To create this program, you will: (1) create a Java source file, (2) compile the source file into a byte code file, and (3) run the program contained in the byte code file. See specific instructions below:

Step 1. Create a Java Source File.

A source file contains text written in the Java programming language. You can use any text editor to create and edit source files.

You have two options:

* You can save the FatCalories.java file onto your computer. This way can save you some typing. Then, you can go straight to step 2.

* Or, you can follow the longer instructions:

(1) Bring up a shell (sometimes called terminal) window.

When the prompt first comes up, your current directory will usually be your home directory. You can change your current directory to your home directory at any time by typing cd at the prompt (typically a “%”) and then pressing Return.

The Java files you create should be kept in a separate directory. You can create a directory by using the command mkdir. For example, to create the directory java in your home directory, you would first change your current directory to your home directory by entering the following command:
% cd

Then, you would enter the following command:
% mkdir java

To change your current directory to this new directory, you would then enter: % cd java

Now you can start creating your source file.

(2) Start the Pico editor by typing pico at the prompt and pressing Return. If the system responds with the message pico: command not found, then Pico is most likely unavailable. Consult your system administrator for more information, or use another editor.

When you start Pico, it'll display a new, blank buffer. This is the area in which you will type your code.

(3) Type the code for FatCalories.java listed here into the blank buffer. Type everything exactly as shown. The Java compiler and interpreter are case-sensitive.

(4) Save the code by typing Ctrl-O. When you see File Name to write: , type FatCalories.java, preceded by the directory in which you want the file to go. If you wish to save FatCalories.java in the directory /home/smith/java, then you would type

/home/smith/java/FatCalories.java and press Return.

Use Ctrl-X to exit Pico.

Explore Linux

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Linux

  1. Home
  2. Computing & Technology
  3. Linux

©2009 About.com, a part of The New York Times Company.

All rights reserved.