I'm doing a Java project for school where we have to use MS Access to store the data. I've managed to get the database up and running, however i can't seem to find how to make the link to the database relative. I've searched the web and tried everything I can think of but nothing seems to work. Anybody here that can help?
Here's how I've put the link now:
private static final String DATABASE_URL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Badlapje\\Dropbox\\workspace\\FOOP\\Group62RISK\\Risk.accdb";
Which I then invoke as follows:
connection = DriverManager.getConnection(DATABASE_URL);
What I want to do is make the link relative, so all members of our team can use the same link.
The problem is in the access database file location: C:\\Users\\Badlapje\\Dropbox\\workspace\\FOOP\\Group62RISK\\Risk.accdb. Just make sure to place this file in a folder that everyone has in its computer like C:\\school-project\\Risk.accdb.
Another solution could be to set your file inside your Java project inside a source package and then load the file absolute path using Java code (adapted from this answer):
package edu.proj.res;
public class Main {
public static String getDatabaseLocation() {
return Main.class.getClassLoader().getResource("edu/proj/res/database.txt").toString();
}
public static String changeToWindowsPath(String path) {
return path.replace("file:/", "").replaceAll("/", "\\\\");
}
public static void main(String[] args) {
System.out.println(changeToWindowsPath(getDatabaseLocation()));
}
}
Having a project structure:
ConsoleTests
- src
- edu.proj.res
+ database.txt
+ Main.java
Program output:
C:\workspace\ConsoleTests\bin\edu\proj\res\database.txt
Related
I want to use RDkit in my java web project. It happens like this "I want to use RDkit in my java web project. It happens like this".
It is so weird.I wirte a sample java application like
public static void main(String[] args) {
System.loadLibrary("GraphMolWrap");//I have put the .dll file in the path
String smiles = "CN1CCN2C(C1)C1=C(CC3=C2C=CC=C3)C=CC=C1";//my pom.xml import the jar
RWMol m1 = RWMol.MolFromSmiles(smiles);
}
It runs without any bug!
But When I put its code in my springboot project.It happens like title.
I know where's is the bug.It is about the maven package for springboot.If run with the command,it will run formally.
I'm trying to load a custom made DLL library using java. It takes Input as string and returns output as Integer. I'm using Intellij as IDE.
My code is:
package com.company;
public class Main {
private native Integer IsMalware(String filePath);
public static void main(String[] args) {
System.out.println(new Main().IsMalware("D:\\git.txt"));
}
static {
System.loadLibrary("AMSI");
}
}
File Structure:
Output/Error:
The java version i use is:
[EDIT]
After adding the DLL to library path It shows another error
Can someone help me with this??. Thanks in advance.
I have a project structure like this:
main-directory
src
code
images
javaFile1.java
test.java
The compiled project structure is like this:
out
artifacts
production
main-directory
code
images
javaFile1
test
images is directory with many images.
Now the code for test looks like this:
public void doSome(){
System.out.println(this.getClass().getResource("./images/image1.png"));
}
public static void main(String[] args){
test test = new test();
test.doSome();
}
I get the file as file:
--location-- /main-directory/out/production/main-directory/code/images/image1.png
But when I run this.getClass().getResource("./images/image1.png") in javaFile1, it returns null. I am not able to understand that why does javaFile1 not find the resource while test does even though they are in the same directory. I tried some answers from SO, but none of them worked.
I would appreciate any help.
For some reason it fixed when I added "/code/images/image1.png" instead of "./images/image1.png". It might be that resources need the parent directory to find it.
I ran into library loading problems after creating a jar from my code via maven. I use intelliJ idea on Ubuntu. I broke the problem down to this situation:
Calling the following code from within idea it prints the path correctly.
package com.myproject;
public class Starter {
public static void main(String[] args) {
File classpathRoot = new File(Starter.class.getResource("/").getPath());
System.out.println(classpathRoot.getPath());
}
}
Output is:
/home/ted/java/myproject/target/classes
When I called mvn install and try to run it from command line using the following command I'm getting a NullPointerException since class.getResource() returns null:
cd /home/ted/java/myproject/target/
java -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
same for calling:
cd /home/ted/java/myproject/target/
java -Djava.library.path=. -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
It doesn't matter if I use class.getClassLoader().getRessource("") instead. Same problem when accessing single files inside of the target directory instead via class.getClassLoader().getRessource("file.txt").
I want to use this way to load native files in the same directory (not from inside the jar). What's wrong with my approach?
The classpath loading mechanism in the JVM is highly extensible, so it's often hard to guarantee a single method that would work in all cases. e.g. What works in your IDE may not work when running in a container because your IDE and your container probably have highly specialized class loaders with different requirements.
You could take a two tiered approach. If the method above fails, you could get the classpath from the system properties, and scan it for the jar file you're interested in and then extract the directory from that entry.
e.g.
public static void main(String[] args) {
File f = findJarLocation("jaxb-impl.jar");
System.out.println(f);
}
public static File findJarLocation(String entryName) {
String pathSep = System.getProperty("path.separator");
String[] pathEntries = System.getProperty("java.class.path").split(pathSep);
for(String entry : pathEntries) {
File f = new File(entry);
if(f.getName().equals(entryName)) {
return f.getParentFile();
}
}
return null;
}
I've got the problem that the following code snip returns null:
System.out.println(Logic.class.getResource("effects\\newball.wav"));
I have a source folder in my project called effects. in this folder there's the referred file. I think there's a syntax error... Because THE FILE IS THERE. I must refer in this way (means with getResource) to my file because I will export it as jar later.
Thank you
Your effect directory should be a direct child of the src dir. Also in which case, you need a / to start the string path. So you would need this
System.out.println(Logic.class.getResource("/effects/newball.wav"));
ProjectRoot
src
effect
newball.wav
What I normally do using an IDE is just create a new package and name it whatever I want the file to be - in your case "effect". It's easier that way.
UPDATE
"I did it exatly so, but it still returns null"
It works fine for me
package stackoverflow;
public class Test {
public static void main(String[] args) {
System.out.println(Test.class.getResource("/effects/stack_reverse.png"));
}
}
Output: file:/C:/Android/workspace/StackOverflow/bin/effects/stack_reverse.png
Resource paths should use forward slashes, regardless of the filesystem on the machine you are using: try "effects/newball.wav"
See http://docs.oracle.com/javase/7/docs/technotes/guides/lang/resources.html (Under "resources, Names, and Contexts -- "The name of a resource is independent of the Java implementation; in particular, the path separator is always a slash (/).")