How to run java programs on Ubuntu

Table of contents

1. Environment introduction

2. Equipment introduction

3. Preliminary preparation

  3.1. Install jdk for Ubuntu

  3.2. Compile the jar package and store it in the ubuntu folder after compilation, I store it in /usr/src/

4. Create the autostart folder

5. Create a javaTest.desktop file (name according to your own requirements)

6. Restart the ubuntu system

Summarize



1. Environment introduction

  • JAVA

Intellij IDEA 2022.3.3 (Ultimate Edition)

jdk-1.8.0_77

  • Ubuntu

Linux version 5.10.110 (root@seven-HP-ZHAN-99-Pro-G1-MT) (aarch64-none-linux-gnu-gcc (GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)) 10.3.1 20210621, GNU ld (GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)) 2.36.1.20210621) #11 SMP Fri Feb 10 18:15:24 CST 2023

openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu120.04.1)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu120.04.1, mixed mode)

2. Equipment introduction

3. Preliminary preparation

  3.1. Install jdk for Ubuntu

The code is as follows (terminal):

 3.11 Install directly using the apt command

armt@localhost:~$ apt-cache search java11

openjdk-11-jdk - OpenJDK Development Kit (JDK)
openjdk-11-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-11-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-11-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
default-jdk - Standard Java or Java compatible Development Kit
default-jdk-headless - Standard Java or Java compatible Development Kit (headless)

3.12 Select the jdk version to be installed, and then enter y to install 

armt@localhost:~$ sudo apt-get install openjdk-11-jdk

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  openjdk-11-jdk-headless openjdk-11-jre openjdk-11-jre-headless
Suggested packages:
  openjdk-11-demo openjdk-11-source visualvm fonts-ipafont-gothic
  fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei
The following NEW packages will be installed:
  openjdk-11-jdk openjdk-11-jdk-headless openjdk-11-jre
  openjdk-11-jre-headless
0 upgraded, 4 newly installed, 0 to remove and 4 not upgraded.
Need to get 281 MB of archives.
After this operation, 435 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

3.13 After the installation is complete, check whether the jdk is installed successfully, and print the following results to indicate that the installation is successful

armt@localhost:~$ java -version

openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu120.04.1)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu120.04.1, mixed mode)

  3.2. Compile the jar package and store it in the ubuntu folder after compilation, I store it in /usr/src/

  • You can use your own java program and package it into a jar
  • If you don't have it yet, you can click the link below to download it. The jar package is in the \JavaUbuntTest\out\artifacts\ directory

 Screenshot of software running

 

4. Create the autostart folder

  Steps

  1. open terminal
  2. Create autostart folder
  3. Switch to the autostart directory
armt@localhost:~$ mkdir /home/armt/.config/autostart
armt@localhost:~$ cd /home/armt/.config/
armt@localhost:~/.config$ ls

autostart  gtk-3.0           mimeapps.list  Thunar            xfce4
dconf      ibus              Mousepad       update-notifier   xubuntu
evolution  libaccounts-glib  pulse          user-dirs.dirs
goa-1.0    libreoffice       QQ             user-dirs.locale

armt@localhost:~/.config$ cd autostart/

Note: armt is the user name, modify here according to your own ubuntu system, and autostart is all lowercase

5. Create a javaTest.desktop file (name according to your own requirements)

  • 5.1.1. The first method is to create a .desktop file in the operating system, then copy the code into the file, and finally copy the file to the /autostart folder (you can skip 5.1.2 directly)

  • 5.1.2. The second method uses the touch command to create a file, and the vim command to edit a file
  • 5.1.2.1 Creating files

  • armt@localhost:~/.config$ cd autostart/
    armt@localhost:~/.config/autostart$ ls
    armt@localhost:~/.config/autostart$ touch javaTest.desktop
    armt@localhost:~/.config/autostart$ ls
    javaTest.desktop
    armt@localhost:~/.config/autostart$ 
  • 5.1.2.2 Editing files

  • armt@localhost:~/.config/autostart$ vim javaTest.desktop 
    armt@localhost:~/.config/autostart$

After opening the .desktop file, enter i to open, press ESC after input, and then enter wq! to save, you can check the information by yourself for specific operation details of vim

.desktop file content (modify Exec according to your own environment)

[Desktop Entry]
Encoding=UTF-8
Version=1
Type=Application
Name=testJavaStart                 
Comment=测试java开机启动
Exec=/usr/lib/jvm/java-11-openjdk-arm64/bin/java -jar /usr/src/JavaUbuntuTest.jar           
Terminal=false
Hidden=false

 The keywords are explained as follows:

/usr/lib/jvm/java-11-openjdk-arm64/bin/java //java jdk path
    
/usr/src/JavaUbuntuTest.jar //program jar package path

Screenshot of file storage location:

6. Restart the ubuntu system

 


Summarize

The above is how to start the java program on ubuntu boot

 In fact, it is very simple, just create a .desktop file, modify the configuration and store it in the /.config/autostart/ folder

Guess you like

Origin blog.csdn.net/qaz96801/article/details/130084104