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.
2. In the Script Editor, select Templates > [by Language] > Java > Imagej 1.x > Skeltons > Bare Plugin.
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 theBare_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.
6. Save the Bare_Plugin.java
to anywhere in your computer by File > Save.
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.
8. There will be a Java error, but just ignore it.
9. Now you can find the Bare_plugin.jar
file in the plugins folder.
3. Run the plugin
11. Go Plugins > Bare Plugin to run the plugin.
12. There you go! You can see the Hello World message showing up.