6

I have been reading the same questioned that were asked here and on Quora, however, I still don`t understand why I cannot access the file that is located in the 'src/main/resources' folder. Everything works when I specify relative path manually "src/main/resources/config/serverConf.xml"

Project structure:

src/main/java/"project related folders"
src/main/resources/config/serverConf.xml

And the main class:

public class Main{

    public Main(){
        File file = new File(this.getClass().getResource("config/serverConf.xml").getPath());
        if(file.exists())
            System.out.println("Yes");
        else
            System.out.println("No");
    }

    public static void main(String[] args){
        Main main = new Main();
    }
}
12
  • 1
    stackoverflow.com/q/2593154/1531971 Commented May 31, 2018 at 19:05
  • 1
    Resource is not a file - it won't work. Your next question would be like "why it works from eclipse but does not work from JAR" Commented May 31, 2018 at 20:04
  • Antoniossss could you explain how to make the program execute as I tried what experienced users answered and nothing worked. Also, I am not using Maven as a built tool... Commented May 31, 2018 at 20:13
  • While ever File can be represented as an URL, not all URLs can be represented as a File. Try printing the URL returned from .getResource( and see if it contains a !. If so, that's one of the times. My question for you is why does this program need a File as opposed to an URL or (otherwise) just access to the resource? Commented Jun 1, 2018 at 3:26
  • "Antoniossss could you explain.." Tip: Add @Antoniossss (or whoever, the @ is important) to notify the person of a new comment. Commented Jun 1, 2018 at 3:28

4 Answers 4

2

The users Nikolas and Antoniossss already answered the question. Just to give a short explanation in case it helps. From your directory structure I guess you're using Maven. When you build the project using Maven, Maven copies all the folders and files to the target/classes folder which is the root of the class path. So in your case the following should work:

File file = new File(this.getClass().getResource("/config/serverConf.xml").getPath());

But if you want to use the relative path as in your original code, you should create the config folder in the directory where the Main.java class is located and put the serverConf.xml there. Then the following should work too:

File file = new File(this.getClass().getResource("config/serverConf.xml").getPath());

That said, the better way is to put the configuration files under src/main/resources folder.

Sign up to request clarification or add additional context in comments.

1 Comment

You do know that this will not work if OP eg. build JAR from that?
2
"config/serverConf.xml"

Should be

"/config/serverConf.xml"

2 Comments

This does not work as I receive NullPointerException... File file = new File(this.getClass().getResource("/config/serverConf.xml").getPath());
Resource is not a file
1

Usually the leading / will be your go-to with filepaths, but I was able to just give it the file name and it recognized it just fine for me.

File file = new File(this.getClass().getResource("serverConf.xml").getPath());

2 Comments

You don`t specify the config directory in which serverConf.xml is placed
You do know that this will not work if OP eg. build JAR from that?
0

The url supplied to getResource() method must be relative to your classpath. Thus if you had to run this from the command line, you would need to supply the path to your resources folder for this to work, i.e:

java Main -cp src/main/resources/

If you're using an IDE, there's usually a configuration for adding the classpath

Comments

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.