I’m trying to use the java.security.KeyPairGenerator via JRuby 1.7 RC2 as per the following code:
require 'java'
kp = java.security.KeyPairGenerator.getInstance("RSA")
puts kp #java.security.KeyPairGenerator$Delegate@45f177b
However, when I try to call initialize i.e.
kp.initialize(2048)
I get the following exception:-
TypeError: no public constructors for #<Class:0x7efe8e7a>
Any suggestions would be very much appreciated.
This is a little clash between ruby’s
initializemethod (which is a constructor in the ruby world) and the method in this particular java class.Normally, one does not call
initializeon a ruby class (you callnewinstead), but anyway it seems to be causing some confusion for the interpreter.If you look at the output of
kp.methodsyou’ll see JRuby has added ainitialize__methodto circumvent the clash (note double underscore).Try this:
Another technique is to use java_method, which is also useful when the interpreter is having trouble picking the right overload.
E.g.: