When lambda expression is used Java actually creates an anonymous (non-static) class. Non-static inner classes always contain references their enclosing objects.
When this lambda expression is called from another library that may invoke lambda in a different process that invocation crashes with class not found exception because it cannot find enclosing object's class in another process.
Consider this example:
public class MyClass {
public void doSomething() {
remoteLambdaExecutor.executeLambda(value -> value.equals("test"));
}
}
Java will create an anonymous inner class that implements certain functional interface and pass it as a parameter to executeLambda(). Then remoteLambdaExecutor will take that anonymous class across the process to run remotely. Remote process knows nothing about MyClass and will throw
java.lang.ClassNotFoundException: MyClass
Because it needs MyClass for that enclosing object reference.
I can always use a static implementation of the functional interface expected by an API, but that defeats the purpose and doesn't utilize lambda functionality.
Is there way to solve it using lambda expressions?
UPDATE: I cannot use static class either unless it's somehow exported to that other process.