LoginSignup
3
1

More than 5 years have passed since last update.

Hello World for ImageJ Java Plugin

Last updated at Posted at 2018-12-04

I wrote a few ImageJ plugins a decade ago, and at that time it was relatively simple. ImageJ only worked with Java or ImageJ Macro language. But ImageJ is like a hybrid of ImageJ1 and ImageJ2, which are completely different internally, and it accepts scripting in BeanShell, Groovy, ImageJ Macro, JavaScript, Lisp (Clojure), MATLAB, Python, R, Ruby, and Scala, half of which I've never heard of.

With that, the way to write and execute the simplest ImageJ Plugin in Java was kind of lost, at least to me. It certainly doesn't exist in the Scripting page. Maybe this is the page, but it's rather difficult to reach and doesn't seem to give you the simplest code example.

I've written an article about "Hello world" for ImageJ with Eclipse", but this is way too complicated for a beginner and me.

So, here I show you how to write the simplest "Hello World" ImageJ Plugin.

1. Save the Java source code (.java)

1. File > New > Script... will open a new script document in the Script Editor.
Image005.png

2. In the Script Editor, select Templates > [by Language] > Java > Imagej 1.x > Skeltons > Bare Plugin.
Image006.png

3. You'll see this code.
Image007.png

4. Copy and paste the following code to the file.

import ij.plugin.PlugIn;
import ij.IJ;

/**
 * This is a template for a plugin that does not require one image
 * (be it that it does not require any, or that it lets the user
 * choose more than one image in a dialog).
 */
public class Bare_PlugIn implements PlugIn {
    /**
     * This method gets called by ImageJ / Fiji.
     *
     * @param arg can be specified in plugins.config
     * @see ij.plugin.PlugIn#run(java.lang.String)
     */
    @Override
    public void run(String arg) {
        IJ.showMessage("Hello World");
    }
}
  • We added the following line at the line 2 of the Bare Plugin to import the package ij.IJ
import ij.IJ;
  • In the run method of the Bare_PlugIn class, we added a line as below. This is the command you want to execute.
IJ.showMessage("Hello World");

5. After the edit, it should look like this.
Image009.png

6. Save the Bare_Plugin.java to anywhere in your computer by File > Save.
Image010.png

2. Save .jar in the plugins folder

7. Then, export the code as a Bare_Plugin.jar file (Java Archive) in the plugins folder (Fiji.app\plugins) by File > Export as .jar.
Image011.png

Image012.png

8. There will be a Java error, but just ignore it.
Image013.png

Image014.png

9. Now you can find the Bare_plugin.jar file in the plugins folder.
Image015.png

3. Run the plugin

10. Re-lauch ImageJ/Fiji.
Image016.png

11. Go Plugins > Bare Plugin to run the plugin.
Image018.png

12. There you go! You can see the Hello World message showing up.
Image019.png

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1