Linux create java program

How to Create and Execute a .Jar File in Linux Terminal

A JAR (Java ARchive) is platform-independent file format used to aggregate many Java class files and associated metadata and resources such as text, images, etc, into a single file for distribution.

It allows Java runtimes to efficiently deploy an entire application in one archive file, and provides many benefits such as security, its elements may be compressed, shortening download times, allows for package sealing and versioning, supports portability. It also supports packaging for extensions.

In this article, we will show how to create a simple Java application and bundle it into a JAR file, and demonstrate how to execute a .jar file from the Linux terminal.

To do this, you must have java command line tool installed to launche a Java application, and the -jar flag to execute a program encapsulated in a JAR file. When this flag is used, the specified JAR file is the source of all user classes, and other class path settings are ignored.

How to Create a JAR File in Linux

1. First start by writing a simple Java class with a main method for an application called TecmintApp, for demonstration purpose.

Copy and paste the following code to TecmintApp.java file.

Save the file and close it.

2. Next, we need to compile and pack the class into a JAR file using the javac and jar utilities as shown.

$ javac -d . TecmintApp.java $ ls $ jar cvf tecmintapp.jar TecmintApp.class $ ls

3. Once tecmintapp.jar created, now you can excute the file using java command as shown.

$ java -jar tecmintapp.jar no main manifest attribute, in tecmintapp.jar 

From the output of the above command, we encountered an error. The JVM (Java Virtual Machine) couldn’t find our main manifest attribute, thus it could not locate the main class containing the main method (public static void main (String[] args)).

The JAR file should have a manifest that contains a line in the form Main-Class:classname that defines the class with the main method that serves as our application’s starting point.

4. To fix the above error, we will need to update the JAR file to include a manifest attribute together with our code. Let’s create a MANIFEST.MF file.

Читайте также:  Резервное копирование при помощи linux

Copy and paste the following line to MANIFEST.MF file.

Save the file and let’s add the file MANIFEST.MF to our tecmintapp.jar using following command.

$ jar cvmf MANIFEST.MF tecmintapp.jar TecmintApp.class

5. Finally, when we executed the JAR file again, it should produce the expected result as shown in the output.

$ java -jar tecmintapp.jar Just executed TecmintApp! 

For more information, see the java, javac and jar command man pages.

$ man java $ man javac $ man jar

That’s all! In this short article, we have explained how to create a simple Java application and bundle it into a JAR file, and demonstrated how to execute a .jar file from the terminal. If you have any questions or supplementary ideas to share, use the feedback form below.

Источник

Your First Java Program in the Ubuntu Terminal

Ubuntu Java Development

Ubuntu Java Development

If you are new to Java Programming in the Ubuntu operating system, this simple tutorial will guide you along in writing and compiling your first Java Hello World program. The applications that you need for this purpose include the Java Runtime Environment and the Java Development Kit. This article covers the installation of these two through the Ubuntu command line. You can then write your first program in a text editor such as gedit, nano or even some graphical text writing tool. You will then compile your program in order to create a class that you can then execute in order to run your Java program.

We have run the commands and procedures mentioned in this article on an Ubuntu 18.04 LTS system.

Java Installation

In order to compile and run a basic Java program, you need to have the following two software installed on your Ubuntu system:

Let us first open the Ubuntu command line, the Terminal, in order to install these two. You can open the Terminal either through the system Dash or the Ctrl+alt+T shortcut.

Since we will be installing Java through the apt utility, first let us update our apt repositories through the following command:

The next thing is to run the following command:

This will ensure either Java Runtime Environment is already installed on your system or not. If yes, it will also let you know which version of Java you have on your system.

In my case, the output shows that I do not have Java installed on my system yet.

Читайте также:  Курсоры для windows 7 linux

Java version

In order to install the Java Runtime Environment on your system, run the following command as root:

Install Java default JRE

The system will prompt a Y/n option for you to continue installation. Please enter Y to continue, after which JRE will be installed on your system.

You can then check the installed version of Java as follows:

Check Java version again

After installing the JRE, let us check if we have the Java Development Kit installed on our system or not. This can be done by checking the version of your Java Compiler, javac, as follows:

javac version

The above output shows that I need to install the Java compiler or the JDK on my system.

You can install it through the following command as root:

$ sudo apt install default-jdk

Install Java JDK

The system will prompt a Y/n option for you to continue installation. Please enter Y to continue, after which JDK will be installed on your system.

You can then check the installed version of the Java Compiler as follows:

Check java compiler version again

The basic installations you need to have before running a Java program are now complete.

Your First Java Program

Before starting to write Java programs, it is best practice to make a dedicated directory for all your Java related work. I am creating such a directory through the following command in my home folder:

Then move to that directory as follows:

Create project directory

The next step is to write your first Java program. You can write it in any of your favorite text editors. Here, I am using the gedit editor to write the program. Run the following command in order to open a new java file in the gedit editor:

Open new Java source code file

Then copy the following code in your file:

Java program code

This program is simply intended to print “Hello World” on your screen.

Save the file and close it.

Then is the time to compile your first program through the Java compiler as follows:

The ls command will show that the compiler will create a class based on the class in your Java code:

Compile java sourcecode

In order to run the compiled program, run the following program:

Running Japa program

In this article, you have learned to install both the Java Runtime Environment and the Java Development Kit used to compile and run your java programs. We also wrote a simple Hello World program in Java and ran to see if you and your system are ready to move to the more complex world of Java Programming.

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Читайте также:  Linux сменить пользователя командная строка

Latest Tutorials

Источник

How to create your first Java program on CentOS 8

There is one JVM per java process. You can run the jps command (from the bin folder of JDK if it is not in your path) to find out what java processes (JVMs) are running on your machine. Depends on the JVM and native libs. You may see JVM threads show up with distinct PIDs in ps .

How do I know if a jar is running on Linux?

  1. jar is running and grep is in process list -> grep returns 2.
  2. jar is running and grep is not in process list -> grep returns 1.
  3. jar is not running and grep is in process list -> grep returns 1.
  4. jar is not running and grep is not in process list -> grep returns 0.

How do I run Java?

  1. Open a command prompt window and go to the directory where you saved the java program (MyFirstJavaProgram. java). .
  2. Type ‘javac MyFirstJavaProgram. java’ and press enter to compile your code. .
  3. Now, type ‘ java MyFirstJavaProgram ‘ to run your program.
  4. You will be able to see the result printed on the window.

How do I install Java on Linux terminal?

  1. Open the terminal (Ctrl+Alt+T) and update the package repository to ensure you download the latest software version: sudo apt update.
  2. Then, you can confidently install the latest Java Development Kit with the following command: sudo apt install default-jdk.

How do I install Java?

  1. Go to the Manual download page.
  2. Click on Windows Online.
  3. The File Download dialog box appears prompting you to run or save the download file. To run the installer, click Run. To save the file for later installation, click Save. Choose the folder location and save the file to your local system.

How To Install Snap on Ubuntu / Debian Linux

Snap

Can I install snap on Debian?How do I install snap on Linux?How do I enable Snap support in Ubuntu?How do I download SNAP store on Ubuntu?What is inst.

Make Sudo Insult User For Each Incorrect Password Attempt

Sudo

How do I enable Sudo insults?How can I Sudo another user without password?How do I force Sudo to ask for password?Where are Sudo insults stored?What d.

How to Download and Install Google Fonts on Ubuntu 20.04

Fonts

The application is available in the Ubuntu APT package repository and can easily be installed on Ubuntu 20.04 LTS system.Step 1: Open up Ubuntu’s Term.

Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system

Источник

Оцените статью
Adblock
detector