George
George

Reputation: 103

Getting java.io.FileNotFoundException: FileName (No such file or directory)

I get the error "java.io.FileNotFoundException: AuthKey_7RHM5B8NS7.p8 (No such file or directory)", the file is clearly in my directory and I am using the relative path for the file. Here is my projects directory.

Project directory Image

final ApnsClient apnsClient = new ApnsClientBuilder()
    .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
    .setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File("AuthKey_7RHM5B8NS7.p8"),
            "GL87ZNESF6", "7RHM5B8NS7"))
    .build();

Upvotes: 1

Views: 8865

Answers (2)

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

You should not use the ApnsSigningKey.loadFromPkcs8File method but instead use the loadFromInputStream method.

The reason is that you are using a resource - and if you build a JAR file from your code, as is often done, your resource will be inside the JAR file and you will not be able to get a File object that points to it.

Code:

InputStream in = getClass().getResourceAsStream("/AuthKey_7RHM5B8NS7.p8");

final ApnsClient apnsClient = new ApnsClientBuilder()
    .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
    .setSigningKey(ApnsSigningKey.loadFromInputStream(in, "GL87ZNESF6", "7RHM5B8NS7"))
    .build();

in.close();

Upvotes: 1

Mak
Mak

Reputation: 1078

As you are trying to fetch file from resource folder hence you need to specify path for that.

File file = new File(getClass().getResource("/AuthKey_7RHM5B8NS7.p8").getFile());

or to get the URL

URL res = getClass().getClassLoader().getResource("AuthKey_7RHM5B8NS7.p8");
File file = Paths.get(res.toURI()).toFile();
String absolutePath = file.getAbsolutePath();

Upvotes: 3

Related Questions