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

[native-image] Use of Graphics fails #1163

Closed
fellmann opened this issue Apr 14, 2019 · 25 comments
Closed

[native-image] Use of Graphics fails #1163

fellmann opened this issue Apr 14, 2019 · 25 comments
Assignees

Comments

@fellmann
Copy link

Minimum Example1.java:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Example1 {
	public static void main(String[] args) throws IOException {
		BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
		Graphics graphics = bufferedImage.getGraphics();
		graphics.drawString("test", 100, 100);
		graphics.dispose();
		ImageIO.write(bufferedImage, "PNG", new File("test.png"));
	}
}

The code can be executued with GraalVM: sudo docker run -v $(pwd):/work -w /work oracle/graalvm-ce:1.0.0-rc15 sh -c "javac Example1.java && java Example1"

But it is not working as Native Image: sudo docker run -v $(pwd):/work -w /work oracle/graalvm-ce:1.0.0-rc15 sh -c "native-image Example1 && ./example1"

Error:

Exception in thread "main" java.lang.Error: Could not find class: null
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:117)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1181)
	at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1170)
	at Example1.main(Example1.java:11)

@cstancu cstancu self-assigned this Apr 15, 2019
@DougTidwell
Copy link

I'm having the same problem...works great in a JAR, fails spectacularly as a native image. I'm building the native image via Quarkus, so I'm using graalvm-ce-1.0.0-rc16. When I invoke my code, I get this error:

UT005023: Exception handling request to /overlayImage: org.jboss.resteasy.spi.UnhandledException: java.lang.UnsupportedOperationException: Not implemented yet for GraalVM native images

After a relatively long stack trace, I get this more specific message:

Caused by: java.lang.UnsupportedOperationException: Not implemented yet for GraalVM native images
at javax.imageio.ImageIO.read(ImageIO.java:133)
at com.redhat.coderland.ImageOverlay.incomingImage(ImageOverlay.java:49)

May or may not be relevant: The call that fails isn't accessing the file system, it's trying to create an image from a base64 string passed in to the method. Here's the line that fails:

  BufferedImage baseImage =
      ImageIO.read(new ByteArrayInputStream
                   (Base64.getDecoder().decode(imageData)));

Hope this sheds some light on the problem.

@rsvoboda
Copy link

rsvoboda commented Oct 8, 2019

@cstancu any plans for java.awt.Graphics support in Substrate VM ?

I hit this problem when playing with JFreeChart and JFreeSVG in native mode using Quarkus.
https://github.com/rsvoboda/rsvoboda-playground/tree/master/jfreesvg/jfreesvg-quarkus

@sherl0cks
Copy link

@cstancu bumping this. If there are no plans, it would be good to for the community to begin coalescing around an alternative.

@NO-ob
Copy link

NO-ob commented Feb 3, 2020

Also bumping everything in my application works when built for native except the ability to save images

@sherl0cks
Copy link

I’m using https://commons.apache.org/proper/commons-imaging/ as a replacement which so far works just fine in a native Linux executable.

@NO-ob
Copy link

NO-ob commented Feb 3, 2020

Just incase anyone wants to write iamges from a url to a file I managed to use this instead of Imageio

try {
            InputStream initialStream = new URL(item.getFileURL()).openStream();
            OutputStream outStream = new FileOutputStream(imageFile);
            byte[] buffer = new byte[8 * 1024];
            int bytesRead;
            while ((bytesRead = initialStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, bytesRead);
            }
            initialStream.close();
            outStream.flush();
            outStream.close();
        } catch (IOException e){
            e.printStackTrace();
        }

This seems to work better too since it works with more filetypes and saves gif/video properly

@smil2k
Copy link

smil2k commented Mar 25, 2020

I’m using https://commons.apache.org/proper/commons-imaging/ as a replacement which so far works just fine in a native Linux executable.

How do you get the imagedata without using bufferedimage?

Can you post some code here?

@sherl0cks
Copy link

