How To Compile And Run Java Code 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:

Correct output is:

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

Now change directory to your project source folder.

In my case it’s:

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:

To compile this class I call Linux Java compiler:

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

Output:

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

Running Java on Ubuntu

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:

Output:

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:

Output:

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?

1 Star2 Stars3 Stars4 Stars5 Stars
Loading...
Scroll Up