How to Create Your First Java Program on Ubuntu Linux

Download ArticleDownload 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 using the Java JDK (Java development kit), creating a directory, Java text file and making use of a text editor.

  1. How.com.vn English: Step 1 Open terminal on your system after installing Java on your system.
  2. How.com.vn English: Step 2 Create a directory to hold your Java programs.
    Open up a terminal on Ubuntu Linux and create your Java applications directory.
    Advertisement
  3. How.com.vn English: Step 3 Type:
    mkdir Java_Applications
    • This will create your Java_Applications directory.
  4. How.com.vn English: Step 4 Navigate to your Java_Applications directory.
    Type or copy/paste: cd Java_Applications
    • This will change you into your newly created Java_Applications directory.
  5. How.com.vn English: Step 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: nano HelloWorld.java or type gedit HelloWorld.java
  6. How.com.vn English: Step 6  Enter the following code below.
      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);}}
  7. How.com.vn English: Step 7 Save the file as HelloWorld.java
  8. How.com.vn English: Step 8 Compile the HelloWorld.java file into a Java class file by issuing the following command below.
    • 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)
  9. How.com.vn English: Step 9 Run or execute your Java class file by issuing the following command.
    • Type: java HelloWorld
  10. Advertisement

Community Q&A

Search
Add New Question
  • Question
    Do I need to compile my Java file?
    How.com.vn English: Community Answer
    Community Answer
    Yes. It will notify you of your programming mistakes. Compile it before you run it.
  • Question
    Where do I type "javac helloworld.java"?
    How.com.vn English: Community Answer
    Community Answer
    The "javac helloworld.java" is typed in command prompt (cmd) when you compile helloworld program. So javac is a primary Java Compiler used to compile the java source code into java bytecodes.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      About This Article

      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 19 people, some anonymous, worked to edit and improve it over time. This article has been viewed 283,457 times.
      How helpful is this?
      Co-authors: 19
      Updated: January 28, 2021
      Views: 283,457
      Categories: Ubuntu
      Thanks to all authors for creating a page that has been read 283,457 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement