Search code examples
javajarfilewriter

Filewriter not working when .jar File is transfered to another Computer


I created a relatively simple GUI app using Netbeans that would access a mySQL DB and create tables for another GUI program. This program would ask the user for mySQL username and password, store these credentials in a notepad in encrypted form so that it can be decrypted and read later, then proceed with creating the database components.

My program runs perfectly on my PC, the problem happens when I try to transfer the program to another computer. When I run the .jar file that can be found on the dir folder in Netbeans, the database components are created normally, but the notepad that is supposed to be created in the C: drive does not appear.

This causes my other program to not work because it needs the mySQL username and password to create a connection with the database. I used these codes for the file writer:

private static PrintWriter pw;
private static final String LOCATION = "C:\\Users\\DONOTDELETE.txt";

public static void Log(String user, String pass) {
    try {
        pw = new PrintWriter(new FileWriter(LOCATION));
        pw.println(Encryption.encrypt(user));
        pw.println(Encryption.encrypt(pass));
        pw.flush();
    } catch (Exception e) {
    }
}

Thanks in advance for any replies, for now I'll try to tinker with it a bit.

UPDATE: I have solved the problem! Thanks to everyone's suggestions, I determined that the problem was simply a problem with Admin access. AFAIK, normally you cannot run a .jar file as administrator so I opted for changing the .jar file into a .exe one using the program:

Launch4j

Thanks again and for the help :)


Solution

  • There could be couple of reason for this behavior. Firstly,

    } catch (Exception e) {
    }
    

    You have just eaten up the exception, as a result you cannot determine what exactly went wrong. If you know you cannot handle the exception, just do not catch it. Let the exception get raised, it will help you know what exactly went wrong.

    Secondly, you may be facing permission issues while writing or reading from the specified directory. As you have eaten up the exception, nothing gets displayed on the console. Try printing the stack-trace of the exception, it may help you get a gist of the problem.