Error in executing a java script

Hi all,
I have a java script (to invert pixel values) in the Scripts sub-folder of plugins folder of Fiji. As I tried to run it from script editor I get the following error:

Started My_Inverter_A.java at Wed Apr 27 10:55:33 CEST 2016
Compiling 1 file in C:\Users\sislam\AppData\Local\Temp\java1912153006258625453
C:\Users\sislam\AppData\Local\Temp\java1912153006258625453\src\main\java\My_Inverter_A.java:10: cannot access java.lang.Object
bad class file: ZipFileIndexFileObject[C:\Users\sislam\DOWNLO~1\FIJI-W~1\Fiji.app\java\win64\jdk1.8.0_66\jre\lib\rt.jar(java/lang/Object.class)]
class file has wrong version 52.0, should be 50.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
public class My_Inverter_A implements PlugInFilter {
       ^
org.scijava.minimaven.JavaCompiler$CompileError: Compile error: 1
    at org.scijava.minimaven.JavaCompiler.call(JavaCompiler.java:105)

Could you please help. Thanks.

Regards,
Shari

1 Like

This is a known issue when running with Java 8 and trying to compile a Java plugin from the script editor. See:

Possible solutions:

  • Instead of writing Java code, use one of the scripting languages, e.g. Javascript, that works well from the script editor.
  • If you really want to develop a Java plugin, use a proper IDE, like e.g. Eclipse (which will provide many more advantages like code auto-completion), and compile the plugin using Maven.
  • Fall back to running Fiji with Java 6 instead of Java 8 (see FAQ)
3 Likes

If you use the Java-8 update site, and delete the jars/tools-1.4.2.jar and jars/javac-1.6.0.24-ubuntu-fiji2.jar files, then restart Fiji, you should be able to compile Java code in the Script Editor again. There is a technical limitation in the ImageJ Updater preventing us from removing these files right now, but we will be able to do it as part of the migration of the Java-8 content back to the core ImageJ and Fiji update sites, later this spring.

That said, I second @imagejan’s advice to either A) use a script language (easier to learn and prototype); and/or B) use an IDE like Eclipse or NetBeans (super powerful).

3 Likes

Thank you very much @imagejan and @ctrueden. I will follow your suggestions.

Hi @imagejan, Hi @ctrueden,
I installed Eclipse and then configured it for Fiji. Then as I import a local java project (called ‘imagingbook_plugins_en2’, which uses Fiji), I get many errors. It looks like the codes in ‘imagingbook_plugins_en2’ does not recognize Fiji. Please see the attached screenshot.

Thank you again for your kind support.

Best,
Shari

If you imported the project as a plain Java project, Eclipse will not know about its dependencies.

That’s where Maven makes your life much easier: from the source repository, I see that imagingbook_plugins_en2 is a Maven project (because it contains a pom.xml file), so simply import the project via File > Import… > Maven/Existing Maven Projects, and it should work.

If that doesn’t work, try building the project from the command line (just by calling mvn in the project folder) and see if it gives errors.

Thanks for youre reply @imagejan. I exactly did that. But I am having this problem!

I see, it seems like this project depends on a parent POM that cannot be resolved:

In imagingbook_plugins_en2, you find:

<parent>
	<groupId>com.imagingbook</groupId>
	<artifactId>imagingbook-public</artifactId>
	<version>1.2-SNAPSHOT</version>
</parent>

and in imagingbook-public:

<parent>
	<groupId>com.imagingbook</groupId>
	<artifactId>imagingbook-all</artifactId>
	<version>FIXED</version>
</parent>

I couldn’t find the imagingbook-all artefact anywhere online. Maybe the original author (@wilbur) can help here?

Thank you very much @imagejan. @wilbur please help!

@hello_fiji as a workaround, you might try to change the parent pom to:

<parent>
	<groupId>net.imagej</groupId>
	<artifactId>pom-imagej</artifactId>
	<version>15.5.0</version>
	<relativePath />
</parent>

See also the minimal-ij1-plugin project.

Hi @imagejan,
Thanks for your suggestion. I tried it…but it does not work! :sweat:

Hi @imagejan,
I tried to bulid the project by using maven build and got the following error. Please see below:

