IraS
IraS

Reputation: 729

JDK9 problems: failes running simple java code

I'm running DMelt (http://jwork.org/dmelt) and I've noticed that simple Java code fails when using JDK9. Here is a Jython example, and the error is posted below. I use Ubuntu to run DMelt.

from jhplot  import *
c1 = HPlot("Canvas")
c1.visible(1)
f1=F1D("x^2")
c1.draw(f1)
c1.export("image.pdf")

Or, rewritten in Java:

import jhplot.*;

class MyFunction  
{
public static void main(String[] args) { 
HPlot c1 = new HPlot("Canvas");
c1.visible(true);
F1D f1 = new F1D("x^2");
c1.draw(f1);
c1.export("image.pdf");
 }    
} 

Here is the error:

Traceback (most recent call last):
  File "a.py", line 7, in <module>
    c1.export("image.pdf")
    at java.desktop/javax.imageio.spi.ServiceRegistry.checkClassAllowed(ServiceRegistry.java:745)
    at java.desktop/javax.imageio.spi.ServiceRegistry.<init>(ServiceRegistry.java:140)
    at org.freehep.graphicsbase.util.export.ExportFileTypeRegistry.<init>(ExportFileTypeRegistry.java:33)
    at org.freehep.graphicsbase.util.export.ExportFileTypeRegistry.getDefaultInstance(ExportFileTypeRegistry.java:44)
    at org.freehep.graphicsbase.util.export.ExportFileType.getExportFileTypes(ExportFileType.java:180)
    at org.freehep.graphicsbase.util.export.ExportFileType.getExportFileTypes(ExportFileType.java:172)
    at jhplot.io.images.ExportVGraphics.export(ExportVGraphics.java:101)
    at jhplot.gui.GHPanel.export(GHPanel.java:501)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: org.freehep.graphicsbase.util.export.ExportFileType is not an ImageIO SPI class

The same code works fine on all Java versions 1.5-1.8. Note that this error has nothing to do with Jython. This is a new JDK9 problem, which is not related to JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState

Upvotes: 2

Views: 1269

Answers (2)

user7975996
user7975996

Reputation: 109

Indeed, Oracle has introduced this sudden feature that restricts javax.imageio.spi.ServiceRegistry to certain image classes. You can easily fix this in DMelt framework and other open source code that use VectorGraphics:

1) Get source code of this package from JDK8 http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/9d617cfd6717/src/share/classes/javax/imageio/spi/ that does not have restriction

2) Rename the package and add it to your project. At this stage, you can remove javax.imageio.spi.ServiceRegistry completely.

This simple solution works well. But make sure your project is GNU-based.

Upvotes: 1

Alan Bateman
Alan Bateman

Reputation: 5449

There is an incompatible change in this area in JDK 9. From the JDK 9 release notes:

Since Java SE 1.4, javax.imageio.spi.ServiceRegistry provided a facility roughly equivalent to the Java SE 1.6 java.util.ServiceLoader. This image i/o facility is now restricted to supporting SPIs defined as part of javax.imageio. Applications that use it for other purposes need to be re-coded to use ServiceLoader.

org.freehep.graphicsbase seems to be the FreeHEP project. Someone needs to check their issue tracker to see if they have already addressed this issue. It might be that they can simply change their code to use java.util.ServiceLoader rather than the somewhat obscure javax.imageio.spi.ServiceRegistry.

Upvotes: 3

Related Questions