#"exit code 1" when calling a getter in a main method

1 messages · Page 1 of 1 (latest)

solid halo
#

In this following main method, I am calling the VersionManager.getVersionsFile() getter, which retrieves a "versions.json" file from a url:

  public static void main(String[] args) {
    ELookAndFeel.setLookAndFeel();

    LauncherConfig config = LauncherConfig.load();
    if (config.getClientToken() == null) {
      ConfigManager.initConfig();
    }

    VersionManager.getVersionsFile();

    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        LauncherFrame.frameInstance = new LauncherFrame();
        LauncherFrame.frameInstance.setVisible(true);
      }
    });
  }

Unfortunately, it completes the download and then exits the program with error code 1. I don't know why or how. It gives me zero information. No exceptions, no nothing.

Here's the getter for reference (the String values are placeholders for now):

  public static void getVersionsFile() {
    File workingDirectory = DirectoryManager.getWorkingDirectory();
    File versionsDirectory = new File(workingDirectory, "versions");
    if (!versionsDirectory.exists() && !versionsDirectory.mkdir()) {
      throw new RuntimeException("Failed to create versions directory");
    }

    File versionsFile = new File(versionsDirectory, "versions.json");
    if (!versionsFile.exists()) {
      versionsFile = FilesManager.downloadFile(FilesManager.VERSIONS_JSON_URL,
          versionsFile.getAbsolutePath());
    }

    try (FileReader fr = new FileReader(versionsFile)) {
      JSONTokener tokener = new JSONTokener(fr);
      JSONObject versions = new JSONObject(tokener);

      String id = versions.getString("id");
      String port = versions.getString("port");
      System.out.println("Version: " + id + " (" + port + ")");
    } catch (IOException e) {
      throw new RuntimeException("Can't read versions file", e);
    } catch (JSONException e) {
      throw new RuntimeException("Failed to parse versions file", e);
    }
  }
dark patrolBOT
#

This post has been reserved for your question.

Hey @solid halo! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

desert crest
#

have you tried debugging or adding prints to see where it stops working

solid halo
#

well, the print-out for the TRY statement does not work.

desert crest
#

does it reach that try though?

#

(also i don't think you should be reducing every error to just RuntimeException)

solid halo
#

documentation does not exactly tell me what exceptions go where in what context

desert crest
#

uh, IOException/JSONException?

solid halo
#

i'm trying to avoid "throws" declarations in methods

#

i heard it's bad practice

desert crest
#

why?

#

no?

#

do you know about what checked exceptions semantically mean

solid halo
#

no

#

how is this relevant to exit code 1

desert crest
desert crest
desert crest
desert crest
solid halo
solid halo
desert crest
#

maybe add a log before the call in main?

solid halo
#

it's in the main

#

just after config init

#

without the call, it works

#

with the call, it exits 1

desert crest
# solid halo it's in the main

buddy... im not a compiler... i have no idea if maybe LauncherConfig.load() or ConfigManager.initConfig() is potentially exiting early

solid halo
#

let me just hide the init call entirely

#

and maybe look and feel too

desert crest
#

if you don't like debugging then good luck

solid halo
#

still loves to exit 1 without any other calls

#

so the method is faulty

#

it works but it's faulty

solid halo
#

After doing a bit of an exception checking here and there for additional conditions, I altered some of the methods and fixed the issue.

First, here's the downloadFile method that I altered:

  public static void downloadFile(String url, File path) {
    try (InputStream is = RequestManager.requestHttpGet(url).getInputStream()) {
      FileUtils.copyInputStreamToFile(is, path);
    } catch (IOException ioe) {
      throw new RuntimeException(String.format("Failed to download file from %s", url), ioe);
    }
  }
#

Then I went and made my getter be void instead of returning File:

  public static void getVersionsFile() {
    File workingDirectory = DirectoryManager.getWorkingDirectory();
    File versionsDirectory = new File(workingDirectory, "versions");
    if (!versionsDirectory.exists() && !versionsDirectory.mkdir()) {
      throw new SecurityException("Failed to create versions directory");
    }

    File versionsFile = new File(versionsDirectory, "versions.json");
    if (!versionsFile.exists()) {
      FilesManager.downloadFile(FilesManager.VERSIONS_JSON_URL, versionsFile);
    }
  }
#

and now it works as intended

#

alternatively, since it uses the requestHttpGet method, I went and altered that a bit as well:

#
  public static HttpsURLConnection requestHttpGet(String url) {
    SSLContext context;
    try {
      context = SSLContext.getInstance("TLSv1.2");
    } catch (NoSuchAlgorithmException nae1) {
      try {
        context = SSLContext.getDefault();
      } catch (NoSuchAlgorithmException nae2) {
        throw new RuntimeException("Failed to get default SSL context", nae2);
      }
    }
    try {
      context.init(null, null, null);
    } catch (KeyManagementException kme) {
      throw new RuntimeException("Failed to initialise SSL context", kme);
    }

    try {
      HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
      connection.setSSLSocketFactory(context.getSocketFactory());
      connection.setRequestMethod("GET");
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection.setRequestProperty("Cache-Control", "no-cache");
      connection.setRequestProperty("Pragma", "no-cache");
      connection.setUseCaches(false);
      return getHttpResponse(connection);
    } catch (IOException ioe) {
      throw new RuntimeException(String.format("Can't connect to %s", url), ioe);
    }
  }
#

and yes, I made it so that if it can't find TLSv1.2 protocol, it will try retrieving the default one, and if that can't budge, it will finally throw an exception during runtime, since that method is only used during runtime, and not during compilation.