0

I've been asking a lot lately, and all my posts are about the same problem, but in different stages and with different possible answers (because the specific problem is different than the previous one, but closely related). So, i'm sorry if it looks like i'm repeating my question. In fact, i'm not. I've been searching on Google and here, but none of the answers seem to solve my problem, or i'm getting them wrong.

Well, my files hierarchy is:

+ MyGame
  + build.xml
  + src
  + ThisIsWhereEclipsePutsTheXMLFile
  + build
    + manyFoldersWithClassFiles
    + aSpecialFolderWhereTheEntryPointIs

    + ThisIsWhereIWantMyXMLfileToBe.xml
    + game.jar

My problems is basically this: My program is supposed to save it's status to a file, and then read that file. This file should be outside the .jar file (there is only one), in the same folder. The file can exist or not. If it exists, it should overwrite it.

In my previous question, I asked about how to read an xml file that's in the same directory. The answer i got actually solves my problem when i'm using it from Eclipse.

But i need to create a .jar file with my whole program, and i need my program to crear such xml file whenever i ask it to do it.

My save() is like this, and it seems to be working when i run it on Eclipse, but won't work when i run it executing my .jar file:

public void save(Game game) throws IOException{
    Document doc = DocumentHelper.createDocument();
    doc.add(game.save());
    File save=null;
    save = new File("save.xml");

    FileWriter writer = new FileWriter(save);
    doc.write( writer);
    writer.close();
}

And this is how i get the informacion back from the file:

public Game getinfofromxml() throws IOException{
    Game game;
    SAXReader reader = new SAXReader();
    try{
        URL fileWithData= new File( "save.xml" ).toURI().toURL();
        Document document = reader.read(fileWithData);
        Element alreadySavedGame= document.getRootElement();
        game= getGameSaved(alreadySavedGame);
    }catch(DocumentException ex){
        throw new IOException();
    }
    return game;
} 

Again, this works from Eclipse, but this won't work when i run it from my jar file. From Eclipse, the xml file is created in the MyGame folder, but not in the folder i have my .jar. When i execute my .jar, no XML is created at all.

Now, i've been reading that this might have something to do with the classpath. So, let me tell you how I compile it:

1) i run Ant, which makes the build directory. It doesn't create the .jar automatically. 2) I create the Manifest.txt file, where i write:

 Main-Class: aSpecialFolderWhereTheEntryPointIs.MyMainClass
 Class-Path: .

3) i create the .jar file on the cmd: jar cfm myGame.jar Manifest.txt *

So, i don't think i'm making too many mistakes there...

May Ant have something to do with it? Any idea? Thanks beforehand (and sorry for my English)

Community
  • 1
  • 1
FDrico
  • 37
  • 2
  • 7

2 Answers2

4

Stop thinking about the 'current directory' and put the config. in user.home as discussed in this answer. For a long time OS manufacturers have been telling us not to put config. settings in the application directory.

See also this answer to "How can a java program use files inside the jar for read and write?" for more tips.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

The way you write and read file is using relative path of the current start directory... It depends on where you started the jvm and where you expect it to write and read your file.

Wins
  • 3,420
  • 4
  • 36
  • 70
  • 1
    Yes, in general you should probably use an absolute path that controls where you want your file to be. You can allow the user to control this path through a property or something. – Francis Upton IV Jul 18 '12 at 04:23
  • If i double clic the .jar file.. isn't the jvm started in that directory? – FDrico Jul 18 '12 at 04:35