1

I have seen other questions on accessing resource in jar topic but my question is a bit different. I need to use .jks files in my code for a client i am writing to access a webservice. This code works perfectly in my system even when i package it as a jar but when i send jar to another person it gets SSL error. I understood it was unable to access the files as I hardcoded in my code. This was the same even when I added the files in the jar.

Can anybody tell me how to access this file in jar. I do not need to use file reader or buffered reader here as i just need to give path of the file.

Thanks in advance

 SslConfigurator sslConfig = SslConfigurator.newInstance() .trustStoreFile("AAAWSKeyStore.jks") .trustStorePassword("password") .keyStoreFile("AAAWSKeyStore.jks") .keyPassword("password") .securityProtocol("TLS"); 

The error thrown here is

 jjava.lang.IllegalStateException: Cannot find key store file ".\AAAWSKeyStore.jks ". at org.glassfish.jersey.SslConfigurator.createSSLContext(SslConfigurator .java:679) at GuiAAAWS.sendPost(GuiAAAWS.java:220) at GuiAAAWS.OkActionPerformed(GuiAAAWS.java:198) at GuiAAAWS.access$2(GuiAAAWS.java:193) at GuiAAAWS$3.actionPerformed(GuiAAAWS.java:88) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener 
3
  • Are your key store and trust store pointing at the same file? Commented Nov 19, 2014 at 16:43
  • What is the description of the SSL error thrown? Commented Nov 19, 2014 at 16:46
  • @Priyesh added the description of error Commented Nov 19, 2014 at 17:05

1 Answer 1

2

It is not as simple as giving a path to the file, because when it is packed in the jar it is not a real file. You can supply a KeyStore object to the SslConfigurator, however, and that is more flexible than specifying a path, as it can be initialized from any input source, including a resource in a jar:

KeyStore keyStore = KeyStore.getInstance("jks"); try (InputStream in = YourClassName.class.getResourceAsStream("AAAWSKeyStore.jks")) { keyStore.load(in, "password".toCharArray()); } SslConfigurator sslConfig = SslConfigurator.newInstance() .trustStore(keyStore) .keyStore(keyStore) .securityProtocol("TLS"); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You for response. But this didnt work I am getting error javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
@shbolise That means the key store and/or trust store could not be read. It is difficult to diagnose remotely, but make sure that if you have a separate key store and trust store, that you are loading both. Make sure the password is correct, that the jks file(s) are actually in the jar, and have the correct path relative to your class. To get the path right, you can put the files in the same package as the class, or put them in the root of the jar and add a '/' in front of the resource name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.