2

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

IraS
  • 729
  • 1
  • 5
  • 6
  • Possible duplicate of [JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState](https://stackoverflow.com/questions/46230413/jdk9-an-illegal-reflective-access-operation-has-occurred-org-python-core-pysys)? – Naman Oct 14 '17 at 16:51
  • In fact did `--add-opens java.base/java.io=ALL-UNNAMED` work? Also do share the code that you wrote in java since the problem occurred there. – Naman Oct 14 '17 at 17:05
  • Here is a similar Java code that failed on JDK9: http://jwork.org/dmelt/code/code.php?id=28356637.java Simply replace the last line 32 with the line c1.export("test.pdf"). The code also failed exporting to EPS (test.eps) or SVG (test.svg). It seems the problem comes from VectorGraphics https://github.com/freehep/freehep-vectorgraphics – IraS Oct 14 '17 at 17:07
  • It's not the code, but the library you are using that is causing that failure. I don't see any other changes in the link shared. – Naman Oct 14 '17 at 17:11
  • 1
    GeoTools had a similar problem. It ended up [reimplementing the `javax.imageio.spi.ServiceRegistry` API](https://github.com/geotools/geotools/pull/1670). – Nicolai Parlog Oct 15 '17 at 09:07

2 Answers2

3

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.

TT.
  • 15,774
  • 6
  • 47
  • 88
Alan Bateman
  • 5,283
  • 1
  • 20
  • 25
1

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.

user7975996
  • 109
  • 1
  • 6