Get Off My Lawn
Get Off My Lawn

Reputation: 36351

Why doesn't directory exist?

I am trying to get the location of a temporary directory that Netbeans creates when I click "Run" to run my project, it creates a file such as run1017994493 within the dist dir.

Then almost immediately after it builds it runs this code:

String fileloc = SQLite.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String location = fileloc.substring(0, fileloc.lastIndexOf("/"));
File f = new File(location);
System.out.println(location);
System.out.println(f.exists());

The Output:

/C:/Users/rnaddy/Documents/NetBeansProjects/Phantom%20Browser/dist/run1017994493
false

Why is it saying that the directory doesn't exist?

I believe it is causing the following 2 lines to break:

Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:" + location + "/phantom.db");

Upvotes: 0

Views: 69

Answers (1)

Dmitry Ginzburg
Dmitry Ginzburg

Reputation: 7461

Firstly, you need to remove heading "/", then all the occurences of "%20"or similar things should be replaced with corresponding characters:

String loc = ...
if (isWindows()) {
    //remove heading slash
    loc = loc.replaceAll("^/", "");
}
loc = URLDecoder.decode (loc, "UTF-8"); // this will replace all characters like "%20" with something like " "

Upvotes: 1

Related Questions