How To Compile and Run Java Program in Linux

Today I’ll explain how to compile and run Java code on Linux environment.

Personally, I like Ubuntu, that’s why I’ll show you how to do it there.

First of all, you have to be sure that Java is installed on your machine.

Just type:

java -version

Correct output is:

java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

If it’s not installed you can do it easy:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Now change directory to your project source folder.

In my case it’s:

cd ~/workspace/explainjava/src/

Inside of my project I have a class Car, that I wrote when I talked about the transient keyword in Java.

It looks like this:

package com.explainjava;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
public class Car implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private String name;
    private transient String color;
 
    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
 
    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
 
    public static void main(String args[]) throws Exception {
        Car car = new Car("BMW", "red");
 
        // serialize object and save it to file
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("carStorage"));
        outputStream.writeObject(car);
        outputStream.close();
 
        // reading read file and deserialize bytes to object
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("carStorage"));
        Car deserializedCar = (Car) inputStream.readObject();
        System.out.println(deserializedCar);
    }
}

How to Compile Java Program

To compile this class I call Linux Java compiler:

javac com/explainjava/Car.java

If everything was OK you should find a new file Car.class near Car.java.

ls -l com/explainjava/

Output:

-rw-rw-r-- 1 dshvechikov dshvechikov 1447 dec  5 09:06 Car.class
-rw-rw-r-- 1 dshvechikov dshvechikov 1236 nov 17 15:29 Car.java

Our class is compiled, the next step is to run compiled Java class on Ubuntu.

How to Run Java Program

In the previous section, I compiled Java code in Linux.

Now I’m going to show how to run Java program.

Just point Java to compiled class like this:

java com.explainjava.Car

Output:

Car{name='BMW', color='null'}

Note that you should run Java class from the source folder.

For example, if I change directory to project (one level upper than src folder).

cd ..

and try to run the same class:

java src.com.explainjava.Car

Output:

Error: Could not find or load main class src.com.explainjava.Car

It doesn’t work!

So you have to be sure that you’re running your Java code on Ubuntu from the source folder.

For compiling process it doesn’t matter.

As you can see it’s easy to compile and run Java on Linux.

Personally, I prefer Java programming on Linux instead of Windows.

I think Java developers should know at least basic Linux commands.

Any questions?

Leave a Comment