0

So, my I have a method that saves some data in a properties file but something weird happens. See, lets say I have the JAR file on desktop. If I open it directly from there (double click, etc) the properties file is saved in the desktop, as should be. However, if you drag the JAR to the Windows start list and open it from there, the properties file will be saved in the System32 folder.

Here is the method:

private void saveAncientsData() {
        Properties prop = new Properties();
        OutputStream output = null;

        try {

            output = new FileOutputStream("ancients.data");
            File file = new File("ancients.data");
            // set the properties value
            for (int x = 0; x < currentLvlSpinnerFields.size(); x++) {
                prop.setProperty(ancientNames[x], currentLvlSpinnerFields.get(ancientNames[x]).getValue().toString());
            }

            // save properties to project root folder
            prop.store(output, null);
            JOptionPane.showMessageDialog(this, "Data successfully saved in \n\n" + file.getCanonicalPath(), "Saved", JOptionPane.INFORMATION_MESSAGE);
        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

Would appreciate any help, since I am clueless.

Thanks in advance!

Moko
  • 441
  • 4
  • 9
  • 15
  • 1
    @FastSnail It actually works properly when dragging the SHORTCUT to the start list. How come the "issue" happens whe dragging the JAR file instead? – Moko Jul 18 '15 at 04:48
  • do you want to create property file in desktop when you add your programe to start list ? – Madhawa Priyashantha Jul 18 '15 at 04:53
  • "the properties file is saved in the desktop, **as should be.**" * I'm not sure this is a correct assumption. Your code specifies the target filename without any path information. So why do you assume that it should be saved to the same directory as the jar file? Can you point to java documentation that says this will be the case? – EJK Jul 18 '15 at 04:55
  • @Moko your property file should be created where your jar file exists.if you create shortcut your actual file exist in desktop .so property file will be created in desktop.can you tell how you drag it to windows start list?to which folder did you drag it? – Madhawa Priyashantha Jul 18 '15 at 05:02
  • @EJK Well, I said "as should be" because it is supposed to be saved in the same location as the JAR, and the JAR is in the desktop. Sorry, didnt mean to sound "confusing". – Moko Jul 18 '15 at 05:08
  • @FastSnail I dragged the JAR (the program generated JAR file, not a shortcut) to the Windows start button and added it to the list. – Moko Jul 18 '15 at 05:10
  • @Moko - I was not saying that your text was 'confusing'. I was saying that you might be looking at this the wrong way. You have stated "it is supposed to be saved in the same location as the JAR". Are you expecting that to just happen? If so, that is not a valid expectation. If you are asking how to make that happen, then that is a different question. – EJK Jul 18 '15 at 05:19
  • @EJK Got it. Yeah, I was expecting to it to save in the JAR's location. Now I can ask haha, how do I make so it is ALWAYS saved in the same location as the JAR? – Moko Jul 18 '15 at 05:22

1 Answers1

1

according to your code you haven't give path of property file to create in desktop.

output = new FileOutputStream("ancients.data");

so your property file will be created in same directory where your jar file exists .

but if you run this .jar file from a parent process your jar file created in the directory where that parent process exists.

i guess when windows starts a specific process exist in win32 directory execute start-up programs .i think it's userinit.exe . so your prop file will be created in System32 directory .

if you want property file to create in desktop you can put your jar file in desktop and add a shortcut to .jar or you can give full-path to your desktop like

output = new FileOutputStream(System.getProperty("user.home") + "/Desktop/"+"ancients.data");

edit

to understand this problem

1) create a folder named example in desktop.and then create 2 folders path1 and path2 .then add .jar to path1 folder

enter image description here

2) double click jar in path1 .and a property file will be created in path1 as you expected .

enter image description here

3) delete property file.open command prompt in path2 . To run Prop.jar file in path1 . type call "pathtodesktop/example/path1/Prop.jar" hit enter.

.property file will be created in path2 instead of path1 that's what happening in your case.

enter image description here

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • I am gonna try that, thanks!. Also, is there a way to get the JARs directory?, so I can save it in whatever location the JAR is instead of the desktop. – Moko Jul 18 '15 at 05:42
  • @Moko yes you can get "from where jar file get executed" .look at this question `http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java` but when your jar file executed by other program you will get parent process path instead of actual path .you can try that.i will give you small example about this problem – Madhawa Priyashantha Jul 18 '15 at 05:48
  • Interesting. So, if I got this right, when I run the JAR from the start list (dragged in the JAR first to there), it runs as a "subprocess" of Windows thus creating the file in system32? – Moko Jul 18 '15 at 07:04
  • @Mokoi think your .jar file executed by [userinit.exe](https://www.google.com/search?q=userinit.exe&ie=utf-8&oe=utf-8) .and it exist in `system32` – Madhawa Priyashantha Jul 18 '15 at 07:07