Java String .isBlank() works in Eclipse/Powershell but not in Jar

94 Views Asked by At

I have a question. I have written a program that sends me an SMS as soon as something specific does not appear in a log. However, it is about this method:

private static void sendWarningSMS() {
        try {
            String computerName = InetAddress.getLocalHost().getHostName();
            URL url = new URL("https://gatewayapi.com/rest/mtsms");
            log("sendWarningSMS","we've reached sendWarningSMS. Right before if statement");
            log("sendWarningSMS","Value of mobileNumberFromConfigFile: "+mobileNumberFromConfigFile);
            if(mobileNumberFromConfigFile != null && !mobileNumberFromConfigFile.isBlank()) {
                HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
                con.setDoOutput(true);
                DataOutputStream os = new DataOutputStream(con.getOutputStream());
                os.writeBytes(
                        "token=*****"
                                + "&sender=" + URLEncoder.encode("SC Warning", "UTF-8")
                                + "&message=" + URLEncoder.encode("Censored " +computerName + " Censored", "UTF-8")
                                + "&recipients.0.msisdn=" + mobileNumberFromConfigFile
                        );
                os.close();
            
                int responseCode = con.getResponseCode();
                log("SMS","SMS Warnung wurde an " + mobileNumberFromConfigFile + " gesendet. Antwort von GatewayAPI:" + responseCode);              
                }else {
                    log("ERROR","SMS konnte nicht gesendet werden. Es wurde keine Telefonnummer im Config File erfasst.");
                }
        } catch (MalformedURLException e) {
            log("SMS","Fehler in der sendWarningSMS Methode"+e);
        } catch(IOException e1) {
            log("SMS","Fehler in der sendWarningSMS Methode"+e1);
        }
        log("sendWarningSMS","end of sendWarningSMS method.");
    }

Why is here

if(mobileNumberFromConfigFile != null && !mobileNumberFromConfigFile.isBlank())

in Eclipse and Powershell false and is executed, but in an exported JAR true and is not executed? If I remove

&& mobileNumberFromConfigFile.isBlank()

it also works in the JAR.

The config file is an .ini file and is read by Java with properties. The config file looks like this:

numberToSend=12345678
name=
location=

It is not a big problem, but I am interested in the why.

Many thanks for your help. Greetings,

I have logged everything and everywhere. The number is taken correctly from the config file. We also get into the method up to the if block, which is no longer executed. The else block is also not executed and therefore not logged.

Log when application is running in Eclipse/Powershell with && !.isBlank:

[Sat Jan 13 02:38:37 CET 2024] Client: Erfolgreich zum Server verbunden.
[Sat Jan 13 02:38:37 CET 2024] sendWarningSMS: weve reached sendWarningSMS. Right before if statement
[Sat Jan 13 02:38:37 CET 2024] sendWarningSMS: Value of mobileNumberFromConfigFile: 12341234565
[Sat Jan 13 02:38:38 CET 2024] SMS: SMS Warnung wurde an 12341234565 gesendet. Antwort von GatewayAPI:200
[Sat Jan 13 02:38:38 CET 2024] sendWarningSMS: end of sendWarningSMS method.

Log when application is startet from exportet JAR with && !.isBlank:

[Sat Jan 13 02:45:43 CET 2024] Client: Erfolgreich zum Server verbunden.
[Sat Jan 13 02:45:43 CET 2024] sendWarningSMS: weve reached sendWarningSMS. Right before if statement
[Sat Jan 13 02:45:43 CET 2024] sendWarningSMS: Value of mobileNumberFromConfigFile: 12341234565

Log when application is startet from Eclipse without && !.isBlank:

[Sat Jan 13 02:50:37 CET 2024] Client: Erfolgreich zum Server verbunden.
[Sat Jan 13 02:50:37 CET 2024] sendWarningSMS: weve reached sendWarningSMS. Right before if statement
[Sat Jan 13 02:50:37 CET 2024] sendWarningSMS: Value of mobileNumberFromConfigFile: 12341234565
[Sat Jan 13 02:50:37 CET 2024] SMS: SMS Warnung wurde an 12341234565 gesendet. Antwort von GatewayAPI:200
[Sat Jan 13 02:50:37 CET 2024] sendWarningSMS: end of sendWarningSMS method.

Log when application is startet from exportet JAR without && !.isBlank:

[Sat Jan 13 02:52:29 CET 2024] Client: Erfolgreich zum Server verbunden.
[Sat Jan 13 02:52:29 CET 2024] sendWarningSMS: weve reached sendWarningSMS. Right before if statement
[Sat Jan 13 02:52:29 CET 2024] sendWarningSMS: Value of mobileNumberFromConfigFile: 12341234565
[Sat Jan 13 02:52:29 CET 2024] SMS: SMS Warnung wurde an 12341234565 gesendet. Antwort von GatewayAPI:200
[Sat Jan 13 02:52:29 CET 2024] sendWarningSMS: end of sendWarningSMS method.
0

There are 0 best solutions below