Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.53j, ij.io.Opener no longer works on headless #140

Closed
rudolphi opened this issue Nov 23, 2021 · 6 comments
Closed

Version 1.53j, ij.io.Opener no longer works on headless #140

rudolphi opened this issue Nov 23, 2021 · 6 comments

Comments

@rudolphi
Copy link

rudolphi commented Nov 23, 2021

Throws

Caused by: java.awt.HeadlessException
	at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
	at java.awt.MenuComponent.<init>(MenuComponent.java:163)
	at java.awt.MenuItem.<init>(MenuItem.java:214)
	at java.awt.MenuItem.<init>(MenuItem.java:198)
	at java.awt.Menu.<init>(Menu.java:157)
	at java.awt.Menu.<init>(Menu.java:137)
	at ij.Menus.getMenu(Menus.java:849)
	at ij.Menus.getMenu(Menus.java:839)
	at ij.Menus.addMenuBar(Menus.java:109)
	at ij.IJ.init(IJ.java:422)
	at ij.Menus.getCommands(Menus.java:1339)
	at ij.io.Opener.<clinit>(Opener.java:47)
	...
@rudolphi rudolphi changed the title Version 1.53j, ij.io.Opener nolonger work on headless Version 1.53j, ij.io.Opener no longer works on headless Nov 23, 2021
@ctrueden ctrueden transferred this issue from imagej/ImageJ Nov 23, 2021
@ctrueden
Copy link
Member

ctrueden commented Nov 23, 2021

@rudolphi With which version of the original ImageJ was this working for you? In a BeakerX Groovy notebook, using the following code:

%classpath add mvn net.imagej:ij:1.53d
System.setProperty("java.awt.headless", "true")
blobs = new ij.io.Opener().open("/home/curtis/data/blobs.tif")

I receive:

java.awt.HeadlessException
	at ij.gui.ImageWindow.<init>(ImageWindow.java:70)
	at ij.gui.ImageWindow.<init>(ImageWindow.java:66)
	at ij.ImagePlus.show(ImagePlus.java:486)
	at ij.io.Opener.open(Opener.java:117)

So it doesn't work with 1.53d either. And same error with 1.53a.

Are you using ImageJ2/Fiji, or plain ImageJ?

Assuming ImageJ2/Fiji, I took the liberty of transferring this issue into the imagej-legacy repo. Please note that ImageJ2 adds headless patching on top of the original ImageJ, to enable better headless support, so I took the liberty of moving your issue here to imagej-legacy, which (along with ij1-patcher) is the project responsible for this patching. Be aware that the newest version of ImageJ officially supported by the ImageJ2 patching mechanism is currently 1.53f. I would not recommend updating the ImageJ core beyond that until we get the patching mechanism updated to support ImageJ's latest code changes.

@rudolphi
Copy link
Author

I am using groupId=net.imagej, artifactId=ij. The problem is the IJ.init() that has been added to Menus.getCommands() if pluginsTable is null. Menus.getCommands() is called during class initialization.

@ctrueden ctrueden transferred this issue from imagej/imagej-legacy Nov 23, 2021
@ctrueden
Copy link
Member

@rudolphi OK, I transferred the issue back to the original ImageJ repository, then.

With which version of ImageJ are you able to use ij.io.Opener headless?

@rudolphi
Copy link
Author

It works smoothly with 1.53i

@ctrueden
Copy link
Member

@rudolphi Ah I see, I was testing the wrong thing. I was using Opener#open(String), but that tries to show the image. The Opener#openImage(String) method, which returns ImagePlus, does indeed work headless with 1.53i for me:

$ echo 'public class X { public static void main(String[] args) { System.out.println(new ij.io.Opener().openImage(args[0])); } }' > X.java
$ mvn dependency:get -Dartifact=net.imagej:ij:1.53i
$ javac -cp ~/.m2/repository/net/imagej/ij/1.53i/ij-1.53i.jar X.java
$ java -Djava.awt.headless=true -cp ~/.m2/repository/net/imagej/ij/1.53i/ij-1.53i.jar:. X ~/data/blobs.tif
img["blobs.tif" (-2), 8-bit, 256x254x1x1x1]

And with 1.53j:

$ mvn dependency:get -Dartifact=net.imagej:ij:1.53i
$ java -Djava.awt.headless=true -cp ~/.m2/repository/net/imagej/ij/1.53j/ij-1.53j.jar:. X ~/data/blobs.tif
Exception in thread "main" java.lang.ExceptionInInitializerError
	at X.main(X.java:1)
Caused by: java.awt.HeadlessException
	at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:165)
	at java.desktop/java.awt.MenuComponent.<init>(MenuComponent.java:181)
	at java.desktop/java.awt.MenuItem.<init>(MenuItem.java:224)
	at java.desktop/java.awt.MenuItem.<init>(MenuItem.java:208)
	at java.desktop/java.awt.Menu.<init>(Menu.java:163)
	at java.desktop/java.awt.Menu.<init>(Menu.java:144)
	at ij.Menus.getMenu(Menus.java:849)
	at ij.Menus.getMenu(Menus.java:839)
	at ij.Menus.addMenuBar(Menus.java:109)
	at ij.IJ.init(IJ.java:422)
	at ij.Menus.getCommands(Menus.java:1339)
	at ij.io.Opener.<clinit>(Opener.java:47)
	... 1 more

So @rasband this is an issue introduced in b553ff4, which as @rudolphi points out, adds an IJ.init() call to Menus.getCommands(). Would it make sense to test for headless mode in Menus#getMenu(String, boolean) and return null in that case?

@rasband
Copy link
Member

rasband commented Nov 23, 2021

This bug should be fixed in the ImageJ 1.53o20 daily build. The Menus.getCommands() method now has a test for headless mode.

   public static Hashtable getCommands() {
      if (pluginsTable==null && !GraphicsEnvironment.isHeadless())
          IJ.init();
      return pluginsTable;
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants