Week 30 — What is required to make a JAR file executable? (Starts on double click)
Question of the Week #30
What is required to make a JAR file executable? (Starts on double click)
7 Replies
a main method, and a manifest packaged into the jar which has a property for the Main class
usually ides or build tools can generate those manifests for you
Submission from liqui.dev#0000
create the jar file. Each jar file have a manifest file, where attributes related to the executable file are.
This is the content of my manifest file.
$cat manifest.mf
Main-class: start.HelloWorldSwing
Just describe what the main class is ( the one with the public static void main method )
Once the manifest is ready, the jar executable is invoked.
It has many options, here I'm using -c -m -f ( -c to create jar, -m to specify the manifest file , -f = the file should be named) and the folder I want to jar.
$jar -cmf manifest.mf hello.jar start
This creates the .jar file on the system
You can later just double click on that file and it will run as expected.
Submission from _sky#4725
JAR files are basically zip files containing
class
files (Java bytecode).
In order for a class to be usable, it needs to be located in the declared package in the JAR file whereas the .
character in the package name represents a subdirectory in the JAR file. For example, the class MyClass
in the package com.mypackage
would need to be located at com/mypackage/MyClass.class
in the zip file.
A class with a valid main
method can be executed using the java
command. For example, assume a JAR called myjar.jar
with the main class MyClass
as outlined before. Then, this main class can be run using the command java --class-path myjar.jar com.mypackage.MyClass
.
In order to not specify the fully qualified name of the class each time when running it, one needs to create a file called MANIFEST.MF
in the META-INF
directory of the JAR file. This file would then need to contain a Main-Class
entry referencing the class (with a main
method) to execute, for example Main-Class: com.mypackage.MyClass
.
If that entry is present in the JAR, it can be run using the command java -jar myjar.jar
.
In this case, the JAR can also be run by double-clicking it if Java is correctly registered for opening JAR files.⭐ Submission from dan1st#0000
There are about 2 things you need to make a JAR file executable:
1. A class containing a main() method.
Example:
2. A MANIFEST.MF file inside the JAR.
The Manifest file is a text file that contains information about the JAR, like who it was built by, what it was built with, what manifest version, etc, But most importantly, you can add the "Main-Class" field which tells the JRE what class the main() method is in.
Example of what it looks like inside a manifest file:
⭐ Submission from geuxy#0000
For a JAR file to be executable, it must contain at least one class with an executable main method. This class should also be listed as the main class in the manifest file.
Submission from stormofgalaxy#0000
When java wants to launch a jar, it looks for a
META-INF/MANIFEST.MF
file in the jar. If it is found, it is read, and the Main-Class
attribute in it is stored. The attribute specifies which class should be considered the "main" class of the jar, containing the static void main(String[])
method. The class is loaded from the jar, and the main method is executed. If the class doesn't exist, or is malformed, or the version is not supported, an error is thrown.
example/Hello.class
: Compiled from
META-INF/MANIFEST.MF
:
Packaging those 2 files in a .zip, then renaming it to .jar will create an executable jar.⭐ Submission from 0x150#0000