Login  Register

Re: How to test plugin?

Posted by Thomas Peterbauer on Jul 16, 2009; 12:44pm
URL: http://imagej.273.s1.nabble.com/How-to-uninstall-macros-tp3691772p3691777.html

Vivian,
I also frequently use what one could call a "launcher" class (Dscho's
variant) as the main class to start ImageJ in Eclipse (Galileo), and in
my opinion this route is more convenient than Justin's variant: your
plugin is started automatically, and you can set the launcher to do all
the things a user would normally do before running the plugin. For example:

      new ImageJ(null);
      IJ.open(path_to_file);
      IJ.run("Measure");
      IJ.runPlugIn("My_Plugin", "");

The macro recorder in combination with the Convert to Plugin command is
a nice tools to get corresponding java code without having to dig too
much in APIs.//
If you do not want to care about arguments to be passed from the IDE to
the main() method, let the launcher do the job:

import ij.IJ;
import ij.ImageJ;
import java.net.URL;

public class IJLauncher implements Runnable {
  public void run() {
      String name = getClass().getName();              
      URL url =getClass().getResource(name+".class");
      String path = url.toString().replaceAll("%20", " ");
      path = path.substring(6, path.indexOf("/plugins"));
      //System.out.println(path);
      System.getProperties().setProperty("plugins.dir", path);
      new ImageJ(null);
      IJ.runPlugIn("My_Plugin", "");
  }

  public static void main(String args[]) {
      Thread runnable = new Thread(new IJLauncher());
      runnable.start();
  }
}

Yours,
Thomas

Senseney, Justin (NIH/CIT) [E] schrieb:

> Vivian,
>
> Dscho's solution is a good general solution, but there's another way that I've found works a lot better when developing ImageJ plugins in Eclipse.  This only works reliably with the newer versions of Eclipse (Europa, Ganymede, etc). Try these steps:
>
> 1) Create a new Java project in Eclipse called "ImageJ" and put the ij.jar and IJ_Prefs.txt from your ImageJ install into this new project's working directory.  
>
> 2) Change the default output folder of your ImageJ project to "plugins", create a "src" folder to hold all your plugins in that same dialog box.
>
> 3) Refresh the project (so you can see the jars), then add ij.jar to the project's classpath.
>
> 4) Make a dummy plugin (implements PlugIn, has run(String) method)
>
> 5) Finally, in the Run configurations... tab set your main class to ij.ImageJ
>
> When you run this configuration, your dummy_plugin appears in ImageJ's list of plugins.  When you make changes to this plugin, your changes are reflected as soon as you restart ImageJ.  It's a little work to set up, but this is the most efficient way I've found to work on ImageJ plugins.  You can even attach the ImageJ src to the jar and use those files to debug w/ line references.
>
> -Justin
>
> -----Original Message-----
> From: Johannes Schindelin [mailto:[hidden email]]
> Sent: Tuesday, July 14, 2009 6:42 AM
> To: List IMAGEJ
> Subject: Re: How to test plugin?
>
> Hi Vivian,
>
> On Tue, 14 Jul 2009, Vivian Yong wrote:
>
>  
>> I tried to write a testing plugin based on the ImageJ tutorial. I can
>> test it after I put the plugin file in the ImageJ plugin folder. I just
>> wonder how can I test run it in Eclipse? Also, I would like to see the
>> output on the system for debugging purpose by using
>> System.out.print("..."); before the plugin be deployed to ImageJ, is
>> that possible and how?
>>    
>
> In a plugin I recently reviewed, I saw this gem:
>
>         public static void main(String[] args) {
>        try {
>                System.getProperties().list(System.err);
>                System.setProperty("plugins.dir", args[0]);
>                new ImageJ();
> IJ.run("This Plugin", "argument=value argument=value");
>        } catch (Exception ex) {
> e.printStackTrace();
>         }
>         }
>
> You specify the plugin class as main class, and pass the main() method the
> path to the directory containing the corresponding .class file.  Before
> constructing ImageJ(), you can output anything to System.out (or
> System.err).  Of course, you need to adjust the parameters of IJ.run()
> appropriately.
>
> Hth,
> Dscho
>