How to Create Your First Java Program on Ubuntu Linux

Open terminal on your system after installing Java on your system., Create a directory to hold your Java programs., Type: mkdir Java_Applications This will create your Java_Applications directory. , Navigate to your Java_Applications directory., Use...

9 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Open terminal on your system after installing Java on your system.

    Open up a terminal on Ubuntu Linux and create your Java applications directory. ,, Type or copy/paste: cd Java_Applications This will change you into your newly created Java_Applications directory. , In this example we will use the traditional first program known as “Hello world”.

    This will open up a blank Java text file to work with and now we will insert some text into our Java file.

    So using nano or gedit we will issue the following command:
    Type: nano HelloWorld.java or type gedit HelloWorld.java , import javax.swing.*; public class HelloWorld extends JFrame { public static void main(String[] args) { new HelloWorld(); } public HelloWorld() { JPanel panel1 = new JPanel(); JLabel label1 = new JLabel("Hello, World, this is my first Java program on Ubuntu Linux"); panel1.add(label1); this.add(panel1); this.setTitle("Hello World"); this.setSize(500,500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } ,, Type javac HelloWorld.java (This will fail if you don't have javac installed, if it fails, either follow the instructions in the introduction or type on your command line: sudo apt-get install openjdk-7-jdk) , Type: java HelloWorld
  2. Step 2: Create a directory to hold your Java programs.

  3. Step 3: Type: mkdir Java_Applications This will create your Java_Applications directory.

  4. Step 4: Navigate to your Java_Applications directory.

  5. Step 5: Use a text editor such as nano or gedit to create a java file.

  6. Step 6: Enter the following code below.

  7. Step 7: Save the file as HelloWorld.java

  8. Step 8: Compile the HelloWorld.java file into a Java class file by issuing the following command below.

  9. Step 9: Run or execute your Java class file by issuing the following command.

Detailed Guide

Open up a terminal on Ubuntu Linux and create your Java applications directory. ,, Type or copy/paste: cd Java_Applications This will change you into your newly created Java_Applications directory. , In this example we will use the traditional first program known as “Hello world”.

This will open up a blank Java text file to work with and now we will insert some text into our Java file.

So using nano or gedit we will issue the following command:
Type: nano HelloWorld.java or type gedit HelloWorld.java , import javax.swing.*; public class HelloWorld extends JFrame { public static void main(String[] args) { new HelloWorld(); } public HelloWorld() { JPanel panel1 = new JPanel(); JLabel label1 = new JLabel("Hello, World, this is my first Java program on Ubuntu Linux"); panel1.add(label1); this.add(panel1); this.setTitle("Hello World"); this.setSize(500,500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } ,, Type javac HelloWorld.java (This will fail if you don't have javac installed, if it fails, either follow the instructions in the introduction or type on your command line: sudo apt-get install openjdk-7-jdk) , Type: java HelloWorld

About the Author

R

Ryan Chapman

A passionate writer with expertise in pet care topics. Loves sharing practical knowledge.

45 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: