Cannot load jks file from class path

I created a JKS file with RSA public and private keys. When I load this file using an external path (for example, c: /file.jks), the program runs like a charm. However, if I try to load the same file from the classpath, I got this exception:

java.io.IOException: Invalid keystore format 

This is the code used to download jks:

  KeyStore keyStore = KeyStore.getInstance("JKS"); InputStream stream=this.getClass().getResourceAsStream("/lutum.jks") ; keyStore.load(stream,passe); 

The only difference is that I use FileInputStream with the full path when loading from the outside. What am I doing wrong?

+8
classpath java-8 encryption jks
source share
2 answers

In general, your solution should work temporarily.

What are these provisions? Make sure your resource folder is in your classpath. If you are not sure, add it to the -cp flag passed to java when your program is running, or if you use Eclipse or some other IDE, make sure it is listed as a member of the class path for this project,

Next up, https://stackoverflow.com/a/414829/ Although the way you use the getResourceAsStream () method is valid (including / at the beginning of the file name causes the class resource loader to defer to the ClassLoader method), it may be less confusing to use ClassLoader directly. Another good example can be found here .

So firstly, make sure your resource folder is explicitly part of the class path. Secondly, prefer the following construction to search for a resource:

 InputStream stream= this.class.getClassLoader().getResourceAsStream("lutum.jks"); 

Note the missing / from file name. This is due to the fact that ClassLoader will automatically start searching in the β€œroot of the project”, and the slash is likely to cause problems (if, for example, you deploy to JBoss or Tomcat, which are likely to be interpreted as a class loader as the path of the relative file system relative ways).

Hope this helps. If not, please comment on more details about your project, and I will change my answer accordingly.

+5
source share

I suspect that the two keystores actually do not match and that the keystore on the classpath is somehow damaged.

Try comparing the two keystores. Just read the files in an array of bytes with something like this:

  public static byte[] streamToByteArray(InputStream is) throws IOException { ByteArrayOutputStream tmp = new ByteArrayOutputStream(); int b = is.read(); while (b > -1) { tmp.write(b); b = is.read(); } tmp.flush(); return tmp.toByteArray(); } 

And then compare them as follows:

  InputStream cpStream = this.getClass().getResourceAsStream("/lutum.jks"); InputStream fileStream = new FileInputStream("c:/file.jks"); byte[] cpBytes = streamToByteArray(cpStream); byte[] fileBytes = streamToByteArray(fileStream); if (Arrays.equals(cpBytes, fileBytes)) { System.out.println("They are the same."); } else { System.out.println("They are NOT the same."); // print the file content ... } 
+2
source share

All Articles