0

In debugging I found that my current way of storing a database's files (embedded derby database) didn't work when running the executable .jar file that's compiled by NetBeans 8.0.

Simply put, I need a way of getting the application to find the directory the .jarfile is being run from, and to set up the embedded DB database in the same directory.

In case it helps, the current way I get it to 'work' (within the IDE) is as follows:

public final void setDBSystemDir() throws UnsupportedEncodingException, SQLException{
        String path = DataBase.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String decodedPath = URLDecoder.decode(path, "UTF-8");
        String systemDir = decodedPath + "/listData/";
        System.setProperty("derby.system.home", systemDir);
}

Any pointers welcome.

Eric
  • 373
  • 1
  • 3
  • 14
  • Did you read http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file?rq=1? – jordan May 19 '14 at 04:40

1 Answers1

0

You can use the following code to get the path,

CodeSource source = DataBase.class.getProtectionDomain().getCodeSource();
File file = new File(source.getLocation().toURI().getPath());
String dir = file.getParentFile().getPath();
swgkg
  • 184
  • 5