I need to use an existing Kotlin encryption file in a java class. While trying to do that getting NoClassDefFoundError Exception. The import statements are proper no issues with that.
If I run this application from intellij its working, but when I run via terminal with ./gradlew clean build its throwing the error.
java.lang.NoClassDefFoundError: Could not initialize class com.xxx.xxx.Email
Location of java file - src/main/java/com/company/team/project/service
Location of kotlin file - src/main/java/com/company/team/project/service/encryption
Kotlin class
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import org.springframework.security.crypto.encrypt.Encryptors
import org.springframework.security.crypto.encrypt.TextEncryptor
private const val EMAIL_ENCRYPTION_SECRET = "EMAIL_ENCRYPTION_SECRET"
class Email constructor(emailValue: String) : Comparable<Email> {
val emailId: String = emailValue
val encryptedValue = encrypt(emailId)
val decryptedValue = decrypt(encryptedValue)
override fun compareTo(other: Email): Int = emailId.compareTo(other.emailId)
@JsonValue
override fun toString(): String = emailId
override fun hashCode(): Int = emailId.hashCode()
companion object {
private var aesCipher: TextEncryptor
init {
val encryptionKey = System.getenv(EMAIL_ENCRYPTION_SECRET)
aesCipher = Encryptors.queryableText(encryptionKey, encryptionKey)
}
@JsonCreator
fun of(value: String?): Email? = if (value == null) null else Email(value)
fun value(e: Email?): String? = e?.emailId
fun encrypt(plaintextEmail: String): String = "EEA:${aesCipher.encrypt(plaintextEmail)}"
fun decrypt(encryptedEmail: String): String {
if (!encryptedEmail.startsWith("EEA:")) {
throw IllegalArgumentException("Encrypted text does not have expected EEA prefix.")
}
val cipherTextWithoutEEA = encryptedEmail.replaceFirst("^EEA:".toRegex(), "")
return aesCipher.decrypt(cipherTextWithoutEEA)
}
}
}
Java class
@Component
@AllArgsConstructor
@Slf4j
public class FindUserData extends TaskHandler<Void> {
--- some codes and other methods ---
private ExecutionResult doExecute(ExecutionContext<Void> context) {
--- properly working code ----
Email email = new Email(userData.get().toString());
String emailId = email.getEncryptedValue();
log.info(context.format("Found user data: %s", emailId));
--- Other working code ---
}
}