How to create your first Java program on Ubuntu Linux

Table of contents:

How to create your first Java program on Ubuntu Linux
How to create your first Java program on Ubuntu Linux
Anonim

To use the method described in this article, you must have a Java development environment such as Oracle Java, OpenJDK, or IBM Java installed on your computer. If not, read this article or simply enter (in the terminal) the command sudo apt-get install openjdk-7-jdk

If you have Java installed on your computer, create a new environment so that you can write your first Java program later. Some users use an IDE such as the Eclipse IDE or NetBeans IDE in which to write programs. This approach simplifies programming when many Java class files are used.

This article describes Java programming without using an IDE, but using the Java JDK, directory, Java text file, and text editor.

Steps

Create Your First Java Program on Ubuntu Linux Step 1
Create Your First Java Program on Ubuntu Linux Step 1

Step 1. Open Terminal when Java is installed

Create Your First Java Program on Ubuntu Linux Step 2
Create Your First Java Program on Ubuntu Linux Step 2

Step 2. Create a folder for Java programs

Open a terminal and create a folder. For this:

Create Your First Java Program on Ubuntu Linux Step 3
Create Your First Java Program on Ubuntu Linux Step 3

3 Enter the command mkdir Java_Applications

The folder "Java_Applications" will be created

Create Your First Java Program on Ubuntu Linux Step 4
Create Your First Java Program on Ubuntu Linux Step 4

Step 4. Go to the "Java_Applications" folder

Type (or copy and paste) the cd Java_Applications command

You will be taken to the created folder "Java_Applications"

Create Your First Java Program on Ubuntu Linux Step 5
Create Your First Java Program on Ubuntu Linux Step 5

Step 5. In a text editor such as nano or gedit, create a Java file

For example, let's write a simple Hello World program. In a text editor, you need to enter several lines of program code.

  • In nano or gedit, enter the following command:
  • nano HelloWorld.java or gedit HelloWorld.java
Create Your First Java Program on Ubuntu Linux Step 6
Create Your First Java Program on Ubuntu Linux Step 6

Step 6. Now enter the following lines of code

    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); }}

Step 7. Save the file as HelloWorld.java 8 Compile the HelloWorld.java file into a Java class file

To do this, enter the following command.

  • javac HelloWorld.java
  • (the file will not compile if there is no javac on the computer; in this case, read the information in the introduction or enter (in a terminal) the command sudo apt-get install openjdk-7-jdk)

Step 9. Run the created program

To do this, enter the following command.

Popular by topic