Nicolas Stifani
Nicolas Stifani

Reputation: 603

ImageJ Create a .jar to run set of macros

I wrote several macros for ImageJ and I would like to create a .jar (.zip) file to make easy the distribution and the installation. I can't find the way to run a macro that is located within the jar file. I looked at the ImageJ API and I maybe because I am not familiar with Java I was not able to find the answer.

The jar file contains:

  1. plugins.config

    Plugins>MyPluginFolder, "Function1", my_plugin("my_macro_1.txt")
    
  2. my_macro_1.txt

    print("Hello world");
    
  3. The my_plugin.java and associated .class file

    import ij.*;
    import ij.process.*;
    import ij.gui.*;
    import java.awt.*;
    import ij.plugin.*;
    import ij.macro.*;
    
    public class my_plugin implements PlugIn {
        static boolean showArgs = true;
    
        public void run(String arg) {
            if (arg.equals("my_macro_1.txt"))
                {my_macro_function1(); return;}
        }
    
        public void my_macro_function1() {
            IJ.runMacro("my_macro_1.txt");
        }
    }
    

I guess I am missing something. Help would be sincerely appreciated.

Upvotes: 0

Views: 630

Answers (1)

Jan Eglinger
Jan Eglinger

Reputation: 4090

This has been discussed a few times in the ImageJ community already.

The easiest way to distribute macros, scripts and plugins is by using your own update site. In this case, there's no need to package scripts into a jar file, and users can install all your scripts at once by a single click.

If you anyway prefer to package your scripts into a jar file, have a look at the Script Launcher plugin: it allows to do exactly what you're trying to do, with supplying only the script file and a plugins.config file, rendering your step #3 redundant.

The problem in your code above was that ImageJ cannot see the resource file my_macro.txt in the jar file. You need to use the ClassLoader#getResourceAsStream() method to do that. See how it's done in the Script Launcher.

Upvotes: 1

Related Questions