Loading an external JAR from a plugin

Is there a way to import a JAR from an ImageJ plugin?

I need the user to select the location of a 3rd party JAR that might not have been available or on the classpath at launch and then import classes from that jar.

Hi @markkitt,

Sorry it took awhile for someone to get to this.

I’d probably recommend copying the jar into fiji’s classpath, i.e. into the Fiji.app/jars, then asking the user to restart fiji. (This is basically what the fiji updater does).

You can copy files in your java code with something like this

Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);

copied from here.

If restarting is not acceptable, then you have to do something fancy - modifying the class loader at runtime which I don’t have experience with. My impression is that it won’t be worth the headache.

John

I’m aware of the copying solution, however, the imported jars look for native library resources relative to its location, so copying just the jar is not useful.

The launcher seems to be already doing something along the lines of modifying the classpath, but I cannot seem to understand how it is intended to be used. What I seems to be heading towards at the moment is developing an alternate launcher.

Is there a way to include another folder in addition to Fiji.app/jars to act in similar fashion via the current launcher?

What would the command line method to instantiate Fiji by directly using the Java interpreter so that it loads in a similar fashion as when the launcher is used?

Hey @markkitt,

Got it thanks for clarifying. This should do it:

your_imagej_executable -cp  the_path_to_your_jar

Check out the command line options for the imageJ / fiji executable.

--class-path, --classpath, -classpath, --cp, -cp
        append <path> to the class path
--jar-path, --jarpath, -jarpath <path>
        append .jar files in <path> to the class path

John

2 Likes