I am trying to access a file in another directory outside my java project.
The structure is like this:
.
|-- resources
| |-- api
| | |-- data.json
|-- src
| |-- java
| | |-- .classpath
| | |-- pom.xml
| | |-- src
My java project is located at ./src/java/
My resource directory is located at ./resources/api/
I have modified the .classpath of src/java/ like so:
<classpathentry kind="lib" path="./../../resources/api"/>
And I am trying to access the resource in the following ways:
String filename = "data.json";
URL file_url = this.getClass().getClassLoader().getResource(filename);
System.out.println("URL IS "+file_url);
//outputs null
file_url = this.getClass().getResource(filename);
System.out.println("URL IS "+file_url);
//outputs null
InputStream in = this.getClass().getResourceAsStream(filename);
System.out.println("InputStream IS "+in);
//outputs null
file_url = this.getClass().getResource("/api/"+filename);
System.out.println("URL IS "+file_url);
//outputs null
file_url = this.getClass().getResource("api/"+filename);
System.out.println("URL IS "+file_url);
//outputs null
file_url = this.getClass().getResource("resources/api/"+filename);
System.out.println("URL IS "+file_url);
//outputs null
file_url = this.getClass().getResource("/resources/api/"+filename);
System.out.println("URL IS "+file_url);
//outputs null
file_url = this.getClass().getResource("./"+filename);
System.out.println("URL IS "+file_url);
//outputs null
file_url = this.getClass().getResource("/"+filename);
System.out.println("URL IS "+file_url);
They are all returning null. Anyone see anything I did wrong here?
==========================
SOLUTION
Pretty silly, but I just needed to edit my Maven pom.xml to add the directory to the classpath, as instructed here.
I updated my pom.xml like this:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>./../../resources/api/</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
Apparently eclipse uses .classpath and maven uses pom.xml.
src/main/java, notsrc/javaas shown in your question.