- Running JAR-Packaged Software
- Applets Packaged in JAR Files
- JAR Files as Applications
- Guide to Creating Jar Executables and Windows Executables from Java
- 1. Overview
- 2. Basics of the jar and the jpackage Commands
- 2.1. The jar Command
- 2.2. The jpackage Command
- 3. Creating Executable Files
- 3.1. Creating an Executable JAR File
- 3.2. Creating a Windows Executable
- 4. Conclusion
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.
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
Guide to Creating Jar Executables and Windows Executables from Java
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We rely on other people’s code in our own work. Every day.
It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.
The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.
Lightrun is a new kind of debugger.
It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.
Learn more in this quick, 5-minute Lightrun tutorial:
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:
DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.
The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.
And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We’re looking for a new Java technical editor to help review new articles for the site.
1. Overview
In this tutorial, we’ll start by learning how to package a Java program into an executable Java ARchive (JAR) file. Then, we’ll see how to generate a Microsoft Windows-supported executable file using that executable JAR.
We’ll use the jar command-line tool that comes with Java for creating JAR files. We’ll then learn to use the jpackage tool, available with Java 16 and later versions as jdk.jpackage, to generate an executable file.
2. Basics of the jar and the jpackage Commands
A JAR file is a container for compiled Java class files and other resources. It’s based on the popular ZIP file format.
An executable JAR file is also a JAR file but contains a main class as well. The main class is referenced in a manifest file, which we’ll discuss shortly.
In order to run an application delivered in a JAR format, we must have a Java Runtime Environment (JRE).
Unlike JAR files, a platform-specific executable file can run natively on the platform it was built for. For example, that platform could be Microsoft Windows, Linux, or Apple macOS.
For a good end-user experience, it’s preferred to provide clients with a platform-specific executable file.
2.1. The jar Command
The general syntax for creating a JAR file is:
jar cf jar-file input-file(s)
Let’s go through some options that can be used when creating a new archive with the jar command:
- c specifies that we want to create a JAR file
- f specifies that we want the output to go to a file
- m is used to include manifest information from an existing manifest file
- jar-file is the name that we want for the resulting JAR file. JAR files are generally given a .jar extension, but it’s not required.
- input-file(s) is a space-separated list of filenames that we want to include in our JAR file. The wildcard * can be used here as well.
Once we create a JAR file, we’ll often be checking its contents. To view what a JAR file contains, we use the following syntax:
Here, t indicates that we want to list the contents of the JAR file. The f option denotes that the JAR file that we want to check is specified on the command line.
2.2. The jpackage Command
The jpackage command-line tool helps us generate installable packages for modular and non-modular Java applications.
It uses the jlink command to generate a Java Runtime Image for our application. As a result, we get a self-contained application bundle for a specific platform.
Since the application packages are built for a target platform, that system must contain the following:
- the application itself
- a JDK
- a software that is needed by the packaging tool. For Windows, jpackage requires WiX 3.0 or later.
Here’s the commonly-used form of the jpackage command:
jpackage —input . —main-jar MyAppn.jar
3. Creating Executable Files
Now let’s go through creating an executable JAR file. Once that’s ready, we’ll work on generating a Windows executable file.
3.1. Creating an Executable JAR File
Creating an executable JAR is fairly simple. We’ll first need a Java project with at least one class with the main() method. We created a Java class named MySampleGUIAppn for our example.
The second step is to create a manifest file. Let’s create our manifest file as MySampleGUIAppn.mf:
Manifest-Version: 1.0 Main-Class: MySampleGUIAppn
We have to make sure there’s a newline at the end of this manifest file for it to work correctly.
Once the manifest file’s ready, we’ll create an executable JAR:
jar cmf MySampleGUIAppn.mf MySampleGUIAppn.jar MySampleGUIAppn.class MySampleGUIAppn.java
Let’s view the contents of the JAR that we created:
jar tf MySampleGUIAppn.jar
META-INF/ META-INF/MANIFEST.MF MySampleGUIAppn.class MySampleGUIAppn.java
Next, we can run our JAR executable via a CLI or in a GUI.
Let’s run it on the command line:
java -jar MySampleGUIAppn.jar
In a GUI, we can simply double-click the relevant JAR file. That should launch it normally as any other application.
3.2. Creating a Windows Executable
Now that our executable JAR is ready and working, let’s generate a Windows executable file for our sample project:
jpackage --input . --main-jar MySampleGUIAppn.jar
This command takes a short while to complete. Once completed, it produces an exe file in the current working folder. The executable’s file name will be concatenated with the version number mentioned in the manifest file. We’ll be able to launch it just like any other Windows application.
Here are some more Windows-specific options that we can use with the jpackage command:
- –type: to specify msi instead of the default exe format
- –win-console: to start our application with a console window
- –win-shortcut: to create a short-cut file in the Windows Start menu
- –win-dir-chooser: to let an end-user specify a custom directory to install the executable
- –win-menu –win-menu-group: to let an end-user specify a custom directory in the Start menu
4. Conclusion
In this article, we learned some basics about JAR files and executable JAR files. We also saw how to convert a Java program into a JAR executable, and later into a Microsoft Windows-supported executable file.
As always, the source code for the examples is available over on GitHub.
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes: