Skip to content

Instantly share code, notes, and snippets.

@suztomo
Last active September 16, 2020 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suztomo/d8a4bcadb06ac22393321f9d82f05e6c to your computer and use it in GitHub Desktop.
Save suztomo/d8a4bcadb06ac22393321f9d82f05e6c to your computer and use it in GitHub Desktop.
multianewarray throws IllegalAccessError

foo/B.java

package foo;

// We change this modifier to non-public.
public class B {

}

bar/C.java

package bar;

import foo.B;

public class C {
  public static void main(String[] arguments) {
    B[][] a = new B[3][3];
    System.out.println(a);
  }
}
suztomo-macbookpro44% java -cp  target/classes:b-public.jar bar.C                 
Exception in thread "main" java.lang.IllegalAccessError: tried to access class foo.B from class bar.C
	at bar.C.main(C.java:26)
suztomo-macbookpro44% java -cp  target/classes:b-public.jar bar.C                 
Exception in thread "main" java.lang.IllegalAccessError: tried to access class foo.B from class bar.C
	at bar.C.main(C.java:26)
suztomo-macbookpro44% javap -verbose -cp b-public.jar bar.C      
Classfile jar:file:/Users/suztomo/wsgenoptions/b-public.jar!/bar/C.class
  Last modified Sep 16, 2020; size 541 bytes
  MD5 checksum 9387d78c144740aca3a20a24512662cf
  Compiled from "C.java"
public class bar.C
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #6.#22         // java/lang/Object."<init>":()V
   #2 = Class              #19            // "[[Lfoo/B;"
   #3 = Fieldref           #23.#24        // java/lang/System.out:Ljava/io/PrintStream;
   #4 = Methodref          #25.#26        // java/io/PrintStream.println:(Ljava/lang/Object;)V
   #5 = Class              #27            // bar/C
   #6 = Class              #28            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               Lbar/C;
  #14 = Utf8               main
  #15 = Utf8               ([Ljava/lang/String;)V
  #16 = Utf8               arguments
  #17 = Utf8               [Ljava/lang/String;
  #18 = Utf8               a
  #19 = Utf8               [[Lfoo/B;
  #20 = Utf8               SourceFile
  #21 = Utf8               C.java
  #22 = NameAndType        #7:#8          // "<init>":()V
  #23 = Class              #29            // java/lang/System
  #24 = NameAndType        #30:#31        // out:Ljava/io/PrintStream;
  #25 = Class              #32            // java/io/PrintStream
  #26 = NameAndType        #33:#34        // println:(Ljava/lang/Object;)V
  #27 = Utf8               bar/C
  #28 = Utf8               java/lang/Object
  #29 = Utf8               java/lang/System
  #30 = Utf8               out
  #31 = Utf8               Ljava/io/PrintStream;
  #32 = Utf8               java/io/PrintStream
  #33 = Utf8               println
  #34 = Utf8               (Ljava/lang/Object;)V
{
  public bar.C();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 24: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lbar/C;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=1
         0: iconst_3
         1: iconst_3
         2: multianewarray #2,  2             // class "[[Lfoo/B;"
         6: astore_1
         7: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
        10: aload_1
        11: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
        14: return
      LineNumberTable:
        line 26: 0
        line 27: 7
        line 28: 14
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      15     0 arguments   [Ljava/lang/String;
            7       8     1     a   [[Lfoo/B;
}
SourceFile: "C.java"

The multianewarray in 2: multianewarray #2, 2 // class "[[Lfoo/B;" throws the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment