I want to implement an ApplicationChangeMonitor
which monitors the filesystem for changes in the currently executed jar file. When a change is detected the application should restart. I'm using a WatchService to detect the changes.
The setup:
- Development in (Windows) Eclipse with a workspace on a samba share (Linux system)
- The jar file is generated by Eclipse maven (m2e) on that samba share
- The jar file is executed from shell on the Linux system (using openjdk)
So everytime a new jar file is created, the running application should be restarted on the Linux system. First I tried to make the application restart itself, but most of the times I ran into fatal errors from the JVM. Then I chose a simpler approach: I just made the application end itself after a change is detected and implemented the restart mechanism with bash:
while true ; do java -jar application.jar ; done
The weird thing is, I still get fatal errors once or twice after an application change. Example:
- java -jar application.jar <-- initial start, application is running
- New jar file created
- java -jar application.jar <-- fatal error
- java -jar application.jar <-- fatal error
- java -jar application.jar <-- application starts
- New jar file created
- java -jar application.jar <-- fatal error
- java -jar application.jar <-- application starts
The output:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGBUS (0x7) at pc=0x00007f46d5e2416d, pid=28351, tid=139942266005248
#
# JRE version: 7.0_25-b30
# Java VM: OpenJDK 64-Bit Server VM (23.7-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [libzip.so+0x516d] Java_java_util_zip_ZipFile_getZipMessage+0x114d
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/workspace/.../target/hs_err_pid28351.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
# http://icedtea.classpath.org/bugzilla
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
OpenJDK creates dump files and I guess the relevant part is the stack trace leading to this fatal error:
- Stack: [0x00007fbc9398f000,0x00007fbc93a90000], sp=0x00007fbc93a8bd90, free space=1011k
- Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
- C [libzip.so+0x516d] Java_java_util_zip_ZipFile_getZipMessage+0x114d
- C [libzip.so+0x5eb0] ZIP_GetEntry+0xd0
- C [libzip.so+0x3af3] Java_java_util_zip_ZipFile_getEntry+0xb3
- j java.util.zip.ZipFile.getEntry(J[BZ)J+0
- j java.util.zip.ZipFile.getEntry(Ljava/lang/String;)Ljava/util/zip/ZipEntry;+38
- j java.util.jar.JarFile.getEntry(Ljava/lang/String;)Ljava/util/zip/ZipEntry;+2
- j java.util.jar.JarFile.getJarEntry(Ljava/lang/String;)Ljava/util/jar/JarEntry;+2
- j sun.misc.URLClassPath$JarLoader.getResource(Ljava/lang/String;Z)Lsun/misc/Resource;+48
- j sun.misc.URLClassPath.getResource(Ljava/lang/String;Z)Lsun/misc/Resource;+53
- j java.net.URLClassLoader$1.run()Ljava/lang/Class;+26
- j java.net.URLClassLoader$1.run()Ljava/lang/Object;+1
- ...
Now, does anyone have any idea why I get these fatal errors? I thought maybe it's because the jar file hasn't been written completely (that would explain why the problem comes from Java_java_util_zip_ZipFile_getZipMessage
). But that's simply not the case because the md5sum of the jar stays the same after executions resulting in fatal errors and working executions.
while true; do md5sum application.jar ; java -jar application.jar ; done