Search code examples
pluginsnetbeansimagej

ImageJ plugin development and NetBeans IDE


Can you help me to setup the ImageJ plugin development with NetBeans IDE? I tried with existing project, but there is two problem's: - Java Free form porject: i cannot attach the onvif.xsd in this project. - i'm trying this tutorial: tutorial but in the netbeans 7.2.1 i can't do this: "Go to Build>Compile "(The name of your plugin)""

please give me advise!


Solution

  • I am using Maven to develop ImageJ plugins in NetBeans. I'll show how to create and debug a simple plugin.

    You can start by creating a new Maven project File->New Project->Maven->Java Application and creating a simple PlugIn class:

    package cz.cuni.lf1.imagejstubproject;
    import ij.IJ;
    import ij.plugin.PlugIn;
    public class Hello implements PlugIn {
      public void run(String arg) {
        IJ.showMessage("My_Plugin", "Hello world!");
      }
    }
    

    Now create a plugins.config file in folder src/main/resources with content:

    Plugins, "Hello World!", cz.cuni.lf1.imagejstubproject.Hello
    

    The first String is the menu folder in which your plugin will appear, the second is the menu label and the third is the full class name of the class to run when the menu item is clicked.

    Next step is to add a dependency to ij.jar which contains the ImageJ classes. This can be done by editing the pom.xml file and adding:

    <project>
      ...
      <build>
       ...
      </build>
      <dependencies>
        <dependency>
          <groupId>gov.nih.imagej</groupId>
          <artifactId>imagej</artifactId>
          <version>1.45</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    </project>
    

    During the build process Maven will automatically download the required library from internet. It can also download source and JavaDoc.

    ImageJ requires that the name of the final jar contains an underscore so we set the filename accordingly. Change pom.xml to contain something like this:

    <project>
    ...
      <build>
        <finalName>${project.artifactId}_${project.version}</finalName>
        ...
      </build>
      ...
    </project>
    

    This is all you need to build a ImageJ plugin. If you compile it and copy the jar to ImageJ plugins directory you should see your plugin in the ImageJ plugin menu. If you do not want to debug your plugin in Netbeans, you can stop here.

    I do like to copy the new jar to the ImageJ plugins folder when I compile it in Netbeans so I can quickly try it out. You don't even have to restart ImageJ to run your new plugin. Just click Help->Refresh Menus in ImageJ to reload plugins. To copy the plugin to the ImageJ folder on comile, edit pom.xml to contain:

    <project>
      ...
      <properties>
       <imagej.path>c:\Program Files (x86)\ImageJ</imagej.path>
      </properties>
      <build>
        <finalName>${project.artifactId}_${project.version}</finalName>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>copytoplugins</id>
                <phase>install</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <tasks>
                    <copy todir="${imagej.path}/plugins/" file="target/${project.build.finalName}.jar"/>
                  </tasks>
                </configuration>
              </execution>
            </executions>
          </plugin>
          ...
        </plugins>
      </build>
      ...
    </project>
    

    And edit the imagej.path property to point to your ImageJ installation folder (not the plugins folder). Now your plugin should be copied to the plugins folder on each build.

    To be able to debug your plugin from Netbeans add this to your pom.xml:

    <project> 
      ...   
      <build>
        <finalName>${project.artifactId}_${project.version}</finalName>
        <plugins>
          ...
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
              <execution>
                <goals>
                  <goal>exec</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <executable>java</executable>
              <commandlineArgs>-jar     -agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} "${imagej.path}/ij.jar"    </commandlineArgs>
              <workingDirectory>${imagej.path}</workingDirectory>
            </configuration>
          </plugin>
        </plugins>
      </build>
      ...
    </project>
    

    And then right click on your project and select properties. In the Actions tab edit the Debug Project action to this:

    • Execute goals: process-classes org.codehaus.mojo:exec-maven-plugin:1.2:exec
    • Activate profiles:
    • Set properties: jpda.listen=true

    Now when you click Debug Project in NetBeans ImageJ shoud run and you shoud be able to debug your plugins.

    You can download this sample plugin here.