skip to Main Content

Im trying to run my java application on Ubuntu server. I enter absolute path to read that file but it produces and error.

The code which is supposed to read that file is below.

private static final String CREDENTIALS_FILE_PATH = "/home/dockeradmin/credentials.json";


InputStream in = application.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }

And the error I get:

java.io.FileNotFoundException: Resource not found: /home/dockeradmin/credentials.json

When I tried to read file with same path using BufferedReader everything worked perfectly.

Like this:

BufferedReader br = new BufferedReader(new FileReader(new File("/home/dockeradmin/credentials.json")));

So my question is, what is the difference between these two and how could I solve my current problem?

2

Answers


  1. Chosen as BEST ANSWER

    This line caused trouble.

    InputStream in = application.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    

    I fixed this by replacing that, with that

    InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
    

  2. The reason of the error is that getResourceAsStream is used to locate file on classpath and it can’t be used for locating file on file system.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search