I have a java code that automatically create a file for the user after login. It works properly when I run it in netbeans/intellij. However when I compiled it into a jar file, the program works but the file is not created. Can someone please help me?
Here is my code:
private void createTxtFile(){ String loc = new File("").getAbsolutePath(); String dir = loc+"/src/lists/"+user+"-"+pass+".txt"; try { File myObj = new File(dir); if (myObj.createNewFile()) { System.out.println("File created: " + myObj.getName()); } else { JOptionPane.showMessageDialog(this, "User already have an existing list file."); } } catch (IOException e) { JOptionPane.showMessageDialog(this, "An error occured in creating file for your list.", "Error",JOptionPane.ERROR_MESSAGE); } }
srcin your code,srcwon't exist once the program is exported. You also can't create new directories/files that get stored in the jar at runtime, instead, you need to create them on the disk itselfFileobject. You would have to use that class to create those directories. Take the advice and create a directory for your app off HOME and then the file in it