Following code does not work for ECDSA keys (with secp256r1):
AsymmetricKeyParameter bprv =
PrivateKeyFactory.createKey(key.getEncoded());
encodedKey = OpenSSHPrivateKeyUtil.encodePrivateKey(bprv);
It produces a private key which cannot be used neither by openssh, nor by jssh.
However, Topaco's answer to https://stackoverflow.com/questions/79262968/how-to-convert-a-ecprivatekey-to-a-pem-encoded-openssh-format works all right.
OpenSSHPrivateKeyUtil.encodePrivateKey does work fine for Ed25519.
It doesn't work for RSA either, but I'm not sure whether it's supposed to, as RSA is supposed to be packed in a different PEM wrapper (which has -----BEGIN PRIVATE KEY----- instead of -----BEGIN OPENSSH PRIVATE KEY-----).