title: IllegalAccessError when using a public method reference of a package-private class through a public subclass from another package
body: <p>Yesterday I faced an interesting issue after deploying my Java 8 webapp on Tomcat 8. Rather than how to solve this issue I'm more interested in understanding why that happens. But let's start from the beginning.</p>
  
  <p>I have two classes defined as follows:</p>
  
  <p><strong>Foo.java</strong></p>
  
  <pre><code>package package1;
  
  abstract class Foo {
  
      public String getFoo() {
          return "foo";
      }
  
  }
  </code></pre>
  
  <p><strong>Bar.java</strong></p>
  
  <pre><code>package package1;
  
  public class Bar extends Foo {
  
      public String getBar() {
          return "bar";
      }
  
  }
  </code></pre>
  
  <p>As you can see, they are in the same package and, ultimately, end up in the same jar, let's call it <em>commons.jar</em>. This jar is a dependency of my webapp (i.e. as been defined as dependency in my webapp's pom.xml).</p>
  
  <p>In my webapp, there is a piece of code which does:</p>
  
  <pre><code>package package2;
  
  public class Something {
  
      ...
  
      Bar[] sortedBars = bars.stream()
                             .sorted(Comparator.comparing(Bar::getBar)
                                               .thenComparing(Bar::getFoo))
                             .toArray(Bar[]::new);
  
      ...
  
  }
  </code></pre>
  
  <p>and when it is executed I get:</p>
  
  <pre><code>java.lang.IllegalAccessError: tried to access class package1.Foo from class package2.Something
  </code></pre>
  
  <p>Playing around and experimenting I was able to avoid the error in <s>three</s> two ways:</p>
  
  <ol>
  <li><p>changing the Foo class to be public instead of package-private;</p></li>
  <li><p>changing the package of the Something class to be "package1" (i.e. literally the same as the Foo and Bar classes but physically different being the Something class defined in the webapp);</p></li>
  <li><p><s>forcing the class-loading of Foo before executing the offending code:</p>
  
  <pre><code>try {
      Class&lt;?&gt; fooClass = Class.forName("package1.Foo");
  } catch (ClassNotFoundException e) { }
  </code></pre>
  
  <p></s></p></li>
  </ol>
  
  <p>Can someone give me a clear, technical explanation that justifies the issue and the above results?</p>
  
  <h1>Update 1</h1>
  
  <p>When I tried the third solution I was actually using the commons.jar of the first one (the one where the Foo class is public instead of package private). My bad sorry.</p>
  
  <p>Moreover, as pointed out in one of my comments, I tried to log the classloader of the Bar class and Something class, right before the offending code and the result for both was:</p>
  
  <pre><code>WebappClassLoader
  context: my-web-app
  delegate: false
  ----------&gt; Parent Classloader:
  java.net.URLClassLoader@681a9515
  </code></pre>
  
  <h1>Update 2</h1>
  
  <p>Ok, I finally solved one of the mysteries!</p>
  
  <p>In one of my comments I said that I wasn't able to replicate the problem by executing the offending code from a simple main created in a different package than Foo and Bar of the <em>commons.jar</em>. Well...Eclipse (4.5.2) and Maven (3.3.3) fooled me here!</p>
  
  <p>With this simple pom:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;my.test&lt;/groupId&gt;
      &lt;artifactId&gt;commons&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <ol>
  <li><p>if I execute "mvn clean package" (as Eclipse Run Configuration) and run the main from within Eclipse I get the wonderful IllegalAccessError (cool!);</p></li>
  <li><p>if I execute Maven -> Update project... and run the main from within Eclipse I don't get any error (not cool!).</p></li>
  </ol>
  
  <p>So I switched to the command-line and I confirmed the first option: the error consistently appears regardless by whether the offending code is in the webapp or in the jar. Nice!</p>
  
  <p>Then, I was able to further simplify the Something class and discovered something interesting:</p>
  
  <pre><code>package package2;
  
  import java.util.stream.Stream;
  import package1.Bar;
  
  public class Something {
  
      public static void main(String[] args) {
  
          System.out.println(new Bar().getFoo());
          // "foo"
  
          Stream.of(new Bar()).map(Bar::getFoo).forEach(System.out::println);
          // IllegalAccessError
  
      }
  
  }
  </code></pre>
  
  <p>I'm about to be blasphemous here so bear with me: could it be that the Bar::getFoo method reference simply get "resolved" to the Foo::getFoo method reference and, since the Foo class is not visible in Something (being Foo package private), the IllegalAccessError is thrown?</p>