mobie / mobie-viewer-fiji

BSD 2-Clause "Simplified" License
33 stars 13 forks source link

Cannot run project on all computers, probably due to LegacyInjector issues #959

Open tischi opened 1 year ago

tischi commented 1 year ago

@nicoKiaru @tibuch @imagejan

Could you please do me a favour?

  1. Clone this repo
  2. git checkout develop
  3. Within your IDE, run: java.test.RunImageJ

Does it work?

maulakhan commented 1 year ago

For me it throws this:

C:\Users\me\.jdks\openjdk-17.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.3\lib\idea_rt.jar=51235:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.3\bin" -Dfile.encoding=UTF-8 @C:\Users\me\AppData\Local\Temp\idea_arg_file1611881551 RunImageJ
Exception in thread "main" java.lang.IllegalArgumentException: Invalid service: org.embl.mobie.lib.Services
    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:75)
    at RunImageJ.main(RunImageJ.java:37)
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/akhan/.m2/repository/net/imagej/ij1-patcher/1.2.2/ij1-patcher-1.2.2.jar

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

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

    at net.imagej.patcher.CodeHacker.javaAgentHint(CodeHacker.java:826)
    at net.imagej.patcher.LegacyInjector.alreadyPatched(LegacyInjector.java:433)
    at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:380)
    at net.imagej.patcher.LegacyInjector.preinit(LegacyInjector.java:376)
    at org.embl.mobie.lib.Services.<clinit>(Services.java:13)
    ... 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:424)
    ... 21 more

Process finished with exit code 1
tischi commented 1 year ago

ah: openjdk-17.0.1 maybe too new?

tibuch commented 1 year ago

Works for me.

imagejan commented 1 year ago

Works for me in Eclipse (Java 11) as well.

However, Eclipse reported an error in one file, fixable by this patch:

diff --git forkSrcPrefix/src/main/java/org/embl/mobie/lib/source/RandomAccessibleIntervalMipmapSource.java forkDstPrefix/src/main/java/org/embl/mobie/lib/source/RandomAccessibleIntervalMipmapSource.java
index b0dd278f22f0739002084ba3a9d07f7cfad9435b..1a13306fe861bbafe75e3379b556a9bc5e3ee787 100644
--- forkSrcPrefix/src/main/java/org/embl/mobie/lib/source/RandomAccessibleIntervalMipmapSource.java
+++ forkDstPrefix/src/main/java/org/embl/mobie/lib/source/RandomAccessibleIntervalMipmapSource.java
@@ -92,7 +92,7 @@ public class RandomAccessibleIntervalMipmapSource< T extends Type< T > > impleme
        {
            final T outOfBoundsVariable = type.createVariable();
            final RandomAccessible ra = new ExtendedRandomAccessibleInterval<>( getSource( t, level ), new OutOfBoundsConstantValueFactory<>( outOfBoundsVariable ) );
-           return Views.interpolate( ra, new NearestNeighborInterpolatorFactory<>() );
+           return Views.interpolate( ra, new NearestNeighborInterpolatorFactory< T >() );
        }
    }
tischi commented 1 year ago

We figured it out; there were a couple of issues (@maulakhan will write a summary later).

maulakhan commented 1 year ago

First of all, we changed the SDK in Project Settings :

image

It would still throw error:

Error running org.embl.mobie.cmd.CommandLineWildcardDarioArif. Command line is too long. Shorten the command line via JAR manifest or via a classpath file and rerun.

And then also changing the Edit Configuration:

image

And then it works fine

imagejan commented 1 year ago

The IDE-specific project settings are not (and should not be) part of the source code managed in this repository. All necessary project metadata are defined in pom.xml, including (via its parent pom-scijava) the Java run configuration. Importing as a fresh Maven project should usually be fine, or maybe resetting/deleting the IDE-specific project metadata (e.g. in .idea) and re-importing... had you tried this?

tischi commented 1 year ago

@imagejan is the java JDK version specified in the pom?!

imagejan commented 1 year ago

@tischi yes, here in pom-scijava-base: https://github.com/scijava/pom-scijava-base/blob/588e01459aa9a07b20e37b587e743fd32fcf095c/pom.xml#L170-L178

You can override it by setting scijava.jvm.version in your own pom.xml's <properties>.

tischi commented 1 year ago

Do you know how to make IntelliJ respect this? Mine isn't....

imagejan commented 1 year ago

Sorry, I don't know IntelliJ very well, but maybe this answer on SO helps: https://stackoverflow.com/a/59885408/1919049

Otherwise, maybe ask on the forum and tag other developers using IntelliJ, then you'll get more robust advice.