Run a Liberty server from a JAR file

We can start a Liberty server from a Java archive (JAR) file. This method provides a compact, portable way of starting a Liberty server. We create the JAR file using the Liberty server command and then run as an executable JAR file using the java -jar command.


Create the runnable JAR file

We can specify minify to get the smallest archive possible. We must specify a JAR type archive to get a runnable JAR file.

    server package <server name> --include=[minify,]runnable --archive=<jar file name>.jar


Run the JAR file

Run the JAR file using the standard java command with the -jar option, for example:

    java -jar <jar file name>.jar


Operation

When the JAR file runs, it gets extracted to a temporary location and then the server runs in the foreground, started by the Liberty server run command. All output is written to stdout or stderr. By default, files are extracted to temporary locations:

  • For Windows: %HOMEPATH%/wlpExtract/<jar file name>_nnnnnnnnnnnnnnnnnnn
  • For all other platforms: $HOME/wlpExtract/<jar file name>_nnnnnnnnnnnnnnnnnnn

We can control the output location using the WLP_JAR_EXTRACT_ROOT or WLP_JAR_EXTRACT_DIR environment variable.


Stop the server

To stop the Liberty server press Ctrl-C. When the Liberty server stops, the extraction directory is automatically deleted. If we stop the active shell in any other way, the extraction directory is not cleaned up automatically, we must clean it up manually.


Run in debug mode

We can run the Liberty server in debug mode by setting the environment variable WLP_JAR_DEBUG before starting the server.


Control output

By default, server output is written to the extraction directory, which is deleted when the server stops. To save the output, specify a durable output location using the WLP_OUTPUT_DIR environment variable before we start the server.


Two-phase commit transactions

By default, two-phase commit commit transactions are disabled because the transaction logs are in the expansion directory and are deleted when the Liberty server is stopped. Therefore, transaction recovery is not possible.

To enable two-phase commit, configure the transaction log to be in a durable location in the file system or an RDBMS and set the WLP_JAR_ENABLE_2PC environment variable.

To configure the transaction log, use either the transactionLogDirectory or dataSourceRef attribute on the transaction element in the server.xml configuration.


Run under CYGWIN

Run a Liberty server JAR file in a CYGWIN shell has two requirements:

  1. Specify the WLP_JAR_CYGWIN environment variable.

    This variable causes the Liberty server JAR runner to do UNIX-style file and process handling when it is running in the CYGWIN environment.

  2. Run under the bash shell, not mintty.

    Automatic extraction file deletion occurs only when we run under the bash shell. We can run under mintty, but we must delete the extraction files manually. Mintty does not forward the necessary signal required to trigger Java shutdown hooks.


Environment variable reference

Environment variable Description
WLP_JAR_EXTRACT_ROOT Extracts JAR file to directory ${WLP_JAR_EXTRACT_ROOT}/<jar file name>_nnnnnnnnnnnnnnnnnnn
WLP_JAR_EXTRACT_DIR Extracts JAR file to directory ${WLP_JAR_EXTRACT_DIR}.
WLP_OUTPUT_DIR Writes Liberty server output files to directory ${WLP_OUTPUT_DIR}.
WLP_JAR_DEBUG Runs Liberty server using server debug <server name> instead of server run <server name>.
WLP_JAR_ENABLE_2PC Set to value true to enable 2PC when the runnable JAR file runs.
WLP_JAR_CYGWIN Set to value true if we are running the JAR file under CYGWIN.