Xp.context.get() in a service gives cryptic error message

Since services does not have a context running xp.context.get() fails.

Perhaps there could be a better error message than this:

java.lang.NoClassDefFoundError: com/enonic/xp/lib/auth/PrincipalMapper

I think this is a bug with the building of lib-context in XP. It depends on a class that is in lib-auth.
It will probably work if you add lib-auth dependency in your build.gradle, even if you don’t use it. Can you check?

include "com.enonic.xp:lib-auth:${xpVersion}"

Services should have a context, the context.get() function should work from a service.

I will check. I get the same message even when I’m using run in context.

That fixed when using run in context. Checking without context now.

Yup that fixed it for normal service without using run in context too.

Ok, thanks. I will register this as a bug. It should not be necessary to include lib-auth for using lib-context, but that is the workaround for now.

2 Likes