Pyimagej error when initializing

When I initialize pyimagej with a specific version (let’s say ‘2.1.0’) a Java error comes out:

>>> import imagej
>>> ij = imagej.init('2.1.0')

Then this error shows up:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/altair/anaconda3/envs/py37/lib/python3.7/site-packages/imagej/__init__.py", line 214, in init
    WindowManager = JObject(_WindowManager)
  File "/home/altair/anaconda3/envs/py37/lib/python3.7/site-packages/jpype/_jobject.py", line 59, in __new__
    return _JObjectFactory(*args, **kwargs)
  File "/home/altair/anaconda3/envs/py37/lib/python3.7/site-packages/jpype/_jobject.py", line 111, in _JObjectFactory
    return tp._cast(v)
TypeError: Unable to cast 'type' to java type 'java.lang.Class'

Instead, when I initialize without specifying the version some warnings appear, mainly log4j, and an ERROR: cannot create pluguin.

log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.imagej.patcher.LegacyInjector (file:/home/altair/.jgo/net.imglib2/imglib2-imglyb/1.0.0+net.imagej-imagej-RELEASE+net.imagej-imagej-legacy-RELEASE/ij1-patcher-1.1.0.jar) to method java.lang.ClassLoader.findLoadedClass(java.lang.String)
WARNING: Please consider reporting this to the maintainers of net.imagej.patcher.LegacyInjector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[ERROR] Cannot create plugin: class='org.scijava.plugins.scripting.javascript.JavaScriptScriptLanguage', name='JavaScript', priority=0.0, enabled=true, pluginType=ScriptLanguage

What are theses errors referencing to? Should I install/fix some package?

Thanks

I get a similar error message to @Altairch95 when trying to initialise the ImageJ GUI using the most basic code:

Python 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 15:50:08) [MSC v.1916 64 bit (AMD64)] on win32

import imagej
ij = imagej.init()
ij.ui().showUI()

gives output:

log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.imagej.patcher.LegacyInjector (file:/C:/Users/username/.jgo/net.imglib2/imglib2-imglyb/1.0.0+net.imagej-imagej-RELEASE+net.imagej-imagej-legacy-RELEASE/ij1-patcher-1.1.0.jar) to method java.lang.ClassLoader.findLoadedClass(java.lang.String)
WARNING: Please consider reporting this to the maintainers of net.imagej.patcher.LegacyInjector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

This was done in a fresh install of Anaconda in WIndows 10, using conda to install Pyimagej (version 1.0) into a conda environment from conda-forge. It was the first package that I added, so it co-installed dependencies (jpype, openjdk, maven, etc.). Specifying a freshly installed local installation of Fiji (after removing FilamentDetector):

import imagej
ij = imagej.init("C:\\Fiji.app", headless=False)
ij.getVersion()
ij.ui().showUI()

gives this error:


WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.imagej.patcher.LegacyInjector (file:/C:/Fiji.app/jars/ij1-patcher-1.1.0.jar) to method java.lang.ClassLoader.findLoadedClass(java.lang.String)
WARNING: Please consider reporting this to the maintainers of net.imagej.patcher.LegacyInjector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

I would love to be able to use ImageJ within a Python framework. Any help appreciated!

@Guy I think the problem may be unrelated to the warnings you’re seeing - to start the UI you need to initialize with headless=False.

The missing log4j is a known issue but shouldn’t affect performance.

What version of Java is being used here? I think maybe this warning starts showing up in Java 9+. It suggests something may need to change in the ij1-patcher component.

This is a bug with a mandatory dependency on imagej-legacy that was meant to be optional. It should be fixed here, in the next release of pyimagej. Until then you can work around the issue by explicitly adding imagej-legacy to the initialization, e.g.:

import imagej
ij = imagej.init(['net.imagej:imagej:2.1.0', 'net.imagej:imagej-legacy'])
1 Like