Search code examples
javascriptbase64encodesha256rhino

TypeError: Cannot call property getEncoder in object


I am trying to encode byte array using java package Base64 in a javascript. Javascript engine is Rhino.

Below is the error in logs.

java.util.concurrent.ExecutionException: javax.script.ScriptException: TypeError: Cannot call property getEncoder in object [JavaPackage java.util.Base64]. It is not a function, it is "object". 

Below is the snippet of code.

var md = java.security.MessageDigest.getInstance('SHA-256');
md.update(salt);
var sha256Bytes = []
sha256Bytes=md.digest(new java.lang.String(givenPassword).getBytes('UTF-8'));
var sha256Hash = java.lang.String(java.util.Base64.getEncoder().encode(sha256Bytes));

I am not able to figure out the issue here. Also, the package java.util.Base64.* is imported.


Solution

  • I imported class java.util.Base64$Encoder. Earlier I was importing it in java.util.Base64.Encoder . Not sure how $ makes a difference comparing to a '.' . But , I assume that my application imports static class in this way.