scijava / scripting-java

JSR-223-compliant Java scripting plugin
Other
8 stars 4 forks source link

InvocationTargetException while compiling Java code in script (Windows) #2

Closed aksagar closed 10 years ago

aksagar commented 10 years ago

I was trying to compile a small Java code block in Fiji(Windows 32 bit)

import net.imagej.ImageJ;

public class UsingOps {
    public static void main(String[] args) {
        final ImageJ ij = new ImageJ();
    }
}

And I got the following error:

java.lang.UnsupportedOperationException: Cannot instantiate more than one DefaultLegacyService

The problem is that the new ImageJ() call creates a new full-blown SciJava Context with its own LegacyService, but more than one simultaneous such service is not allowed due to limitations in the design of ImageJ 1.x.

dscho commented 10 years ago

I think this will be fixed by https://github.com/scijava/scijava-common/pull/88...

dscho commented 10 years ago

javax.script.ScriptException: java.lang.reflect.InvocationTargetException

Unfortunately, this is not quite as informative as the full output... i.e. the full stacktrace. Remember, the ScriptException just wraps an InvocationTargetException which in turn wraps another exception. It is that wrapped exception that is the culprit... Now, I seem to remember that I saw on your screen that it said "URI is not hierarchical" but please include this information in bug reports: my memory is not always to be trusted, and other developers who could address the issue do not have access to my memory at all...

Now back to the problem. I think I fixed it. Could you remove the scijava-common-2.24.0.jar file from your Fiji.app/jars/ directory and place a downloaded http://jenkins.imagej.net/view/SciJava/job/SciJava-common/lastSuccessfulBuild/artifact/target/scijava-common-2.24.1-SNAPSHOT.jar into said directory instead, then try again to run the plugin in the script editor? Thank you!

aksagar commented 10 years ago

@dscho Sorry for not being detailed in the issue. I downloaded the jar file and placed it in the folder you mentioned but I am getting the same error. Here is the detailed error.

javax.script.ScriptException: java.lang.reflect.InvocationTargetException at org.scijava.plugins.scripting.java.MainJavaRunner.run(MainJavaRunner.java:68) at org.scijava.plugins.scripting.java.DefaultJavaService.run(DefaultJavaService.java:61) at org.scijava.plugins.scripting.java.JavaEngine.eval(JavaEngine.java:178) at org.scijava.script.ScriptModule.run(ScriptModule.java:172) at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167) at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126) at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65) at org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:164) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.scijava.plugins.scripting.java.MainJavaRunner.run(MainJavaRunner.java:59) ... 12 more Caused by: java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.DefaultLegacyService at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:279) at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:230) at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:193) at org.scijava.service.ServiceHelper.loadServices(ServiceHelper.java:165) at org.scijava.Context.(Context.java:234) at org.scijava.Context.(Context.java:195) at org.scijava.Context.(Context.java:138) at org.scijava.Context.(Context.java:98) at org.scijava.Context.(Context.java:86) at net.imagej.ImageJ.(ImageJ.java:82) at UsingOps.main(UsingOps.java:15) ... 17 more Caused by: java.lang.UnsupportedOperationException: Cannot instantiate more than one DefaultLegacyService at net.imagej.legacy.DefaultLegacyService.checkInstance(DefaultLegacyService.java:511) at net.imagej.legacy.DefaultLegacyService.initialize(DefaultLegacyService.java:329) at org.scijava.service.ServiceHelper.createServiceRecursively(ServiceHelper.java:339) at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:268) ... 27 more Caused by: java.lang.Throwable: Initialized here: at net.imagej.legacy.DefaultLegacyService.initialize(DefaultLegacyService.java:345) at org.scijava.service.ServiceHelper.createServiceRecursively(ServiceHelper.java:339) at org.scijava.service.ServiceHelper.createExactService(ServiceHelper.java:268) at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:230) at org.scijava.service.ServiceHelper.loadService(ServiceHelper.java:193) at org.scijava.service.ServiceHelper.loadServices(ServiceHelper.java:165) at org.scijava.Context.(Context.java:234) at org.scijava.Context.(Context.java:195) at org.scijava.Context.(Context.java:138) at org.scijava.Context.(Context.java:98) at org.scijava.Context.(Context.java:86) at net.imagej.ImageJ.(ImageJ.java:82) at net.imagej.Main.launch(Main.java:61) at net.imagej.Main.main(Main.java:73) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:258) at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:184) at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:76) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at fiji.Main.main(Main.java:174) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:258) at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:184) at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:76)

ctrueden commented 10 years ago

Thanks @aksagar. The problem is not specific to Windows; it happens on OS X too, and very likely also on Linux. I updated the description above to reflect the gist of the problem.

You can work around it by using the following block of code instead:

import net.imagej.ImageJ;
import net.imagej.ops.Op;

import org.scijava.command.Command;
import org.scijava.plugin.Parameter;

public class UsingOps implements Command {

    @Parameter
    private ImageJ ij;

    public void run() {
        ij.ui().showDialog("Hello world");
        // do more stuff with ij...
    }
}

@dscho: I'm sort of amazed that this is the very first time anyone has tried constructing a new SciJava context in the Script Editor. And I'm not sure of the best way to fix it, or if we even should do so...

dscho commented 10 years ago

I do not think that it makes sense to allow creating new contexts in the script editor. The whole point of the script editor being run in a context is to reuse it.

So I close the bug with the suggestion to use the script editor to develop Commands in the style as mentioned in the comment above.