How can we prepare an environment for the development of ImageJ plugins in Java using Eclipse IDE? Here is an answer. The example below was for Windows 10, but the procedures required for macOS is very similar, except that you don't have to set the environmental variables. It has quite a lot of steps, and a bit challenging for someone who is not familiar with Eclipse.
References
These two references are almost identical to mine. Yet, the first one is pretty old (last updated in 2010) with a bug, and I still needed to struggle a bit to get it to work. The second one is in Japanese. So I decided to write my own version in English, which really worked for me. I think the example below is equivalent of Method 3 of the reference 1: "Importing the ImageJ source, using ImageJ build.xml, building with ant"
1. Install JDK 8
-
Go to the Oracle web site and download JDK 8.
-
Accept License Agreement and download JDK (Windows x64 in this case. Choose the one that is for you).
-
You'll have downloaded the file
jdk-8u181-windows-x64.exe
(Windows x64). Double-click the file and install the JDK to your PC.# -
Change the Environment Variables for JDK (NOTE: this is only required for Windows. For macOS, jump to step 5)
-
Type
adv
in the Windows Search Box next to the Start button. Click View advanced system settings. Then go to Environmental Variables... > User variables for XXXXX
-
Find the folder where the JDK binary files are. It should look like:
C:\Program Files\Java\jdk1.8.0_181\bin
. Copy the address from the Explorer and paste it into a new field by clicking the New button.
-
-
Type
cmd
in the Windows Search Box next to the Start button. Launch Command Prompt. (for macOS, launch Terminal) -
Type
javac -version
into Command Prompt and press Enter. -
If the installation of JDK is successful, you'll see the version of JDK as below:
2. Install Eclipse
-
Go to Eclipse download page https://www.eclipse.org/downloads/ and Click the Download button. Click Download in the next page to start downloading.
-
Launch the Eclipse installer, and click Eclipse IDE for Java Developers.
-
Choose Installtion folder. With regard to file permission, it is recommended to install Eclipse outside of usual
C:\Programs\
. click the INSTALL button.
-
During installation, you'll be asked about licensing. Click Accept to proceed.
-
Select a directory as workspace. Accept as it is and click Launch.
3. Create a Java project IJ
-
In the Source tab, make sure that Default output folder is set as
IJ/bin
.
-
Then click Link additional source:. This will make ImageJ jar files available to this Java project.
-
Download the latest version of Image from https://imagej.nih.gov/ij/download/src/. Unzip the file in a appropriate folder.
-
Using Browse button, choose the
source
folder within the ImageJ folder unzipped. Click Next.
-
In the Inclusion and Exclusion Patterns dialog, click Add for Inclusion patterns.
-
Also Add
images/
,macors/
, andplugins/
to Exclusion patterns, and click Finish.
-
-
In the Libraries tab, click Add External JARs..., and browse to JDK's
toolbar.jar
file.
- In Windows 10,
toolbar.jar
can be found atC:\Program Files\Java\jdk1.8.0_181\lib\toolbar.jar
or somewhere similar. - In macOS, it can be found at
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/toolbar.jar
.
- In Windows 10,
4. Create a Java project TESTPlugin_
-
Set Project name as
TESTPlugin_
. In order to show the new plugin in ImageJ's pull down menu, the name must end with underscore_
. Click Next.
-
In the Source tab, make sure that the Default output folder is set to
TESTPlugin_/bin
.
-
In the Projects tab, click Add and select the IJ, which you've just created. Click Finish.
5. Create TESTPlugin_ class
-
Copy the code below and paste it into
TESTPlugin_.java
. Make sure the name of the.java
file and the class name must be identical. Again, you need the underscore_
in order to make this plugin visible in ImageJ's pulldown menu.
import ij.IJ;
import ij.plugin.PlugIn;
public class TESTPlugin_ implements PlugIn {
public void run(String arg) {
IJ.error("Hello world!");
}
}
6. Create build.xml
<project name="TESTPlugin_" default="" basedir=".">
<description>
TESTPlugin_ build file
</description>
<property name="src" location="src" />
<property name="build" location="bin" />
<property name="dist" location="dist" />
<property name="pluginsDir" location="$basedir/../../IJ/plugins/" />
<property name="user.name" value="Patrick Pirrotte" />
<target name="main" depends="compress" description="Main target">
<echo>
Building the .jar file.
</echo>
</target>
<target name="compress" depends="" description="generate the distribution">
<jar jarfile="TESTPlugin_.jar">
<fileset dir="." includes="plugins.config" />
<fileset dir="${build}" includes="**/*.*" />
<manifest>
<attribute name="Built-By" value="${user.name}"/>
</manifest>
</jar>
<copy file="TESTPlugin_.jar" toDir="${pluginsDir}" />
</target>
</project>
7. Builder configuration
-
From Package Explorer, right-click on the
TESTPlugin_
project and select Properties.
-
In Builders, click New..., and select Ant Builder and click OK.
-
In the Main tab, go to Buildfile: and click Browse Workspace... and select the
build.xml
inTESTPlugin_
project.
-
In the Targets tab, click Set Targets... for both After a "Clean": and Auto Build:, and select main and compress for both of them.
8. Debug
-
Double-click Java application. Set Project to
IJ
and Main class: toij.ImageJ
(JavaPackageName.ClassName
)
-
Select the
TESTPlugin_
project. This step is crucial if you want to step into your plugin source during the debug phase.
-
ImageJ should launch and select Plugins > TESTPlugin (it should be visible at this stage).
Contact
Dr Kouichi C. Nakamura
kouichi.nakamura@pharm.ox.ac.uk
kouichi.c.nakamura@gmail.com
MRC Brain Network Dynamics Unit, Department of Pharmacology, University of Oxford