How to create your first Java program on Ubuntu Linux

Author: Marcus Baldwin
Date Of Creation: 18 June 2021
Update Date: 24 June 2024
Anonim
How to Create Your First Java Program on Ubuntu Linux
Video: How to Create Your First Java Program on Ubuntu Linux

Content

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

  1. 1 Open terminal when Java is installed.
  2. 2 Create a folder for Java programs. Open a terminal and create a folder. For this:
  3. 3 Enter the command mkdir Java_Applications
    • The folder "Java_Applications" will be created.
  4. 4 Go to the Java_Applications folder. Enter (or copy and paste) the command cd Java_Applications
    • You will be taken to the created folder "Java_Applications".
  5. 5 In a text editor like nano or gedit, create a Java file. For example, let's write the simplest 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
  6. 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); }}

  7. 7 Save the file as HelloWorld.java
  8. 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)
  9. 9 Run the created program. To do this, enter the following command.
    • java HelloWorld