How to call a function within a Java plug-in using Jython?

Tags: #<Tag:0x00007fd548b6e318> #<Tag:0x00007fd548b6df30> #<Tag:0x00007fd548b6d800> #<Tag:0x00007fd548b6cec8> #<Tag:0x00007fd548b6cce8>


I’m writing a Jython script that is supposed to call a function from an open-source plug-in called IHC Toolbox. I decompiled this .jar plug-in using JD GUI so that I can see all the files associated with the plug-in. There are many .class and a few .java files inside this plug-in. I sifted through all the codes to find the command I would like to run. It’s called runCommand and seems to be nested inside the class buttonCommand. I don’t know Java but I thought in Jython, I could just say

import buttonCommand

But it’s not finding runCommand because apparently runCommand doesn’t exist in the class buttonCommand.

I think the problem here is that I don’t know Java and do not understand how it’s organized. Would anyone please give me a pointer? I’ve included some screenshots here. I circled the class and the function in question. Also circled the corresponding “Color” button that the Jython script is suppose to tell the plug-in to execute.



there are two problems, the second one is hard to solve.

  1. The class buttonCommand is not part of a package. I don’t know how Jython handles this. Best practice is to organize Java code in package. If you open the Duplicater plugin it starts with package ij.plugin.filter; and therefore the import is from ij.plugin.filter import Duplicater.
  2. To run a Java method (a function belonging to a class) you need to create an instance of that class. In this case btnCmd = buttonCommand("command", imp, ip). Now you can run the method using btnCmd.runCommand("Color", imp, ip). But the visibility will prevent you from using this class and it’s methods. Again have a look at Duplicater plugin. The class and all it’s methods are public and therefore usable from within a Jython script. buttonCommand has no visibility keyword and therefore the visibility is package private. This means that only classes of the same package can access it.



Thank you so much. So in regards to the method being inaccessible to the outside world, there is no way to change this since the original .jar file is written this way? Is my understanding correct?



Well, it is possible to access the class using the Java reflection API, but it is not exactly intuitive to do. Better would be to create a new version of the IHC Toolbox with proper visibility and package structure.


Got it. I learned a lot trying to hack the IHC Toolbox. Thank you. Ended up finding an alternative solution using the built-in function Colour Deconvolution instead. Here’s the macros I wrote for batch, automated measurement of Masson Trichrome and Sirius Red stained tissues in case anyone find this useful :slight_smile:

For Masson Trichrome detection:

Or for Sirius Red detection. Pretty similar to Masson Trichrome with 1 little change…