Desktop Java: Self Executing Jars

xiaoxiao2021-03-06  67

Source: http://www.javalobby.com/forums/thread.jspa?threadid=15486&tstart=0

At 4:00 PM on Nov 9, 2004, RJ ​​Lorimer wrote: I've had multiple requests regarding the steps to take after writing an application - deployment One of the steps that can be difficult when building an application is building effective and usable. 'binary' formats for your application that allow it to be distributed each scenario of development has different recommended paths;. and with desktop, rich-client development especially, it is important to make users on each unique platform comfortable with installing and using your application .

So, why self executing JAR files (also referred to as executable JARs)? Well, when it comes to Java development, the cornerstone of any good installation is usually a self-executing JAR. There are steps beyond self executing JARs that I plan to Discuss In Future Entries, Butled To Master. There Are Multiple Reasons for this.

First, by making the (large) majority of the code centralized in the JAR and in the Java code, you can ensure that as much of your code as possible is cross platform. This can not be underestimated, as it minimizes the amount of parallel configuration / development Occuring for Different Platforms.

Second, JAR files are clean, easy to manage, and can be signed. Although not all that common, JAR signing is an effective step to be taken to ensure that application code has not been tampered with. The steps to take for signing JARs IS a discussion for another day.

Finally, JAR files are a good least common denominator implementation. Although not as 'familiar' on each platform as some formats (EXE, BIN, RPM, DMG), most intermediate level users should be able to figure out how to run an executable JAR file without too much trouble. For instance, on Windows (assuming the JRE is installed correctly), JAR files are executable by default simply by double clicking in Windows Explorer. The level of support varies from platform to platform and configuration to configuration, but in General, IF you can run 'java' or 'javaw', You Can Run An Executable Jar File (Simply by Typing Java (w) -jar [jar name]). Here Is A Screenshot of Me Performing A Double-Click Execution in Windows With Many People's Favorite File-Sharing Program, Azureus.azureus Jar Screenshot.

The mouse is missing, but I just finished clicking Azureus.jar, and am now being confronted with the splash screen. Notice that the JAR file is right next to an EXE file. This helps cozy up to Windows users, but realize that the EXE IS really executing The Jar File. I Will Discuss Making Executables for this purpose (among Other Things) In A Future Entry.

The first step to making executable JAR files is to understand the structure and manifest of these special JAR files An executable JAR is composed of essentially only two things:. A folder structure containing binary code and resources and a special folder called 'META-INF' With a 'manifest.mf' file contained inside.

The folder structure is relatively simple; in most cases the root of the JAR file is simply the root of the classpath for the JAR (so if your code is in a 'org.myapp ...' package structure, the root of the JAR would have an 'org' folder) .The manifest file is the more important component, and essentially informs the JVM how to handle the JAR (or more specifically, how to execute it). Each manifest file begins with a 'Manifest-Version' THE Standard for this is currently 1.0.

THE IMPORTANT Entries In The Manifest File Are 'Main-Class' And 'Class-Path', Each of Which Help You Wire your application code together Into an Executable bundle.

The 'Main-Class' attribute tells the JVM which class should be executed in the JAR file (implying the class has a 'main' method) to start your application. So, if your application had a 'org.myapp.Main' class You Would Have a Manifest File That LOOKED LIKE THIS:

Manifest-Version: 1.0

Main-class: org.myapp.main

The 'Class-Path' attribute tells the JVM about any other code outside the JAR file that should be included in the execution of the application. For instance, if your application used some code in the 'myapp-dependencies.jar', you could Place this Jar File in The Same Folder As Your 'MyApp.jar' File, And The Refer to It Like this:

Manifest-Version: 1.0

Class-path: myApp-dependencies.jar

Main-class: org.myapp.main

If you had more than one classpath entry, simply seperate them by a space. One thing to note is that MANIFEST files are very picky about formatting. In particular, they must end with a blank line and each entry in a list of elements (such AS class-path) Must Be Followed by a space.so, The Folder Structure for your Application Would Now Look Like this:

Application Folder

|

. ----- MyApp.jar (Contains Manifest.mf)

|

. ----- myApp-dependencies.jar

... and Your New Jar File Would Have this Internal Structure:

myapp.jar (root)

|

. ----- Meta-INF

| | |

| .----- manifest.mf

|

. ----- ORG

|

. ----- MyApp

|

. ----- main.class

TO Convert a Folder Structure (Like the Block Above) Into a Jar File, Either Use The Built-in Jar Tool Included with Each JDK (Documentation for Jar On Each Platform Can Be Found

Here), or Simply Use a Program Such AS

Winzip OR

WinRAR. Just Be Sure The final file is in zip format, and ends in a '.jar' extension.

Click this

Link to Download An Example 'Hello World' Jar File That Us Executable. You Can Open this Jar File Using One of the Zip Programs listed Above, and you can also try to run it.

For Further Reading On Jars, The Detailed Jar File Specification Can Be Found Here:

JAR File Specification.

转载请注明原文地址:https://www.9cbs.com/read-92784.html

New Post(0)