Packaging LibGDX program to exe with Packr breaks code which opens URL stream

400 Views Asked by At

In my LibGDX program, I attempt to obtain the user's IP from https://api.ipify.org. When I run the jarfile directly it works as expected, but it breaks when I run the exe produced by Packr.

I compiled the jar with JDK 11.0.1 and tried bundling it with both JDK 11 and 12. Because JDK 11 and 12 do not contain the jre folder which Packr looks for, I manually created it and copied bin and lib into it. This is the config json I gave to Packr:

{
    "platform": "windows64",
    "jdk": "C:\\Program Files\\Java\\jdk-12",
    "executable": "ShengJi-win64",
    "classpath": [
        "desktop-1.0.jar"
    ],
    "removelibs": [
        "desktop-1.0.jar"
    ],
    "mainclass": "com.sage.shengji.desktop.DesktopLauncher",
    "minimizejre": "soft",
    "output": "out-windows64"
}

This code is run in the show() method for my Screen.

           try {
               System.out.println("In beginning of try");
               String thisMachineIP =
                        new BufferedReader(
                                new InputStreamReader(
                                        new URL("https://api.ipify.org").openStream())).readLine();
               System.out.println("Retrieved this machine IP");
           } catch(IOException e) {
               e.printStackTrace();
           } catch(Exception e) {
               e.printStackTrace();
           }

When I run the exe and print to a log file, it only prints "In beginning of try" and the window closes. It doesn't crash with "the program has stopped working"; the window just closes with no explanation. The log file does not contain the prints from either of the catches. Everything works as expected when I run the jarfile directly.

EDIT I attempted to use LibGDX's HTTP methods instead:

            Net.HttpRequest httpGet = new Net.HttpRequest(Net.HttpMethods.GET);
            System.out.println("After httpGet object creation");
            httpGet.setUrl("https://api.ipify.org");
            System.out.println("After set httpGet url");

            Gdx.net.sendHttpRequest(httpGet, new Net.HttpResponseListener() {
                @Override
                public void handleHttpResponse(Net.HttpResponse httpResponse) {
                    System.out.println("in handleHttpResponse()");
                }

                @Override
                public void failed(Throwable t) {
                    System.out.println("in failed()");
                }

                @Override
                public void cancelled() {
                    System.out.println("in cancelled()");
                }
            });
            System.out.println("After Gdx.net.sendHttpRequest()");

Once again, the jarfile itself runs fine. The exe never prints "After Gdx.net.sendHttpRequest()", and also doesn't print any of the messages in the HttpResponseListener.

0

There are 0 best solutions below