Joseph
Joseph

Reputation: 2726

Replacing FileInputStream with getResourceAsStream

I am currently reading in a public key file using the following code:

    // Read Public Key.
    File filePublicKey = new File(path + "/public.key");
    FileInputStream fis = new FileInputStream(path + "/public.key");
    byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
    fis.read(encodedPublicKey);
    fis.close();

However, I wish to include the key files in with my jar. I have dragged the key files into my project in eclipse and I am trying to load the public key using the following to replace what is above:

    InputStream is = getClass().getResourceAsStream( "/RSAAlgorithm2/public.key" );
    byte[] encodedPublicKey = new byte[(int) 2375];
    is.read(encodedPublicKey);
    is.close();

However I keep getting a NullPointerException.

java.lang.NullPointerException at RSA.LoadKeyPair(RSA.java:122) at RSA.main(RSA.java:31)

Is this because I am incorrectly loading in the file? Can files be dragged into eclipse and loaded like this or is it a requirement to have them seperate from the JAR?

Upvotes: 2

Views: 1920

Answers (2)

erickson
erickson

Reputation: 269637

With this name, you have to put the "public.key" file in the RSAAlgorithm2 package. That means in your "jar" file, you should see an entry named "RSAAlgorithm2/public.key".

Upvotes: 0

siegi
siegi

Reputation: 5996

Check if is is null after doing getResourceAsStream. If it is, the resource has not been found. In this case check the path to the file, it is relative to your classpath. I don't know your project setup but I would try to simply use "/public.key"

Upvotes: 1

Related Questions