SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES

FORGOT YOUR PASSWORD?

FORGOT YOUR DETAILS?

AAH, WAIT, I REMEMBER NOW!
Openbaraza

Openbaraza

Openbaraza

Baraza is a Swahili word that means a deliberation meeting held by a collective group of people with wisdom.

T : +254 (20) 2243097 / 2227100
Email: info@openbaraza.org

OPENBARAZA
Dew CIS Solutions LTD Barclays Plaza, 12th Floor. Loita Street.

Open in Google Maps
  • Framework
  • Projects
    • Human Resource
      • Human Resource
      • Time Attendance
      • Project Planning
    • Finance Management
      • Business
      • Payroll
    • Investment Management
      • Banking
      • Chama
      • Sacco
    • Case Management
      • Judiciary
    • Real Estate Management
      • Property
    • Academic Management
      • UMIS
      • Schools
  • Forums
    • HCM HR Payroll
    • Business
    • Sacco
    • Banking
  • Blog & News
    • OpenBaraza Framework
    • Databases
    • Java Programming
    • Scrum
  • Contact Us
  • Home
  • Blog & News
  • Java Programming
  • 02. Learning Java in Linux
May 18, 2025

02. Learning Java in Linux

02. Learning Java in Linux

by dennis@dennis.me.ke / Thursday, 06 June 2019 / Published in Java Programming

Learning Java in Linux

One of the main motivations for the Java programming language is the notion of having the ability to write one set of source code and give the resulting program the ability to run on different operating systems or operating environments.

Java has this “write once, run anywhere” ability due to the way the compiler translates the source code into a special file called a “Byte Code” file, that can run under any supported Java Runtime Environment (JRE).

Java development process steps ; 

  1. Write the Java source code and save this code in one or more plain text files. These files typically have .java at the end of their file name.
  2. Run the Java Compiler (javac) to “compile” the source code into a “Byte Code” file. These Byte Code files typically have .class at the end of their file name.
  3. Run the program by submitting the byte code file to a Java Runtime Environment by way of the java

The advantage of this approach is that it is generally possible to copy the byte code (.class) file to any operating system that supports a JRE, and run the program directly.

Download and Install Java Development Kit

Most Linux distributions such as Debian. Ubuntu, Red Hat and CentOS include a software repository tool that automates the download and install steps. For example, under Debian and Ubuntu Linux distributions, the apt-get command can be used to download and install the JDK. For example:

sudo apt-get update

sudo apt-get install oracle-java12-installer

 

Writing, Compiling and Testing your first Java Program.

When I started programming in Java, I was lost simply because I did not understand how to build applications using this language. So I decided to build this app as my first Java application. In Linux the nano or pico editors or edit/kedit or kate programs are fine for editing Java source code files. In our case, we will use kate. For the purposes of this series  we will stick to using  simple tools.

The first program we will write is a very simple “Area of a Rectangle” example.

  1. In Linux, search for the Terminal program and open it.
  2. Create a new file using the kate program by searching for it. The kate program will open as follows.

 

  1. Type in the following Java source code (we’ll explain what each line does below).

public class Area{

public static void main(String []args){

int length;

int width ;

int area;

length = 4;

width = 3;

area = (length * width);

System.out.println(“The area is “+area);

}

}

  1. Save your file by clicking on “file” located at the top on your left. A drop down will appear then select “save as”. Name your file with an extension of .java. Make sure the name of your file resembles that of your class. Click save. When completed kate should now look like the following:

 

As a way of explanation, here are some comments (followed by the //) that describe the program.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

LIVE A LITTLE  

Why was the computer late for work? Because it had a hard drive

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – 

 

//This is the start of our program. A public class is accessible by all the java code, even outside the package.

public class Area{

/*The main method where our program begins. Program cannot execute without this method.     public – access specifier means from everywhere we can access it static – access modifier means we can call this method directly using class name without creating an object of it void –  it returns nothing main – method name string [] args – in java accept only string type of argument and stores it in a string*/

public static void main (String []args){

 

//Declare your variables of data type integer

int length;

int width;

int area;

//Initialize your variables

length = 4;

width = 3;

//area stores the result after computing (length * width)

area = (length * width);

//Prints the result.

System.out.println (“The area is “+area);

//End of “main” method

}

//End of the class

}

  1. Exit out of kate.
  2. Compile the program using the javac Type this command javac Area.java as shown below in the terminal.
  3. If there are no syntax errors in your code, the compiler will simply finish up silently.
  4. If Linux cannot locate the javac compiler, the following error message will appear:

No command ‘javac’ found, but there are 18 similar ones

javac: command not found

Solution: Install jdk.

 

  1. Now run your sample program by running java followed by the name of your program

as shown below java Area in the terminal.

 

Now you have an idea on how to write and run a Java program, does being  a programmer  sound so mystical or scary? guess not. The fun doesn’t end there! Linux is user friendly only that its particular on who its friends are. If you are a Windows user, join the ride in the next post.

 

 

0
  • Tweet

About dennis@dennis.me.ke

What you can read next

04. Control Structures
08. BODMAS IN JAVA
05. Variables

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 08. BODMAS IN JAVA

  • 07. JAVA ARRAYS: Store Student Details Using Arrays and List

  • 06. Classes: Store Student Details Using Json Format

  • 02. SPRINT RETROSPECTIVE

  • 01. SCRUM (DEMO)

Categories

  • Databases
  • Java Programming
  • OpenBaraza Framework
  • Scrum

QUICK LINKS

  • Home
  • Our Stories
  • Contact Us
  • Terms and Conditions
  • Our Policy

VISIT US

Open Baraza
Barclays Plaza, 12th Floor.
Loita Street, Nairobi.

Open in Google Maps

GET IN TOUCH

Tel: +254 (20) 2243097 / 2227100
     +254(726)209214, +254(738)819505
Email: info@openbaraza.org

Follow Us

Could not authenticate you.
  • GET SOCIAL
Openbaraza

© All rights reserved | Open Baraza developed by DewCis Solutions.

TOP