Java execute jar files

Run JAR File from Command Line

JAR is short for Java Archive. JAR files contain compressed Java project files. It includes the .class files and other metadata and resources needed for the project.

JAR files are a great way to share and distribute our Java code with others.

In this short tutorial, we will learn how to create and run a JAR file in Java.

Creating JAR Files

A JAR file is composed of the compiled Java .class files and other resources. We can create an executable or a non-executable JAR.

The difference between the two is that an executable JAR file contains a manifest file, while the non-executable JAR file does not contain this. This manifest file contains the name of the class that should be executed when the JAR is run.

For this tutorial, we will work with just a single class whose main method prints Hello World.

Our directory has two files, the .java file and the manifest file which will be required for executable JAR.

Current Directory files

We need the compiled .class files to create a JAR file. We first need to run the javac command to create the .class file from the .java file.

Creating an Executable JAR File

To create an executable JAR file, we also need a manifest file. The content of the manifest file is shown below.

Next, we can create a JAR file by running the following jar command.

jar -cfm HelloWorld.jar ManifestFile.txt HelloWorld.class

The -c flag is used to create an archive file. The -f flag is used to specify the file name. And the -m flag will include the content of the manifest file.

After executing the above command, a new JAR file with the name HelloWorld.jar is created in our current directory. Note that the .class file shown in the image below was created after running the javac command.

Creating a Non-Executable JAR File

To create a non-executable JAR file, we will exclude the -m flag. We don’t need to pass the name of the manifest file to our command.

jar -cf HelloWorld.jar HelloWorld.class

Running JAR Files

Running the Executable JAR File

We can use the following java command with the -jar option to run the executable JAR file.

Читайте также:  Opacity css internet explorer

The output of the command is shown below.

Output

Running the Non-Executable JAR File

Instead of using the -jar option, we will use the -cp option to run a non-executable JAR file. We need to specify the JAR file name and the main class name in the command.

java -cp HelloWorld.jar HelloWorld

The output of this command is shown below.

Output

Running JAR Files with Command-Line Arguments

Any Java application can have any number of command-line arguments. These arguments are passed to the main() method of the class through a string array. JAR files also contain classes, and we can pass command-line arguments when running a JAR file.

The arguments should be separated by whitespace. If any argument contains whitespace, then we must enclose that argument in quotes.

Let’s create a new class with a main method. The main() method should print the command-line arguments passed to it.

public class CommandLineArgs < public static void main(String args[]) < for(String s : args) System.out.print(s); >>

We will compile the .java file and create the executable and non-executable jar files, as shown in the previous sections.

To run the executable JAR, we will use the java -jar command and pass the arguments.

In the command below, three arguments have been passed. Since the first two should contain whitespace at the end, we have enclosed them in quotes.

java -jar demo.jar "Welcome " "Back " User

For the non-executable JAR, we will use the —cf option and pass the main class name. We will also pass the arguments as we did in the code above.

java -cp demo.jar CommandLineArgs "Welcome " "Back " User

Summary

JAR files are a great way to share and distribute our code with others. In this tutorial, we learned how to create a JAR file and run it from the command line. We also learned how to pass command-line arguments to the main() method of the classes in JAR files.

Источник

Running JAR-Packaged Software

Now that you have learned how to create JAR files, how do you actually run the code you packaged? Consider these scenarios:

  • Your JAR file contains an applet that is to be run inside a browser.
  • Your JAR file contains an application that is to be started from the command line.
  • Your JAR file contains code that you want to use as an extension.
Читайте также:  Html class btn group

This section will cover the first two situations. A separate trail in the tutorial on the extension mechanism covers the use of JAR files as extensions.

Applets Packaged in JAR Files

To start any applet from an HTML file for running inside a browser, you use the applet tag. For more information, see the Java Applets lesson. If the applet is bundled as a JAR file, the only thing you need to do differently is to use the archive parameter to specify the relative path to the JAR file.

As an example, use the TicTacToe demo applet. The applet tag in the HTML file that displays the applet can be marked up like this:

If the TicTacToe demo was packaged in a JAR file named TicTacToe.jar, you can modify the applet tag with the addition of an archive parameter:

The archive parameter specifies the relative path to the JAR file that contains TicTacToe.class. For this example it is assumed that the JAR file and the HTML file are in the same directory. If they are not, you must include the JAR file’s relative path in the archive parameter’s value. For example, if the JAR file was one directory below the HTML file in a directory called applets, the applet tag would look like this:

JAR Files as Applications

You can run JAR packaged applications with the Java launcher (java command). The basic command is:

The -jar flag tells the launcher that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all of the application-specific code.

Before you execute this command, make sure that the runtime environment has information about which class within the JAR file is the application’s entry point.

To indicate which class is the application’s entry point, you must add a Main-Class header to the JAR file’s manifest. The header takes the form:

The header’s value, classname, is the name of the class that is the application’s entry point.

For more information, see the Setting an Application’s Entry Point section.

Читайте также:  Треугольник

When the Main-Class is set in the manifest file, you can run the application from the command line:

To run the application from the JAR file that is in another directory, you must specify the path of that directory: java -jar path/app.jar

Источник

Using JAR Files: The Basics

JAR files are packaged with the ZIP file format, so you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking. These tasks are among the most common uses of JAR files, and you can realize many JAR file benefits using only these basic features.

Even if you want to take advantage of advanced functionality provided by the JAR file format such as electronic signing, you’ll first need to become familiar with the fundamental operations.

To perform basic tasks with JAR files, you use the Java Archive Tool provided as part of the Java Development Kit (JDK). Because the Java Archive tool is invoked by using the jar command, this tutorial refers to it as ‘the Jar tool’.

As a synopsis and preview of some of the topics to be covered in this section, the following table summarizes common JAR file operations:

AppletClassName.class archive width=width height=height> 

This section shows you how to perform the most common JAR-file operations, with examples for each of the basic features:

Creating a JAR File

This section shows you how to use the Jar tool to package files and directories into a JAR file.

Viewing the Contents of a JAR File

You can display a JAR file’s table of contents to see what it contains without actually unpacking the JAR file.

Extracting the Contents of a JAR File

You can use the Jar tool to unpack a JAR file. When extracting files, the Jar tool makes copies of the desired files and writes them to the current directory, reproducing the directory structure that the files have in the archive.

Updating a JAR File

This section shows you how to update the contents of an existing JAR file by modifying its manifest or by adding files.

Running JAR-Packaged Software

This section shows you how to invoke and run applets and applications that are packaged in JAR files.

Additional References

The documentation for the JDK includes reference pages for the Jar tool:

Источник

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