ImageJ legacy injector error when running Tests

@ctrueden @imagejan @hinerm

I am getting errors when running Tests in certain of my repositories, both from within IntelliJ and also using mvn clean install.

For example here

I would be greatful for any tips that help to solve this issue. It could well be that I am doing the whole testing setup entirely wrong, but it worked for me so far…
I have the feeling that with scijava 30.0.0 tests started to fail that used to work, but I am not sure.

Error running tests within IntelliJ

log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.LegacyService

	at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:278)
	at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:229)
	at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:192)
	at org.scijava.service.ServiceHelper.loadServices(ServiceHelper.java:164)
	at org.scijava.Context.<init>(Context.java:285)
	at org.scijava.Context.<init>(Context.java:234)
	at org.scijava.Context.<init>(Context.java:174)
	at org.scijava.Context.<init>(Context.java:160)
	at net.imagej.ImageJ.<init>(ImageJ.java:77)
	at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:64)
	at test.TestFlyEmbryoRegistration.test0(TestFlyEmbryoRegistration.java:52)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
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:300)
	at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:267)
	... 35 more
Caused by: java.lang.RuntimeException: Found incompatible ij.IJ class
	at net.imagej.patcher.LegacyEnvironment.initialize(LegacyEnvironment.java:112)
	at net.imagej.patcher.LegacyEnvironment.applyPatches(LegacyEnvironment.java:494)
	at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:395)
	at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:374)
	at net.imagej.legacy.LegacyService.<clinit>(LegacyService.java:141)
	... 42 more
Caused by: java.lang.IllegalArgumentException: Cannot modify method: public void run(java.lang.String arg)
	at net.imagej.patcher.CodeHacker.insertAtTopOfMethod(CodeHacker.java:166)
	at net.imagej.patcher.LegacyInjector.inject(LegacyInjector.java:288)
	at net.imagej.patcher.LegacyInjector.injectHooks(LegacyInjector.java:107)
	at net.imagej.patcher.LegacyEnvironment.initialize(LegacyEnvironment.java:100)
	... 46 more
Caused by: java.lang.IllegalArgumentException: No such class: MacAdapter
	at net.imagej.patcher.CodeHacker.getClass(CodeHacker.java:878)
	at net.imagej.patcher.CodeHacker.getMethod(CodeHacker.java:904)
	at net.imagej.patcher.CodeHacker.getBehavior(CodeHacker.java:894)
	at net.imagej.patcher.CodeHacker.insertAtTopOfMethod(CodeHacker.java:157)
	... 49 more
Caused by: javassist.NotFoundException: MacAdapter
	at javassist.ClassPool.get(ClassPool.java:430)
	at net.imagej.patcher.CodeHacker.getClass(CodeHacker.java:873)
	... 52 more


java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.LegacyService

	at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:278)
	at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:229)
	at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:192)
	at org.scijava.service.ServiceHelper.loadServices(ServiceHelper.java:164)
	at org.scijava.Context.<init>(Context.java:285)
	at org.scijava.Context.<init>(Context.java:234)
	at org.scijava.Context.<init>(Context.java:174)
	at org.scijava.Context.<init>(Context.java:160)
	at net.imagej.ImageJ.<init>(ImageJ.java:77)
	at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:64)
	at test.TestFlyEmbryoRegistration.test1(TestFlyEmbryoRegistration.java:59)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class net.imagej.legacy.LegacyService
	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:300)
	at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:267)
	... 35 more

Error running tests with maven

mvn clean install

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running test.TestFlyEmbryoRegistration
[ECLIPSE HELPER] Indexing annotations...
log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 2.187 s <<< FAILURE! - in test.TestFlyEmbryoRegistration
[ERROR] test0(test.TestFlyEmbryoRegistration)  Time elapsed: 1.969 s  <<< ERROR!
java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.LegacyService
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test0(TestFlyEmbryoRegistration.java:52)
Caused by: java.lang.ExceptionInInitializerError
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test0(TestFlyEmbryoRegistration.java:52)
Caused by: java.lang.RuntimeException: Found incompatible ij.IJ class
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test0(TestFlyEmbryoRegistration.java:52)
Caused by: java.lang.IllegalArgumentException: Cannot modify method: public void run(java.lang.String arg)
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test0(TestFlyEmbryoRegistration.java:52)
Caused by: java.lang.IllegalArgumentException: No such class: MacAdapter
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test0(TestFlyEmbryoRegistration.java:52)
Caused by: javassist.NotFoundException: MacAdapter
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test0(TestFlyEmbryoRegistration.java:52)

[ERROR] test1(test.TestFlyEmbryoRegistration)  Time elapsed: 0.105 s  <<< ERROR!
java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.LegacyService
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test1(TestFlyEmbryoRegistration.java:58)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class net.imagej.legacy.LegacyService
        at test.TestFlyEmbryoRegistration.runRegistrationTest(TestFlyEmbryoRegistration.java:63)
        at test.TestFlyEmbryoRegistration.test1(TestFlyEmbryoRegistration.java:58)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   TestFlyEmbryoRegistration.test0:52->runRegistrationTest:63 ? IllegalArgument I...
[ERROR]   TestFlyEmbryoRegistration.test1:58->runRegistrationTest:63 ? IllegalArgument I...
[INFO] 
[ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0
1 Like

Indeed, pom-scijava-30.0.0 still pins to ij1-patcher-1.2.0 which does not yet include these adaptations to a breaking change in ImageJ1:


Nevertheless, I was successfully building projects with parent pom-scijava-30.0.0 on Mac OSX, so I suspect there might also be a race condition under the following conditions:

  • you use LegacyService and IJ1-related classes, and
  • you run multiple tests.

The order of tests being run might differ between machines, and the hacky ImageJ1 patching might not have been performed yet.

For this reason, every test in the imagej-legacy component contains this static call to LegacyInjector.preinit():

You might want to try if this helps in your case as well…

(I’m tired of seeing these IJ1 patching-related problems by the way…)

3 Likes

Did not help unfortunately.

@imagejan I found back an own observation: Imagej-legacy error - #5 by Christian_Tischer Does that maybe help to track down the issue?

It is only crashing within the @Test context.

Running the exact same code like this is fine:

	public static void main( String[] args )
	{
		final TestFlyEmbryoRegistration testFlyEmbryoRegistration = new TestFlyEmbryoRegistration();
		testFlyEmbryoRegistration.test0();
		testFlyEmbryoRegistration.test1();
	}