Imagej-legacy error

Hi ImageJ Community,

After the great ImgLib2 introduction from @haesleinhuepf at the NEUBIAS we, @schmiedc and me, are facing a strange ImageJ1 vs ImageJ2 error.
The “group 4” created a small plugin, based on @erickratamero .py script(https://github.com/schmiedc/auto_qc)
It is working fine in Fiji if we install it via maven. But if we want to start it directly from intelliJ we get a “LegacyInjector” error:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.LegacyService
	at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:281)
	at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:232)
	at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:195)
	at org.scijava.service.ServiceHelper.loadServices(ServiceHelper.java:167)
	at org.scijava.Context.<init>(Context.java:281)
	at org.scijava.Context.<init>(Context.java:237)
	at org.scijava.Context.<init>(Context.java:177)
	at org.scijava.Context.<init>(Context.java:163)
	at net.imagej.ImageJ.<init>(ImageJ.java:77)
	at lu.neubias.imagej.autoqc.main(autoqc.java:440)
Caused by: java.lang.ExceptionInInitializerError
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at java.lang.Class.newInstance(Class.java:442)
	at org.scijava.service.ServiceHelper.createServiceRecursively(ServiceHelper.java:303)
	at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:270)
	... 9 more
Caused by: java.lang.RuntimeException: Found incompatible ij.IJ class
	at net.imagej.patcher.LegacyEnvironment.initialize(LegacyEnvironment.java:114)
	at net.imagej.patcher.LegacyEnvironment.applyPatches(LegacyEnvironment.java:496)
	at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:397)
	at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:376)
	at net.imagej.legacy.LegacyService.<clinit>(LegacyService.java:143)
	... 16 more
Caused by: java.lang.RuntimeException: Cannot load class: ij.ImagePlus (loader: sun.misc.Launcher$AppClassLoader@18b4aac2)
It appears that this class was already defined in the class loader!
Please make sure that you initialize the LegacyService before using
any ImageJ 1.x class. You can do that by adding this static initializer:

	static {
		LegacyInjector.preinit();
	}

To debug this issue, start the JVM with the option:

	-javaagent:/E:/Users/Eggord/.m2/repository/net/imagej/ij1-patcher/0.12.9/ij1-patcher-0.12.9.jar

To enforce pre-initialization, start the JVM with the option:

	-javaagent:/E:/Users/Eggord/.m2/repository/net/imagej/ij1-patcher/0.12.9/ij1-patcher-0.12.9.jar=init

	at net.imagej.patcher.CodeHacker.javaAgentHint(CodeHacker.java:828)
	at net.imagej.patcher.CodeHacker.loadClass(CodeHacker.java:807)
	at net.imagej.patcher.CodeHacker.loadClasses(CodeHacker.java:855)
	at net.imagej.patcher.LegacyInjector.injectHooks(LegacyInjector.java:116)
	at net.imagej.patcher.LegacyEnvironment.initialize(LegacyEnvironment.java:102)
	... 20 more
Caused by: java.lang.ClassFormatError: loader (instance of  sun/misc/Launcher$AppClassLoader): attempted  duplicate class definition for name: "ij/ImagePlus"
	at javassist.util.proxy.DefineClassHelper$Java7.defineClass(DefineClassHelper.java:182)
	at javassist.util.proxy.DefineClassHelper.toClass(DefineClassHelper.java:263)
	at javassist.ClassPool.toClass(ClassPool.java:1232)
	at javassist.CtClass.toClass(CtClass.java:1384)
	at net.imagej.patcher.CodeHacker.loadClass(CodeHacker.java:801)
	... 23 more

We tried the suggested

static {
		LegacyInjector.preinit();
	}

But it doesn’t work. Only if we delete the imagej-legacy from the POM file we able to start it from IntelliJ but as ImageJ1 not ImageJ2.

I hope somebody can help us to find the problem.
Maybe @haesleinhuepf has another great tip for us :wink:

Cheers,
Jan

1 Like

Is there a possibility that you have another ImageJ1 instance already running in the same JVM? As ImageJ1 is a singleton, the LegacyService can’t be initialized when there’s another instance already.

I.e. you can have multiple SciJava/ImageJ2 Contexts, but only one of them can have a functional legacy environment. Maybe that helps…

1 Like

Hey @imagejan

unfortunately, the error happens in the main() method while initializing the very first ImageJ instance.

I think the error message is related to ij.ImagePlus existing twice in different version in the class path… It may have something todo with BioFormats derived dependencies. I’m digging further… Any hints/suggestions welcome!

1 Like

Hi there,

Apologies for reviving an old topic (I would have PM’d if I could), but I’m anxious to know if there was a resolution as I am facing precisely the same issue, and there appears to be no discussion elsewhere.

Thanks.