0

So I'm trying to dynamically create a folder inside the web pages folder. I'm making a game database. Everytime a game is added I do this:

public void addGame(Game game) throws DatabaseException {
    em.getTransaction().begin();        
    em.persist(game);   
    em.getTransaction().commit();
    File file = new File("C:\\GameDatabaseTestFolder");
    file.mkdir();      
}

So everything works here. The file get's created. But I want to create the folder like this:

public void addGame(Game game) throws DatabaseException {
    em.getTransaction().begin();        
    em.persist(game);   
    em.getTransaction().commit();
    File file = new File(game.getId()+"/screenshots");
    file.mkdir();      
}

Or something like that. So it will be created where my jsp files are and it will have the id off the game. I don't understand where the folder is created by default.

thank you in advance, David

David Maes
  • 574
  • 6
  • 23

1 Answers1

0

It's by default relative to the "current working directory", i.e. the directory which is currently open at the moment the Java Runtime Environment has started the server. That may be for example /path/to/tomcat/bin, or /path/to/eclipse/workspace/project, etc, depending on how the server is started.

You should now realize that this condition is not controllable from inside the web application.

You also don't want to store it in the expanded WAR folder (there where your JSPs are), because any changes will get lost whenever you redeploy the WAR (with the very simple reason that those files are not contained in the original WAR).

Rather use an absolute path instead. E.g.

String gameWorkFolder = "/path/to/game/work/folder";
new File(gameWorkFolder, game.getId()+"/screenshots");

You can make it configureable by supplying it as a properties file setting or a VM argument.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555