Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.2K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 437 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Lambda Expression throws IllegalAccessError

Hi,
I have the following structure and want to use lambda expressions with it, but an exception occurs:
class SuperClass // (package private)
public class SubClass extends SuperClass // in same package as SuperClass
I have a functional interface (a listener), which has one argument of type SubClass.
public interface MyListener() { void myMethod(SubClass e); }
When I use the old-school Java 7 style:
addListener(new MyListener() { @Override public void myMethod(SubClass e) { System.out.println(e); } });
everything works fine!
If I use Java 8 lambda expression instead:
addListener(e -> { System.out.println(e); });
it will throw an exception:
java.lang.IllegalAccessError: tried to access class SuperClass
I wonder if this is a documented shortcoming of Java 8 lambda expressions or a bug?
Can somebody help here?
Answers
-
Is the SuperClass in the default package? Classes from the default package do not get imported. The error should also be in Java 7 if the same package structure is used.
-
No, it's in a "real" package, like "com.example". Java 7 does not support Lambdas.
-
I tried this, or something similar (under 1.8.0u40 ea) and it worked fine.
My code:
SuperClass.java:
package pri; class SuperClass { }
SubClass.java:
package pri; public class SubClass extends SuperClass { }
MyLIstener.java (different package):
package pub; import pri.SubClass; @FunctionalInterface public interface MyListener { void myMethod(SubClass e) ; }
Test.java (another package still):
package test; import pri.SubClass; import pub.MyListener; public class Test { public static void main(String[] args) { MyListener l = new MyListener() { @Override public void myMethod(SubClass e) { System.out.println(e); } }; MyListener l2 = e -> System.out.println(e); MyListener l3 = System.out::println ; l.myMethod(new SubClass()); l2.myMethod(new SubClass()); l3.myMethod(new SubClass()); } }
-
Sorry, it turned out, that the problem only exists, when there are generics involved (i.e. class SuperClass<T>). I've got an example at home, if you are interested.
In the meanwhile I met an Oracle guy (at Devoxx), who said, it looks like a bug in Java 8. I don't know where it's tracked.