1

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.

1
  • if you are working with maven, the folder for your production sources should be src/main/java , not src/java as shown in your question. Commented Oct 19, 2016 at 6:02

1 Answer 1

1

Maybe the magic can be done with:

getClass().getResource("/api/data.json");

or (based on the use)

 getClass().getResourceAsStream("/api/data.json");
Sign up to request clarification or add additional context in comments.

5 Comments

Still returning null :/ I updated my question with the added tests. I think something is wrong with how I am adding "resource/api" to the classpath, because I think if it WAS in the classpath, this strategy would be working
@Kayvar Which IDE are you using?
I was building using maven and just realized it probably isn't using .classpath.. lol whoops! Works now! I added the classpath stuff to my pom.xml
maven never uses .classpath, only eclipse. From time to time eclipse (better: the eclipse maven plugin) works not correct when it handles maven projects
Thanks @JimHawkins! Good to know hah

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.