Issue loading remote images

49 views
Skip to first unread message

Yuri Pourre

unread,
Oct 27, 2017, 1:10:39 PM10/27/17
to CheerpJ Developers
Hello,

I am trying to run the following code using cheerpj:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class RemoteImage {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.leaningtech.com/cheerpj/images/cheerpj.png");
            BufferedImage image = ImageIO.read(url.openStream());
            System.out.println(image.getWidth() + " x " + image.getHeight());
        } catch (IOException e) {
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }
}


It runs on java but I get this error in cheerpj:
null
cheerpOS.js:833

cheerpOS.js:833 java.net.SocketException
cheerpOS.js:833

cheerpOS.js:833     at java.lang.Throwable.getOurStackTrace(Unknown Source)
cheerpOS.js:833

cheerpOS.js:833     at java.lang.Throwable.getOurStackTrace(Unknown Source)
cheerpOS.js:833

cheerpOS.js:833     at java.lang.Throwable.printStackTrace(Unknown Source)
cheerpOS.js:833

cheerpOS.js:833     at java.lang.Throwable.printStackTrace(Unknown Source)
cheerpOS.js:833

cheerpOS.js:833     at java.lang.Throwable.printStackTrace(Unknown Source)
cheerpOS.js:833

Alessandro Pignotti

unread,
Oct 27, 2017, 1:46:06 PM10/27/17
to CheerpJ Developers, Yuri Pourre
Hello Yuri,

there are no low level networking primitives in the Web platforms, this is why the normal http handler is failing with a SocketException. CheerpJ provides a Web specific implementation for HTTP/HTTPS using XHR. To enable this special implementation you need to set the java.protocol.handler.pkgs property, by adding something like this early in your code:

System.setProperty ("java.protocol.handler.pkgs", "com.leaningtech.handlers");

Please remember that the browser will not allow you to access arbitrary HTTP resources from other domains, unless appropriate CORS header are sent from the server. The image you are currently using for example would not work, but this one should: https://cheerpjdeploy.leaningtech.com/cheerpj.png

to clarify, this limitation is enforced by the browser and it is not introduced by CheerpJ.

Alessandro

--
You received this message because you are subscribed to the Google Groups "CheerpJ Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cheerpj-developers+unsub...@googlegroups.com.
To post to this group, send email to cheerpj-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/cheerpj-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/cheerpj-developers/3a476f43-de7f-4712-ae97-d8c746dddf2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Leaning Technologies Limited

Alessandro Pignotti
CTO
aless...@leaningtech.com

Leaning Technologies Limited 
www.leaningtech.com

Twitter  Facebook  LinkedIn 

Yuri Pourre

unread,
Oct 27, 2017, 2:06:50 PM10/27/17
to CheerpJ Developers
Thanks Alessandro for the fast reply.

Using System.setProperty ("java.protocol.handler.pkgs", "com.leaningtech.handlers"); I can load remote images and local images pointing to their URL.
Reply all
Reply to author
Forward
0 new messages