Jar file java launcher

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:

Читайте также:  Загрузить таблицу csv python

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

Источник

joewalnes / jar-launcher.md

This is a little trick to turn an executable Java jar.

It works on all unixy like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.

Step 1 Start with an all-in-one jar (with entry point and all deps within)

$ ls hello.jar $ java -jar hello.jar Hello from Java

Step 2: Prepend launcher to beginning of jar file

Add a shell script to the header of the jar, rename to anything, and make it executable

$ (echo '#!/bin/sh' && echo 'exec java -jar $0 "$@"' && cat hello.jar) > hello $ chmod +x hello $ rm hello.jar $ ls hello 

Step 3: Deploy a single all-in-one executable launcher

Читайте также:  Python day number in year

This single file can be copied to a server. The only dependency is it expects to find a java JVM in the PATH .

This relies on a little known trick about the Zip format allowing arbitrary data to be prepended/appended around the main zip file.

  • The file looks like a shell script. Executing it will run it
  • The shell script calls exec java -jar [myfilename] [args. ] replacing the current process
  • The Java VM loads, loads the same script as a Jar and happily ignores the initial shell script because Zip allows that

Bonus: It also works with Python!

Yep. This makes it much simpler to distribute Python tools without having to worry about pip, virtual-env, or Conda. Just ship a single self-contained executable.

In Python you use .pex files, which like .jar files are just Zips. See PEP-441.

Create a Zip containing all your Python files (and dependencies). Include __main__.py as the entry point. Prepend #!/usr/bin/env python to the Zip, make executable, remove the .zip suffix. This is easy to script, but if you want something out of the box there’s an existing tool

Источник

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