title: Strange Java exception java.util.zip.ZipException: error in opening zip file
body: <p>I wrote JMTI agent for get exceptions from app:</p>
  
  <pre><code>#include &lt;jvmti.h&gt;
  #include &lt;string.h&gt;
  
  void printStackTrace(JNIEnv* env, jobject exception) {
      jclass throwable_class = (*env).FindClass("java/lang/Throwable");
      jmethodID print_method = (*env).GetMethodID(throwable_class, "printStackTrace", "()V");
      (*env).CallVoidMethod(exception, print_method);
  }
  
  static void frameToString(jvmtiEnv *jvmti, jvmtiFrameInfo *finfo)
  {
      jvmtiError           error;
      jclass               klass;
      char                *signature;
      char                *methodname;
      char                *methodsig;
      jboolean             isNative;
      char                *filename;
      int                  lineCount;
      jvmtiLineNumberEntry*lineTable;
      int                  lineNumber;
  
      /* Initialize defaults */
      klass      = NULL;
      signature  = NULL;
      methodname = NULL;
      methodsig  = NULL;
      isNative   = JNI_FALSE;
      filename   = NULL;
      lineCount  = 0;
      lineTable  = NULL;
      lineNumber = 0;
  
      /* Get jclass object for the jmethodID */
      error = (*jvmti).GetMethodDeclaringClass(finfo-&gt;method, &amp;klass);
  
      /* Get the class signature */
      error = (*jvmti).GetClassSignature(klass, &amp;signature, NULL);
  
  
      /* Get the name and signature for the method */
      error = (*jvmti).GetMethodName(finfo-&gt;method, &amp;methodname, &amp;methodsig, NULL);
  
  
      /* Check to see if it's a native method, which means no lineNumber */
      error = (*jvmti).IsMethodNative(finfo-&gt;method, &amp;isNative);
  
  
      /* Get source file name */
      error = (*jvmti).GetSourceFileName(klass, &amp;filename);
      if ( error != JVMTI_ERROR_NONE &amp;&amp; error != JVMTI_ERROR_ABSENT_INFORMATION ) {
          printf("%d \n", error);
      }
  
      /* Get lineNumber if we can */
      if ( !isNative ) {
          int i;
  
          /* Get method line table */
          error = (*jvmti).GetLineNumberTable(finfo-&gt;method, &amp;lineCount, &amp;lineTable);
          if ( error == JVMTI_ERROR_NONE ) {
              /* Search for line */
              lineNumber = lineTable[0].line_number;
              for ( i = 1 ; i &lt; lineCount ; i++ ) {
                  if ( finfo-&gt;location &lt; lineTable[i].start_location ) {
                      break;
                  }
                  lineNumber = lineTable[i].line_number;
              }
          }
      }
  
      /* Create string for this frame location.
       *    NOTE: These char* quantities are mUTF (Modified UTF-8) bytes
       *          and should actually be converted to the default system
       *          character encoding. Sending them to things like
       *          printf() without converting them is actually an I18n
       *          (Internationalization) error.
       */
      printf("%s.%s@%d[%s:%d]\n", signature, methodname, (int)finfo-&gt;location, filename, lineNumber);
  
  //    /* Free up JVMTI space allocated by the above calls */
  //    deallocate(jvmti, signature);
  //    deallocate(jvmti, methodname);
  //    deallocate(jvmti, methodsig);
  //    deallocate(jvmti, filename);
  //    deallocate(jvmti, lineTable);
  }
  
  void JNICALL ExceptionCallback(jvmtiEnv* jvmti, JNIEnv* env, jthread thread,
                                 jmethodID method, jlocation location, jobject exception,
                                 jmethodID catch_method, jlocation catch_location) {
  //    int count;
  //    int line_number = 0;
  //    int i;
  //    char* class_name;
  //    char* method_name;
  
      jvmtiFrameInfo frames[10];
      jint count;
      jint frame_count;
  
  //    jclass exception_class = (*env).GetObjectClass(exception);
  //    (*jvmti).GetClassSignature(exception_class, &amp;class_name, NULL);
  //    (*jvmti).GetMethodName(method, &amp;method_name, NULL, NULL);
  //    (*jvmti).GetSourceFileName()
  //
  //    jvmtiLineNumberEntry *location_table;
  //    (*jvmti).GetLineNumberTable(method, &amp;count, &amp;location_table);
  //    for (i = 0; i &lt; count - 1; i++)
  //    {
  //        jvmtiLineNumberEntry entry1 = location_table[i];
  //        jvmtiLineNumberEntry entry2 = location_table[i+1];
  //        if (location &gt;= entry1.start_location &amp;&amp; location &lt; entry2.start_location)
  //        {
  //            line_number = entry1.line_number;
  //            break;
  //        }
  //    }
  //    if (location &gt;= location_table[count-1].start_location)
  //    {
  //        line_number = location_table[count-1].line_number;
  //    }
  //
  //    printf("%s %d \n", class_name, line_number);
  
      (*jvmti).GetStackTrace(thread, (jint)0, (jint)10, (jvmtiFrameInfo *)&amp;frames, &amp;count);
      frameToString(jvmti, frames);
      printStackTrace(env, exception);
  }
  
  JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
      jvmtiEnv* jvmti;
      jvmtiEventCallbacks callbacks;
      jvmtiCapabilities capabilities;
  
      (*vm).GetEnv((void**)&amp;jvmti, JVMTI_VERSION_1_0);
  
      memset(&amp;capabilities, 0, sizeof(capabilities));
      capabilities.can_generate_exception_events = 1;
      capabilities.can_get_line_numbers = 1;
      capabilities.can_get_source_file_name = 1;
  //    capabilities.can_suspend = 1;
      (*jvmti).AddCapabilities(&amp;capabilities);
  
      memset(&amp;callbacks, 0, sizeof(callbacks));
      callbacks.Exception = ExceptionCallback;
      (*jvmti).SetEventCallbacks(&amp;callbacks, sizeof(callbacks));
      (*jvmti).SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);
  
      return 0;
  }
  </code></pre>
  
  <p>And after test it. A have strange exceptions:</p>
  
  <pre><code>java.util.zip.ZipException: error in opening zip file
      at java.util.zip.ZipFile.open(Native Method)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:219)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:149)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:166)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:103)
      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:893)
      at sun.misc.URLClassPath$JarLoader.access$700(URLClassPath.java:756)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:838)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:831)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:830)
      at sun.misc.URLClassPath$JarLoader.&lt;init&gt;(URLClassPath.java:803)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:530)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:520)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:519)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:492)
      at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
      at sun.misc.URLClassPath.getResource(URLClassPath.java:211)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
  java.security.PrivilegedActionException: java.util.zip.ZipException: error in opening zip file
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:830)
      at sun.misc.URLClassPath$JarLoader.&lt;init&gt;(URLClassPath.java:803)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:530)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:520)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:519)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:492)
      at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
      at sun.misc.URLClassPath.getResource(URLClassPath.java:211)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
  Caused by: java.util.zip.ZipException: error in opening zip file
      at java.util.zip.ZipFile.open(Native Method)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:219)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:149)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:166)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:103)
      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:893)
      at sun.misc.URLClassPath$JarLoader.access$700(URLClassPath.java:756)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:838)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:831)
      ... 20 more
  java.util.zip.ZipException: error in opening zip file
      at java.util.zip.ZipFile.open(Native Method)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:219)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:149)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:166)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:103)
      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:893)
      at sun.misc.URLClassPath$JarLoader.access$700(URLClassPath.java:756)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:838)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:831)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:830)
      at sun.misc.URLClassPath$JarLoader.&lt;init&gt;(URLClassPath.java:803)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:530)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:520)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:519)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:492)
      at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
      at sun.misc.URLClassPath.getResource(URLClassPath.java:211)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
  java.security.PrivilegedActionException: java.util.zip.ZipException: error in opening zip file
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:519)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:492)
      at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
      at sun.misc.URLClassPath.getResource(URLClassPath.java:211)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
  Caused by: java.util.zip.ZipException: error in opening zip file
      at java.util.zip.ZipFile.open(Native Method)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:219)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:149)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:166)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:103)
      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:893)
      at sun.misc.URLClassPath$JarLoader.access$700(URLClassPath.java:756)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:838)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:831)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:830)
      at sun.misc.URLClassPath$JarLoader.&lt;init&gt;(URLClassPath.java:803)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:530)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:520)
      ... 15 more
  java.util.zip.ZipException: error in opening zip file
      at java.util.zip.ZipFile.open(Native Method)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:219)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:149)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:166)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:103)
      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:893)
      at sun.misc.URLClassPath$JarLoader.access$700(URLClassPath.java:756)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:838)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:831)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:830)
      at sun.misc.URLClassPath$JarLoader.&lt;init&gt;(URLClassPath.java:803)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:530)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:520)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:519)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:492)
      at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
      at sun.misc.URLClassPath.getResource(URLClassPath.java:211)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
  java.util.zip.ZipException: error in opening zip file
      at java.util.zip.ZipFile.open(Native Method)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:219)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:149)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:166)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:103)
      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:893)
      at sun.misc.URLClassPath$JarLoader.access$700(URLClassPath.java:756)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:838)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:831)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:830)
      at sun.misc.URLClassPath$JarLoader.&lt;init&gt;(URLClassPath.java:803)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:530)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:520)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:519)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:492)
      at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
      at sun.misc.URLClassPath.getResource(URLClassPath.java:211)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
  java.security.PrivilegedActionException: java.util.zip.ZipException: error in opening zip file
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:830)
      at sun.misc.URLClassPath$JarLoader.&lt;init&gt;(URLClassPath.java:803)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:530)
      at sun.misc.URLClassPath$3.run(URLClassPath.java:520)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:519)
      at sun.misc.URLClassPath.getLoader(URLClassPath.java:492)
      at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
      at sun.misc.URLClassPath.getResource(URLClassPath.java:211)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
  Caused by: java.util.zip.ZipException: error in opening zip file
      at java.util.zip.ZipFile.open(Native Method)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:219)
      at java.util.zip.ZipFile.&lt;init&gt;(ZipFile.java:149)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:166)
      at java.util.jar.JarFile.&lt;init&gt;(JarFile.java:103)
      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:893)
      at sun.misc.URLClassPath$JarLoader.access$700(URLClassPath.java:756)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:838)
      at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:831)
      ... 20 more
  </code></pre>
  
  <p>Why this exception? How to solve it?</p>