Create Your First Java Program on Ubuntu Linux
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 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.
Steps
- Open terminal on your system after installing Java on your system.
- Create a directory to hold your Java programs. Open up a terminal on Ubuntu Linux and create your Java applications directory.
- Type: mkdir Java_Applications
- This will create your Java_Applications directory.
- Navigate to your Java_Applications directory. Type or copy/paste: cd Java_Applications
- This will change you into your newly created Java_Applications directory.
- 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
- Enter the following code below.
- Save the file as HelloWorld.java
- 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)
- Run or execute your Java class file by issuing the following command.
- Type: java HelloWorld
Related Articles
- Create Your First Qt Program on Ubuntu Linux
- Install Netbeans on a Linux