Hello World for ImageJ Java Plugin

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](https://imagej.net/Scripting). Maybe [this](https://imagej.net/Developing_Plugins_for_ImageJ_1.x) 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"](https://qiita.com/kouichi-c-nakamura/items/cdaf19ed623436fb82ff), 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](https://qiita-image-store.s3.amazonaws.com/0/109898/6cdafd9b-7e85-75f3-c02e-e5e96bb01cb2.png)

2\. In the Script Editor, select **_Templates > [by Language] > Java > Imagej 1.x > Skeltons > Bare Plugin_**.
 ![Image006.png](https://qiita-image-store.s3.amazonaws.com/0/109898/a8731e92-6e9c-22d4-c3a6-f98f16f4c8c5.png)

3\. You'll see this code.
![Image007.png](https://qiita-image-store.s3.amazonaws.com/0/109898/aae4f851-a59e-cafc-fa74-a919130a9733.png)

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

```java
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](https://javadoc.scijava.org/ImageJ1/ij/IJ.html)

```java
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.

```java
IJ.showMessage("Hello World");
```

5\. After the edit, it should look like this. 
![Image009.png](https://qiita-image-store.s3.amazonaws.com/0/109898/9b03a2bc-82a8-11c5-8dac-9f6e3ab06cc2.png)

6\. Save the `Bare_Plugin.java` to anywhere in your computer by **_File > Save_**.
![Image010.png](https://qiita-image-store.s3.amazonaws.com/0/109898/5b589642-0c5b-49a6-5e02-308f3e8f5222.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](https://qiita-image-store.s3.amazonaws.com/0/109898/45465dd8-d0e1-d7da-462f-3e1295b0962f.png)

![Image012.png](https://qiita-image-store.s3.amazonaws.com/0/109898/09cc0200-3669-c9e9-eb8a-ec04ce42d8c0.png)

8\. There will be a Java error, but just ignore it.
![Image013.png](https://qiita-image-store.s3.amazonaws.com/0/109898/63a30cd3-f340-4db9-9523-13cc8cef8774.png)

![Image014.png](https://qiita-image-store.s3.amazonaws.com/0/109898/ed5a5449-83c0-c267-c3c3-e108bd9b3e87.png)

9\. Now you can find the `Bare_plugin.jar` file in the plugins folder.
![Image015.png](https://qiita-image-store.s3.amazonaws.com/0/109898/11a747c7-0b8b-8af7-dca6-561f54ce982b.png)

# 3. Run the plugin
10\. Re-lauch ImageJ/Fiji.
![Image016.png](https://qiita-image-store.s3.amazonaws.com/0/109898/e2dca47b-7b0a-12fd-0a67-0b345510c744.png)

11\. Go **_Plugins > Bare Plugin_** to run the plugin.
![Image018.png](https://qiita-image-store.s3.amazonaws.com/0/109898/7f94f693-ac86-f7c7-50ee-21cf57b3910e.png)

12\. There you go! You can see the Hello World message showing up.
![Image019.png](https://qiita-image-store.s3.amazonaws.com/0/109898/d721c3d0-d2e3-9f9b-bfe1-f46a8c852712.png)