Can't find (connect to) database using relative path

I am having trouble accessing my database on the server. I am using this piece of code to try and connect.

String currentDir = new File(".").getAbsolutePath();
String currdir = System.getProperty("user.dir");
System.out.println("EPIC currentDir = " + currentDir);
System.out.println("EPIC currdir = " + currdir);
String builder = "jdbc:sqlite:"+ currentDir +"dbname";


Now ... printlns tell me that:

currentDir = /usr/local/cpanel/base/.
currdir = /usr/local/cpanel/base


and I know it will give SQLException because my db is in .. / WEB -INF / lib / dbname So no wonder I get this error message:

java.sql.SQLException: opening db: '/usr/local/cpanel/base/.dbname': Permission denied


So my question is, what's after the base and how does this relate to my directory (where does the servlet run from)? I don't have SSH to search for it (rolls his eyes) and if anyone can shed some light on this topic I'd be great! Thank you in advance!

Edit: strange things

I am testing it all with these commands.

String currentDir = new File("dbname").getAbsolutePath();
String currdir = System.getProperty("user.dir");
System.out.println("EPIC currentDir = " + currentDir);
System.out.println("EPIC currdir = " + currdir);
System.out.println("EPICER  = " + this.getClass().getResource("dbname").toString());
System.out.println("EPICEST!  = ");


this is the result:

EPIC currentDir = /usr/local/cpanel/base/dbname


<<this gives me "Permission Denied"

EPIC currdir = /usr/local/cpanel/base


<<this gives me "Permission Denied"



<<that's ok .. but wait .. NO EPICER ???

System.out.println("EPICER  = " + this.getClass().getResource("dbname").toString());


DO NOT PRINT! what? is my log filtered to prevent the path from being watched? Also, none of these paths work :(

<<<--- UPDATE --- β†’ β†’

Didn't get printed because "this" was returning null and I was actually getting NPE on that line; thus no println. Spirit.


Ok, so I was going to get it all wrong, and I am posting this just in case some other poor guy is about to spend three days like me.



"this" was returning null because what? SURPRISE, there is no ServletContext so



also returned null (which I experimented later on)

I found out from other resources on the internet that the way I was doing it was wrong because every time I redeploy the WAR file the DB will be overwritten .. so I created a small directory in my / home / username / called dbfiles and used the commands:

String dir= "jdbc:sqlite:/home/username/dbfiles/dbname.db";
connection = DriverManager.getConnection(dir);


and it works !!

Thanks everyone


source to share

2 answers

user.dir is the directory of the system user. If you want to find a file inside your app war use ServletContext.getRealPath ()



To access a database from an applet using JDBC (read-only):

I made a connection similar to the one here but read



I put



sample.db went to /myprojectname/resource/sample.db, with the resource being in the same directory (myprojectname) as / myprojectname / src /

Hope this helps someone



All Articles