@smil2k take a look at https://commons.apache.org/proper/commons-imaging/sampleusage.html. You only replace FileIO which has native extensions to each platform (e.g. Linux vs Windows). BufferedImage works as normal.

@SetoKaiba
Copy link

Hi. When will this feature be added? I don't have a way to draw an image as the captcha code without Graphics. And it seems that there's no alternative. @cstancu

@smil2k
Copy link

smil2k commented Jun 4, 2020

@sherl0cks: Can you send some example code which compiles with graal into native image and uses buffered images? For me it did not, first I needed to provide the color profiles, then it was stopped somewhere again with "not implemented", thrown at runtime!

@sherl0cks
Copy link

I no longer have code on this, my team moved to python for image manipulation. My suspicion is that your not implemented is related to ImageIO, not buffered images, but I'm not in a position to help at this time.

@sherl0cks
Copy link

@cstancu I'm looking at some code again that needs ImageIO in graal. Any way to support you and help get this feature built?

@sherl0cks
Copy link

@vjovanov Any thoughts on how we may collaborate here?

@vjovanov
Copy link
Member

20.3 will include AWT and Swing that work on JDK 11. Please try it out in a few days on the master branch or wait for 20.3 release.

@IvanLuchkin
Copy link

@vjovanov This is great news but what about javax.imageio.ImageIO?

@jerboaa
Copy link
Collaborator

jerboaa commented Nov 26, 2020

At revision c92adba of graal master I'm getting (producing the native image works) the following trace on the initial ImageIO example from the reporter:

$ native-image Example1 graal-issue-1163
[graal-issue-1163:22214]    classlist:   1,789.06 ms,  0.96 GB
[graal-issue-1163:22214]        (cap):     472.43 ms,  0.96 GB
[graal-issue-1163:22214]        setup:   2,065.96 ms,  0.96 GB
[graal-issue-1163:22214]     (clinit):     449.75 ms,  2.29 GB
[graal-issue-1163:22214]   (typeflow):   9,233.10 ms,  2.29 GB
[graal-issue-1163:22214]    (objects):   9,714.72 ms,  2.29 GB
[graal-issue-1163:22214]   (features):     590.63 ms,  2.29 GB
[graal-issue-1163:22214]     analysis:  20,385.93 ms,  2.29 GB
[graal-issue-1163:22214]     universe:     588.62 ms,  2.29 GB
[graal-issue-1163:22214]      (parse):   2,749.90 ms,  2.29 GB
[graal-issue-1163:22214]     (inline):   3,021.43 ms,  2.30 GB
[graal-issue-1163:22214]    (compile):  14,004.08 ms,  3.29 GB
[graal-issue-1163:22214]      compile:  20,759.73 ms,  3.29 GB
[graal-issue-1163:22214]        image:   2,236.85 ms,  4.74 GB
[graal-issue-1163:22214]        write:     400.21 ms,  4.74 GB
[graal-issue-1163:22214]      [total]:  48,410.97 ms,  4.74 GB
$ ./graal-issue-1163 
Exception in thread "main" java.lang.NoSuchFieldError: java.awt.image.ColorModel.pData
	at com.oracle.svm.jni.functions.JNIFunctions$Support.getFieldID(JNIFunctions.java:1106)
	at com.oracle.svm.jni.functions.JNIFunctions.GetFieldID(JNIFunctions.java:408)
	at java.awt.image.ColorModel.initIDs(ColorModel.java)
	at java.awt.image.ColorModel.<clinit>(ColorModel.java:220)
	at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:351)
	at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:271)
	at java.awt.image.BufferedImage.<clinit>(BufferedImage.java:287)
	at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:351)
	at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:271)
	at Example1.main(Example1.java:10)

So there is still work left to do for ImageIO to work.

@vjovanov
Copy link
Member

Have you generated the config first with an agent? AWT is still not supported without configuration.

@jerboaa
Copy link
Collaborator

jerboaa commented Nov 26, 2020

Have you generated the config first with an agent? AWT is still not supported without configuration.

I have not. Let me try that now.

