Edit Article

This document presumes you have some kind of Java software development environment installed on your system such as Oracle Java, OpenJDK or IBM Java.If you don't have a Java development environment setup please see the following document How to Install Oracle Java on Ubuntu Linux, or quick and dirty, type on your command line: sudo apt-get install openjdk-7-jdk


If Java is installed on your system then our next task is setting up a clear environment in order to create our first Java program. Some people prefer to use an IDE( integrated development environment ) such as Eclipse IDE or NetBeans IDE to write their programs. Since it makes programming tasks less complicated when you are working with many Java class files.


For this example, we are going to work manually with Java programming without the use of an IDE. By this I mean using the Java JDK( Java development kit ), creating a directory, Java text file and making use of a text editor.

Ad

Steps

  1. Create Your First Java Program on Ubuntu Linux Step 1 Version 2.jpg
    1
    Open terminal on your system after installing Java on your system.
    Ad
  2. Create Your First Java Program on Ubuntu Linux Step 2 Version 2.jpg
    2
    Create a directory to hold your Java programs. Open up a terminal on Ubuntu Linux and create your Java applications directory.
  3. 3
    ’’’Type/Copy/Paste: mkdir Java_Applications
    • This will create your Java_Applications directory.
      Create Your First Java Program on Ubuntu Linux Step 3 Version 2.jpg
  4. 4
    Navigate to your Java_Applications directory. ‘’’Type/Copy/Paste: cd Java_Applications
    • This will change you into your newly created Java_Applications directory.
      Create Your First Java Program on Ubuntu Linux Step 4 Version 2.jpg
  5. 5
    Use a text editor such as nano or gedit to create a java file. 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/Copy/Paste: nano HelloWorld.java or Type/Copy/Paste: gedit HelloWorld.java
        Create Your First Java Program on Ubuntu Linux Step 5 Version 2.jpg
    • Enter the following code below. ‘’’Type/Copy/Paste:
      Create Your First Java Program on Ubuntu Linux Step 6 Version 2.jpg
    Ad


Create Your First Java Program on Ubuntu Linux Step 7 Version 2.jpg
  1. #import javax.swing.*;
    
  2. public class HelloWorld extends JFrame
  3. {
  4. public static void main(String[] args)
  5. {
  6. new HelloWorld();
  7. }
  8. public HelloWorld()
  9. {
  10. JPanel panel1 = new JPanel();
  11. JLabel label1 = new JLabel("Hello, World, this is my first Java program on Ubuntu Linux");
  12. panel1.add(label1);
  13. this.add(panel1);
  14. this.setTitle("Hello World");
  15. this.setSize(500,500);
  16. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. this.setVisible(true);
  18. }
  19. }
  1. 1
    Save the file as HelloWorld.java
  2. 2
    Next we are going to compile the HelloWorld.java file into a Java class file by issuing the following command below.
  3. 3
    Type/Copy/Paste: 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)
  4. 4
    Run or execute your Java class file by issuing the following command.
  5. 5
    Type/Copy/Paste: java HelloWorld
    Ad

We could really use your help!

Can you tell us about
heat rashes?
Yes
No
heat rashes
how to get rid of heat rash
Can you tell us about
unfreezing iPods?
Yes
No
unfreezing iPods
how to unfreeze and iPod
Can you tell us about
relationships?
Yes
No
relationships
how to stop a break up
Can you tell us about
soft hands?
Yes
No
soft hands
how to make your hands soft
Thanks for helping! Please tell us everything you know about
...
Tell us everything you know here. Remember, more detail is better.
Tips
Provide Details.
Please be as detailed as possible in your explanation. Don't worry about formatting! We'll take care of it. For example:
Don't say: Eat more fats.
Do say: Add fats with some nutritional value to the foods you already eat. Try olive oil, butter, avocado, and mayonnaise.

Article Info

Categories: Ubuntu

In other languages:

Español: Cómo crear tu primer programa Java en Ubuntu Linux, Italiano: Come Creare il tuo Primo Programma Java in Ubuntu Linux, Русский: создать свою первую Java программу на Ubuntu Linux, Português: Como Criar Seu Primeiro Programa em Java no Ubuntu Linux

Thanks to all authors for creating a page that has been read 88,608 times.

Did this article help you?
Yes No

Become
an Author!

Write an Article