Search code examples
javaconcurrencyjarioswingworker

Issue with running JAR from Desktop, but not from command line or Eclipse


I am running into a peculiar issue (peculiar for me anyways) that seems to happen in a SwingWorker that I use for saving the result of another 'SwingWorker' thread as a tab-delimited file (just a spreadsheet of data).

Here is the worker, that initializes and declares an object which organizes the data and writes each table row to a file (using BufferedWriter):

// Some instance variables outside of the SwingWorker:
// model: holds a matrix of numerical data (double[][])
// view: the GUI class

class SaveWorker extends SwingWorker<Void, Void> {

        /* The finished reordered matrix axes */
        private String[] reorderedRows;
        private String[] reorderedCols;
        private String filePath;  // the path of the file that will be generated

        public SaveWorker(String[] reorderedRows, String[] reorderedCols) {

            // variables have been checked for null outside of the worker
            this.reorderedRows = reorderedRows;
            this.reorderedCols = reorderedCols;
        }

        @Override
        protected Void doInBackground() throws Exception {

            if (!isCancelled()) {
                LogBuffer.println("Initializing writer.");
                final CDTGenerator cdtGen = new CDTGenerator(
                        model, view, reorderedRows, reorderedCols);

                LogBuffer.println("Generating CDT.");
                cdtGen.generateCDT();

                LogBuffer.println("Setting file path.");
                filePath = cdtGen.getFilePath();         // stops inside here, jumps to done()
                LogBuffer.println("Path: " + filePath);
            }
            return null;
        }

        @Override
        protected void done() {

            if (!isCancelled()) {
                view.setLoadText("Done!");
                LogBuffer.println("Done saving. Opening file now.");

                // need filePath here to load and then display generated file
                visualizeData(filePath);

            } else {
                view.setReorderOngoing(false);
                LogBuffer.println("Reordering has been cancelled.");
            }
        }
    }

When I run the program from Eclipse, this all works perfectly fine. No issues whatsoever. Now I know there have been tons of question on here that are about Eclipse running fine while the runnable JAR fails. It's often due to not including dependencies or referring to them in the wrong way. But what's weird is that the JAR also works completely fine when it's being started from command line (Windows 8.1):

java -jar reorder.jar

Et voilà, everything as expected. The CDTGenerator will finish, write all the matrix rows to a file, and return the filePath. With the filePath I can subsequently open the new file and display the matrix.

In the case of double-clicking the JAR on my desktop, where I placed it when creating it from Eclipse, this is where the program will let me know that stuff happens. I get the error message I created for the case of filePath == null and using some logging I closed in on where the CDTGenerator object stops executing its method generateCDT() (Eclipse debugger also won't reproduce the error and do everything as planned).

What the log shows made me think it's an issue with concurrency, but I am actually leaning against that because Eclipse and command line both run the code fine. The log just tells me that the code suddenly stops executing during a loop which transforms double values from a matrix row (double[]) to Strings to be stored in a String[] for later writing with BufferedWriter.

If I use more logging in that loop, the loop will stop at a different iterator (???).

Furthermore, the code does work for small matrices (130x130) but not for larger ones (1500x3500) but I haven't tested where the limit is. This makes it seem almost time dependent, or memory.

I also used jVisualVM to look at potential memory issues, but even for the larger matrices I am on ~250MB which is nowhere near problematic regarding potential OutOfMemoryExceptions.

And finally, the last potential factor I can think of: Generating the JAR 'fails' due to some classpath issues (clean & rebuild have no effect...) but this has never been an issue before as I have run the code many many times using the 'broken' JAR and execute from Desktop.

I am a real newbie to programming, so please point in some direction if possible. I have tried to find logged exceptions, logged the values of variables, I am checking for null and IndexOutOfBound issues at the array where it stops executing... I am at a complete loss especially because this runs fine from command line.


Solution

  • It looks like the problem had to see with the java versions installed in OP's computer. They checked the file extensions and the programs associated to each one in order to see if it was the same java version as executed from Eclipse and the command line.

    Once they cleaned older java versions the jar started to work by double-clicking it :)