@jerboaa
Copy link
Collaborator

jerboaa commented Nov 26, 2020

OK that seems to work for png images illustrated in Example1.java:

$ rm -rf META-INF/native-image/* && java -agentlib:native-image-agent=config-output-dir=META-INF/native-image Example1
$ native-image Example1 graal-issue-1163 
[graal-issue-1163:55973]    classlist:   1,677.60 ms,  0.96 GB
[graal-issue-1163:55973]        (cap):     460.19 ms,  0.96 GB
[graal-issue-1163:55973]        setup:   1,930.53 ms,  0.96 GB
[graal-issue-1163:55973]     (clinit):     465.25 ms,  2.24 GB
[graal-issue-1163:55973]   (typeflow):  11,971.34 ms,  2.24 GB
[graal-issue-1163:55973]    (objects):  12,522.57 ms,  2.24 GB
[graal-issue-1163:55973]   (features):     900.19 ms,  2.24 GB
[graal-issue-1163:55973]     analysis:  26,571.84 ms,  2.24 GB
[graal-issue-1163:55973]     universe:     822.20 ms,  2.24 GB
[graal-issue-1163:55973]      (parse):   4,000.64 ms,  2.27 GB
[graal-issue-1163:55973]     (inline):  10,430.50 ms,  2.91 GB
[graal-issue-1163:55973]    (compile):  25,809.22 ms,  3.60 GB
[graal-issue-1163:55973]      compile:  41,916.14 ms,  3.60 GB
[graal-issue-1163:55973]        image:   3,146.05 ms,  3.61 GB
[graal-issue-1163:55973]        write:     512.69 ms,  3.61 GB
[graal-issue-1163:55973]      [total]:  76,750.03 ms,  3.61 GB
$ ./graal-issue-1163 
$ ls test.png 
test.png

@vjovanov
Copy link
Member

Can we close the issue?

@smil2k
Copy link

smil2k commented Nov 26, 2020

@jerboaa: for the record, could you tell me how did you created the "config with an agent" for graalvm?

@vjovanov
Copy link
Member

We have documentation on that:

https://github.com/oracle/graal/blob/master/substratevm/BuildConfiguration.md

@jerboaa
Copy link
Collaborator

jerboaa commented Nov 27, 2020

@smil2k See #1163 (comment) The -agentlib:native-image-agent[...] part generates files in META-INF/native-image which then the native-image invocation picks up. Consider also adding --verbose so as to see how this config is being picked up.

@jerboaa
Copy link
Collaborator

jerboaa commented Nov 27, 2020

@vjovanov One small issue left. See #3031

@dilipdhankecha2530
Copy link

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.0)

2022-12-17T00:42:33.826+05:30  INFO 10967 --- [           main] com.example.demo.DemoApplication         : Starting AOT-processed DemoApplication using Java 17.0.5 with PID 10967 (/home/dilip/Desktop/demo/build/native/nativeCompile/demo started by dilip in /home/dilip/Desktop/demo)
2022-12-17T00:42:33.826+05:30  INFO 10967 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2022-12-17T00:42:33.832+05:30  INFO 10967 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.016 seconds (process running for 0.019)
Exception in thread "main" java.lang.NoSuchFieldError: java.awt.image.ColorModel.pData
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions$Support.getFieldID(JNIFunctions.java:1271)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions.GetFieldID(JNIFunctions.java:425)
	at [email protected]/java.awt.image.ColorModel.initIDs(ColorModel.java)
	at [email protected]/java.awt.image.ColorModel.<clinit>(ColorModel.java:221)
	at [email protected]/java.awt.image.Raster.<clinit>(Raster.java:172)
	at org.apache.pdfbox.pdmodel.PDDocument.<clinit>(PDDocument.java:107)
	at com.example.demo.DemoApplication.main(DemoApplication.java:20

While creating a native build with spring 3.0 at that time AWT has not supported any idea or any suggestion for to achieve this issue.

You can refer to this issue as well spring-projects/spring-framework#29704.

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

No branches or pull requests