Jython instances and imports randomly lost over time?

I wrote a Jython script defining a couple of classes. This script also creates a swing GUI has buttons which access instances of these classes by callback functions.

The script was working last year and had not used it for quite a while.
Now when I use it, it seems that instances are randomly deleted over time, i. e. when I start the script, the buttons work but after some time I get error messages that instances are not present anymore.
The same seems to be true for imported packages.

Has someone experienced this? Is it a ImageJ or a Java problem?

I would be very grateful for any help or comments!

Can you share the Jython script? This would facilitate giving specific advice.


Hi All,

We had the same issue here, where for instance from ij import IJ worked for 'some time" and then the IJ instance was lost. We figured out that this can happen when script is using Swing to make a GUI. It basically can happen that the script itself is not running anymore but the Swing GUI is still active; now since the script is finished the imported instances are lost. So our workaround was to prevent the script from stopping. Overall seems a bit weird and the behavior was different on different computers.

A short script to reproduce would be really helpful. I am not a Python expert, so coding up even a small UI as a Python script takes a lot of fumbling for me. Something to get us started would be appreciated. Otherwise, this thread is just going to sit.


thanks for the answers and sorry for not coming back I’m very busy currently. I rebuilt the code and could not find the cause of the error. The instances just get lost randomly after some time. I switched computers and it worked, so I think its a combination of Java version (8) and old OSX (10.7.5). I will post the code as soon as I have more time.


Here is “the minimal script”. Was not as obvious as we thought in the beginning.

SwingScript_Test.zip (867 Bytes)

Apparently problem occurs not after the first call of imported classes, but after hundreds or thousands of iterations.

Can you please check if error in this script also occurs on your machine?
You have to click [Create] a bunch of times and at some point it throws some error, e.g.

Exception in thread “AWT-EventQueue-0” Traceback (most recent call last):
NameError: global name ‘ByteProcessor’ is not defined

When you uncomment last line (to start dialog that prevents stopping the script) the error should disappear.

Thanks @Christian_Tischer! I will try to find time to investigate next week.

@Christian_Tischer When I run your script, I get the same exception when I click a button for the second time. A small modification let me create 1,000,000 images without an exception. I just moved some imports to the methods that use them.

def quit(self, event): #quit the gui
    from ij import IJ
    IJ.run("Close All")
def create(self, event): #quit the gui        
    from ij import ImagePlus, IJ
    from ij.process import ByteProcessor
    for i in range(0,10000):
        print self.counter
        newImage=ImagePlus('New Image',ByteProcessor(10,10))        
    print 'Finish create'

Interesting. If that’s true than each GUI element has to import its own modules. This has some logic to it but blows up the code quite a bit; and I am also not sure this is how it is supposed to be.

It would be good to check whether this happens when running a Jython script from the CLI using jython itself (i.e.: not ImageJ). And if so, ask on the jython users list whether it is a bug.

Spurred by PAvel, here are my initial thoughts on where to investigate first:

  1. Upgrade Fiji to Jython 1.7. The heavy lifting has already been done. Just need to upload it, then see if this bug is fixed. You can test before it is uploaded by downloading the 2.7.0 version of jython-shaded and replacing the 2.5.3 version in the jars folder of Fiji.

  2. If not fixed, then look in the scripting-jython repository codebase at the Jython ScriptLanguage code. In particular, we needed to add reference cleanup logic to prevent Jython from polluting the JVM long-term (I cannot recall the details; check commit messages). If I had to bet on where the problem is, I would bet this logic is killing the script data structures internally upon conclusion of execution. If I am right, we would need to make this logic smarter, to refrain from cleaning up until new threads are stopped or some such.

I attempted the fix number 1. Downloaded Jython 2.7.0 and replaced 2.5.3 in jars folder. Unfortunately the problem persists.

The second suggested fix is beyond my reach. In case it is helpful here is a link to a code that reproduces the problem https://github.com/fzadow/tileserver/blob/01d934e73b606888360c3706493291c0432a6add/scripts/Convert_to_HDF5.py

It needs a large image opened through Import/Bioformats