imagej2 icon indicating copy to clipboard operation
imagej2 copied to clipboard

Exception in thread "main" java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.LegacyService

Open workhours opened this issue 3 years ago • 2 comments

clone this repo to local directory by git-gui, configure project according to "https://imagej.net/develop/intellij". Run/Debug "ImageJ2" shows following exceptions: Exception in thread "main" 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.(Context.java:285) at org.scijava.Context.(Context.java:234) at org.scijava.Context.(Context.java:174) at org.scijava.Context.(Context.java:160) at net.imagej.ImageJ.(ImageJ.java:77) at net.imagej.Main.main(Main.java:54) Caused by: java.lang.ExceptionInInitializerError at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128) at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:347) at java.base/java.lang.Class.newInstance(Class.java:645) at org.scijava.service.ServiceHelper.createServiceRecursively(ServiceHelper.java:300) at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:267) ... 9 more Caused by: java.lang.RuntimeException: No _hooks field found in ij.IJ 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:/C:/Users/XXX/.m2/repository/net/imagej/ij1-patcher/1.2.1/ij1-patcher-1.2.1.jar

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

-javaagent:/C:/Users/XXX/.m2/repository/net/imagej/ij1-patcher/1.2.1/ij1-patcher-1.2.1.jar=init

at net.imagej.patcher.CodeHacker.javaAgentHint(CodeHacker.java:826)
at net.imagej.patcher.LegacyInjector.alreadyPatched(LegacyInjector.java:431)
at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:378)
at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:374)
at net.imagej.legacy.LegacyService.<clinit>(LegacyService.java:141)
... 18 more

Caused by: java.lang.NoSuchFieldException: _hooks at java.base/java.lang.Class.getField(Class.java:2117) at net.imagej.patcher.LegacyInjector.alreadyPatched(LegacyInjector.java:422) ... 21 more

Test environment: OS: windows10 JDK: Oracle JDK 17.0.2 Intellj IDEA: 2021.3.1

workhours avatar Jan 22 '22 15:01 workhours

debugging by step, it seems: findLoadedClass.setAccessible(true); thrown exception at net.imagej.patcher.LegacyInjector at line 405, don't know if this exception is expected

workhours avatar Jan 22 '22 16:01 workhours

Did you build ImageJ2 with JDK bundled with it? In my case, the error occurs when using java 17 (SDK of 'imagej' module) in Run/Debug Configurations.

Suikaba avatar Mar 01 '22 12:03 Suikaba