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?
Check if
isisnullafter doinggetResourceAsStream. 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"…