Reputation: 35
I've already finished some game I was working on, however when the user wants to save and load any of his previous games, it works fine while running in netbeans when I need to save/write, but when i'm running it from the JAR generated by netbeans it works only if the application never closes, I mean, it loses all the data when it closes, and I'm guessing is because it isn't writing or loading the files i get this error when running it from console. There's just a single ArrayList inside that txt file with every user's info it is also doing the same with the Records file that it just stores the top 10 players of the game.
java.io.FileNotFoundException: saveFiles\Users\Usuarios.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at SaveNLoad.SaveUsers.saveUsers(SaveUsers.java:26)
at CrossyMain.MainMenu$ButtonListening.actionPerformed(MainMenu.java:309)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Is this because some path problem? in netbeans it seems like that path is the absolute path when loading it from there, however I don't think it is the same when loading it from the JAR file, any advice would be highly appreciated!
Here are the methods that manage the file write/read operations for Users
public static void saveUsers (ArrayList<User> usersToSave){
try { //Used to Save Users
File path = new File("saveFiles/Users/Usuarios.txt");
System.out.println(path.getAbsolutePath());
FileOutputStream writeUsers = new FileOutputStream(path);
ObjectOutputStream userStream = new ObjectOutputStream(writeUsers);
userStream.writeObject(usersToSave);
userStream.close();
} catch (IOException ex) {
Logger.getLogger(SaveUsers.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void loadUsers (){
try {
File path = new File("saveFiles/Users/Usuarios.txt");
System.out.println(path.getAbsolutePath());
FileInputStream readUsers = new FileInputStream(path);
ObjectInputStream userStream = new ObjectInputStream(readUsers);
ArrayList<User> loaded = (ArrayList<User>) userStream.readObject();
User.setUsers(loaded);
userStream.close();
} catch (IOException | ClassNotFoundException ex) {
System.out.println("File Not Created");
}
}
Read/Write operations for the Records file (Note sort() is just a function that orders the ArrayList with a comparator)
private static final String RECORDSFILE = "saveFiles/Records/Records.txt";
public static void loadScoreFile() {
try{
inputStream = new ObjectInputStream(new FileInputStream(RECORDSFILE));
scores = (ArrayList<Score>) inputStream.readObject();
sort();
inputStream.close();
} catch (IOException | ClassNotFoundException e) {
}
}
public void updateScoreFile() {
try {
outputStream = new ObjectOutputStream(new FileOutputStream(RECORDSFILE));
sort();
outputStream.writeObject(scores);
outputStream.close();
} catch (IOException e) {
}
}
Again, it works fine in netbeans but it is not working in the JAR file, if you guys need any more code from what i've already posted lemme know! but those are the functions that handles the in/out operations for the 2 files records and users! any suggestion would be highly appreciated!
Upvotes: 0
Views: 1310
Reputation: 310868
It works in Netbeans because it isn't using the JAR file, it's using the file system.
You can't write into files inside a running JAR file. Rethink your requirement.
Upvotes: 1