How to do it...

  1. Building a default self-executing JAR file is very easy; actually, it is done automatically once we execute the ./gradlew clean bootJar command.
  2. We can proceed to launch the created application simply by invoking ./build/libs/bookpub-0.0.1-SNAPSHOT.jar.
  1. In an enterprise environment, it is rare that we are satisfied with the default JVM launch arguments as we often need to tweak the memory settings, GC configurations, and even pass the startup system properties in order to ensure that we are using the desired version of the XML parser or a proprietary implementation of class loader or security manager. To accomplish those needs, we will modify the default launch.script file to add support for the JVM options. Let's start by copying the default launch.script file from the https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script Spring Boot GitHub repository in the root of our project.

The launch.script file is supported only on Linux and OS X environments. If you are looking to make self-executing JARs for Windows, you will need to provide your own launch.script file that is tailored for the Windows shell command execution. The good news is that it is the only special thing that is required; all the instructions and concepts in this recipe will work just fine on Windows as well, provided that the compliant launch.script template is being used.

  1. We will modify the copied launch.script file and add the following content right above the line 142 mark (this is showing only the relevant part of the script so as to condense the space):
...
# Find Java
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
javaexe="$JAVA_HOME/bin/java"
elif type -p java 2>&1> /dev/null; then
javaexe=java
elif [[ -x "/usr/bin/java" ]]; then
javaexe="/usr/bin/java"
else
echo "Unable to find Java"
exit 1
fi
# Configure JVM Options
jvmopts="{{jvm_options:}}"
arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $jvmopts $JAVA_OPTS -jar $jarfile $RUN_ARGS "$@")
# Action functions
start() {
...
  1. With the custom launch.script file in place, we will need to add the options setting to our build.gradle file with the following content:
applicationDefaultJvmArgs = [ 
    "-Xms128m", 
    "-Xmx256m" 
] 
 
bootJar { 
    classifier = 'exec' 
    baseName = 'bookpub' 
    launchScript { 
        script = file('launch.script') 
        properties 'jvm_options' : applicationDefaultJvmArgs.join(' ') 
    } 
} 
  1. We are now ready to launch our application. First, let's use the ./gradlew clean bootRun command, and if we look at the JConsole VM Summary tab, we will see that our arguments indeed have been passed to the JVM, as follows:
  2. We can also build the self-starting executable JAR by running the ./gradlew clean bootJar command and then executing ./build/libs/bookpub-0.0.1-SNAPSHOT-exec.jar in order to launch our application. We should expect to see a similar result in JConsole.
  1. Alternatively, we can also use the JAVA_OPTS environment variable to override some of the JVM arguments. Say we want to change the minimum memory heap size to 128 megabytes. We can launch our application using the JAVA_OPTS=-Xmx128m ./build/libs/bookpub-0.0.1-SNAPSHOT-exec.jar command and this would show us the following effect in JConsole:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.12.172