PDF download Download Article PDF download Download 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. Open up a terminal on Ubuntu Linux and create your Java applications directory.
    Advertisement
  2. mkdir Java_Applications
    • This will create your Java_Applications directory.
  3. Type or copy/paste: cd Java_Applications
    • This will change you into your newly created Java_Applications directory.
  4. 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
  5. Advertisement

Community Q&A

Search
Add New Question
  • Question
    Do I need to compile my Java file?
    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"?
    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
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      About This Article

      Thanks to all authors for creating a page that has been read 287,863 times.

      Is this article up to date?

      Advertisement