Relative path to connect to ms accdb - java

I'm using NetBeans and this is my project folder:
My executable is in the 'dist' folder. About the code,in my connection class I used the following string:
"jdbc:ucanaccess://.\\mydb.accdb"
So i used a relative path from dist folder.
This works the first time i execute the program and correctly connect to database, but the next times i have the following exception:
"given file does not exist: .\mydb.accdb"
Obviously the file exists and it works the first time as i wrote. I'm also sure i'm doing something wrong. Can someone help me? Thank you

I've solved. I was using a relative path starting from the dist folder while I had to use a relative path from the folder where is the connection class.

Related

Get relative path from jar file

I tried to reach a special path in Ubuntu, relative to the current jar file.
In Windows it is working without any problem:
String jarPath = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(jarPath+"/../../configurationFile.xml");
However, in Linux I always get the jar file but I cannot step back two directories to the configurationFile.xml
/some/directory/where/xml/is/located/xyz.jar/configurationFile.xml: Not a directory
However, if I do
pwd /some/directory/where/xml/is/located/xyz.jar/../../
it works without any problems.
What I am doing wrong here?
I cannot figure it out.
Use only directories in your path.
After you determined the path to your jar file, extract the path to its directory and use directories only.

How to get the absolute path of the executed jar?

How do I get the location of the executed jar? I found a lot of solutions but none of them work for me. When I run them in my IDE everything is fine. As soon as I build a jar file and run it with java -jar myapp.jar the output is like /.../myapp.jar!/foo/bar
I will run the code in myapp.jar - not in any library.
Location of jar: /home/me/dev/myapp/myapp.jar
Expected output: /home/me/dev/myapp/
I don't want the working directory as I would get with System.getProperty("user.dir");
Why I want to do this:
I want to store and load a file beside the actual jar. Like
/home/me/bin/myapp/myapp.jar
/home/me/bin/myapp/license.key
I want to avoid storing the file into some generic folder like System.getProperty("user.home");. And I don't want to store the file within the jar file.
java.nio.file.Paths.get(".").toAbsolutePath() will return absolute path to current directory.
I use something along these lines:
[YourClass].class.getProtectionDomain().getCodeSource().getLocation().getPath()
Regards

Reference a Text File with relative path in Java with the IntelliJ IDEA

I have a program that reads numbers from a .txt file. My problem is where to place this file, or how to reference it with a relative path to make this file accessible without using the absolute path.
When you try to open a file, it takes your current working path.
For example this working tree:
Project
|->src
| |-->MyClass.java
| |-->MyFile1.txt
|->res
|->files
|-->MyFile2.txt
You can use new File("MyFile1.txt"); for MyFile1.
or
new File("./res/files/MyFile2.txt"); for MyFile2.
You need to start your path from src. "src/your/path/file.txt". See my answer here
If you have a multi-project setup, it may not be obvious what the "root" directory is for a relative path. In that case, open the Terminal tab in your IntelliJ and see what directory it lands in. That is the "root" directory. Your relative path should start there.

Tomcat returns not the same path of current directory as JUnit tests returns

First of all excuse me for my ugly English.
I have some problem with loading properties file into object of java.lang.Properties in my web application. Maybe it will be appear as stupid question, but I can't resolve it myself.
So I have this code snippet:
String path = Paths.get("").toAbsolutePath().toString();
When I run tests, value of variable path was equals to absolute path of my project root directory, as I expected (in my case it is "D:\ProgsJava\persons-web"). But when I have started my app in Eclipse using Tomcat, and one of servlets has invoked this code snippet the value of variable path was equals to absolute path of directory in which IDE has been installed (in my case it is "D:\soft\Eclipse"). I have fix this problem by entering absolute path of properties file but it is not correct solution. All dependencies have been injected by maven. Could you explain me what is the magic with pathes?
Hi If you want to get the path of the project directory , try the following code snippet
System.getProperty("user.dir");

add sqlite DB to executable JAR file

I am using JAVA (with eclipse juno) and try to create an executable JAR file which include sqlite DB file.
I've try to get connection to the DB by this line:
DriverManager.getConnection("jdbc:sqlite:"+DataController.class.getResource("test.sqlite").getPath())
The DataController is a class that located where the sqlite located.
and i keep get an error:
java.sql.SQLException: invalid database address
Does someone can help and give step by step instructions about how to include sqlite DB inside an executable JAR file?
Apparently sqlite-jdbc can open resources on it's own. Per this thread https://groups.google.com/forum/?fromgroups=#!topic/xerial/Oayzj5nrJGk, add :resource to the path. So try the following:
DriverManager.getConnection("jdbc:sqlite::resource:package/test.sqlite");
or depends on version of sqlite
DriverManager.getConnection("jdbc:sqlite::resource:package/test.db");
Replacing package with the '/' separated path to the package this file is in.
Be aware that it will actually copy the file to a tmp directory.-
The ::resource way is right. And these explanations will help if you are using ::resource but still get errors like resource database.db not found: java.net.MalformedURLException: no protocol: database.db like verdana.
Most common anwsers are:
DriverManager.getConnection("jdbc:sqlite::resource:path/to/database.db")
However the path/to/database.db must be the exact path(the Path in Real File System but not in Jar) to your jar file.
I recommend to use getClass().getResource():
DriverManager.getConnection("jdbc:sqlite::resource:" +
getClass().getResource("/path/to/db/in/the/jar/file"))
NOTE:the /path/to/db/in/the/jar/file part must start with /
I'm not sure if this has changed in recent JDBC versions, but after fiddling with it for about an hour and getting various exceptions (MalformedURLException and "Database has been closed"), I found that the following worked for me:
DriverManager.getConnection("jdbc:sqlite::resource:file:/path/to/database.db")
The :file: portion seems to be missing from other answers and I couldn't get it to work without it.
Also, note that the
/path/to/database.db
is the absolute path starting from the root of the .jar file, rather than a normal resource root.
jdbc:sqlite::resource:notes_app.db
worked for me. My database(notes_app.db) was in the root of generated jar file.
public class Database {
public static Connection con () throws SQLException {
String url = "jdbc:sqlite::resource:notes_app.db";
return DriverManager.getConnection(url);
}
}
My notes_app.db is in the root of generated jar. I'm developing with maven and notepadd++

Categories

Resources