Sample code for 30+ languages & platforms
Xbase++

Add Trusted Certificate to JKS

See more Java KeyStore (JKS) Examples

Adds a trusted certificate to a Java keystore file.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJks
LOCAL cJksPassword
LOCAL cJksPath
LOCAL oCert
LOCAL cAlias

nSuccess := 0

//  This requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oJks := CreateObject("Chilkat.JavaKeyStore")

cJksPassword := "secret"
cJksPath := "/myJksTrustedCerts/cacerts.jks"

//  Load the Java keystore from a file.
nSuccess := oJks:LoadFile(cJksPassword, cJksPath)
IF (nSuccess != 1)
    ? oJks:LastErrorText
    oJks:destroy()
    RETURN
ENDIF

oCert := CreateObject("Chilkat.Cert")

//  The cert's LoadFrommFile method can load a certificate from
//  virtually any format.  It will automatically determine the format
//  and load appropriately.
nSuccess := oCert:LoadFromFile("/certFiles/myNewTrustedCert.pem")
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oJks:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  The alias can be anything.  It's basically just a label 
//  used within the JKS associated with the entry.  It should
//  be unique among aliases within the JKS file.
cAlias := "habanero"

nSuccess := oJks:AddTrustedCert(oCert, cAlias)
IF (nSuccess != 1)
    ? oJks:LastErrorText
    oJks:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Write the JKS containing the new certificate.
nSuccess := oJks:ToFile(cJksPassword, cJksPath)
IF (nSuccess != 1)
    ? oJks:LastErrorText
    oJks:destroy()
    oCert:destroy()
    RETURN
ENDIF

? "Added a trusted certificate to the JKS."

oJks:destroy()
oCert:destroy()