[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.imagingbook:imagingbook-public:1.2-SNAPSHOT: Failure to find com.imagingbook:imagingbook-all:pom:FIXED in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ com.imagingbook:imagingbook-public:1.2-SNAPSHOT, C:\Users\sislam\Downloads\imagingbook\mshariful-imagingbook-5bf255198bc4\pom.xml, line 6, column 10
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.imagingbook:imagingbook_plugins_en2:[unknown-version] (C:\Users\sislam\Downloads\imagingbook\mshariful-imagingbook-5bf255198bc4\imagingbook-plugins-en2\pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for com.imagingbook:imagingbook-public:1.2-SNAPSHOT: Failure to find com.imagingbook:imagingbook-all:pom:FIXED in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ com.imagingbook:imagingbook-public:1.2-SNAPSHOT, C:\Users\sislam\Downloads\imagingbook\mshariful-imagingbook-5bf255198bc4\pom.xml, line 6, column 10 -> [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

This is a multi-module project and since none of the artifacts seem to be deployed to remote maven repositories, you will need to build the whole thing yourself (instead of picking individual components). This means you want to check out the top-level project and build from that directory.

The following modifications to the top-level pom.xml are necessary to build:

  • Use pom-imagej parent (since imagingbook-all is not publicly available, and we need dependency management for the net.imagej:ij artifact)
+    <groupId>com.imagingbook</groupId>
     <artifactId>imagingbook-public</artifactId>
     <!-- https://issues.apache.org/jira/browse/MNG-624 -->
     <packaging>pom</packaging>
     <parent>
-        <groupId>com.imagingbook</groupId>
-        <artifactId>imagingbook-all</artifactId>
-        <version>FIXED</version>
+        <groupId>net.imagej</groupId>
+        <artifactId>pom-imagej</artifactId>
+        <version>15.5.0</version>
     </parent>
  • Add dependency management for imagingbook-common so all the plugin modules use a consistent version of the dependency:
+  <dependencyManagement>
+    <dependencies>
+        <dependency>
+            <groupId>com.imagingbook</groupId>
+            <artifactId>imagingbook-common</artifactId>
+            <version>1.2-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
+  </dependencyManagement>
+
  • Add the maven.imagej.net so we can fetch ImageJ maven artifacts:
+
+  <repositories>
+    <!-- NB: for project parent -->
+    <repository>
+      <id>imagej.public</id>
+      <url>http://maven.imagej.net/content/groups/public</url>
+    </repository>
+  </repositories>
+

These changes should allow you to build the plugins locally.

@wilbur I encourage you to adapt these to fit your project. As-is I would still say they are insufficient, as the version linking needs to be easier to manage (and should use non-SNAPSHOT dependencies). See trakem2 for an example of how multi-module plugins can be structured. If you have any questions, let us know.

1 Like

FWIW, personally I would recommend using multi-module builds only when all versions are supposed to be in lock-step, and using separate Maven projects when they are supposed to be distinct. TrakEM2 uses a multi-module project with separate versioning of each component, which ends up being quite tricky (though doable) to manage.

Hi @hinerm,
Thank you very much for your kind reply. If I understand you correct, the first chunk of code should be placed in the top-level pom.xml, the 2nd chunk of code should be placed in the pom file of “imagingbook-common” folder. Where should I put the 3rd chunk of code? Please note that I have the following folder structure:

ImagingBook (top folder containing a pom file)
---- subfolder “imagingbook-common” (folder containing a pom file)
---- subfolder “imagingbook-plugins-all” (folder containing a pom file)
---- subfolder “imagingbook-plugins-en2” (folder related to the version of my book;containing a pom file)
---- subfolder “settings”
---- other 4 subfolders related to other version of the book.

Thanks!

Dear All,
After modifying pom files as suggested by @hinerm, I noticed that many of the errors related to ij classes are gone. But I do have many errors related to the classes of imagingbooks. Please see the attached snapshot. Thanks

No, sorry for not pasting the full patch. All of these changes go in the very top-level pom.xml (with artifactId of imagingbook-public).

All of the POMs in subdirectories inherit from imagingbook-public so the changes will propagate through the project.

You have errors in the imagingbook_plugins_* projects because they aren’t picking up the imagingbook-common dependency properly (as-is, the dependency is listed with no version property).

Adding the dependencyManagement block to your imagingbook-public POM will effectively tell the imagingbook_plugins_* components what version of imagingbook-common to depend on (1.2-SNAPSHOT). Then they should be able to find this dependency and be happy.

Thank you very much @hinerm. Now I could load the project without any error messages (though there are two ! signs … please see the attached screenshot). But when I tried to compile a java class, it fails! Please see the attached screenshot. Could please tell me what’s going wrong? Thank you very much.

These are warning symbols. That is OK. Warnings can actually vary based on how your Eclipse is configured.

I am assuming you tried to build imagingbook_plugins_en2 by right-clicking the project and selecting Run As > Maven install (or something similar, to explicitly invoke Maven).

When Maven goals are called, even from Eclipse, they behave like they would on the command line. So, they will look for the things they need only from Maven. Even though Eclipse will recognize these projects as linked some necessary pieces aren’t available in your Maven ecosystem (note that if you ran these projects as Java Applications, it would find the dependencies correctly).
.
To fix this, you just need to install all the projects (which will also install their POMs). This will put versions of everything in your local Maven repository. This is only necessary because there is no remote Maven repository hosting these components.

To install:

  1. Right-click the imagingbook-public project in Eclipse
  2. Select Run As > Maven install

You should then be able to call Maven goals on the other projects after this, through Eclipse or the command line.

1 Like