Compiling java files in linux

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.

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.

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.

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'; > 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.

-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.

Читайте также:  Linux execute bin files

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:

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).

and try to run the same class:

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

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.

Источник

Оцените статью
Adblock
detector