Code:
public String sign(String msg, BigInteger d) throws IOException {
ECDSASigner signer = new ECDSASigner();
X9ECParameters params = SECNamedCurves.getByName("secp256k1");
ECDomainParameters ecDomainParameters = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
ECPrivateKeyParameters ecPrivateKeyParameters = new ECPrivateKeyParameters(d, ecDomainParameters);
signer.init(true, ecPrivateKeyParameters);
BigInteger[] sigs = signer.generateSignature(DatatypeConverter.parseHexBinary(msg));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DERSequenceGenerator seq = new DERSequenceGenerator(byteArrayOutputStream);
seq.addObject(new DERInteger(sigs[0]));
seq.addObject(new DERInteger(sigs[1]));
seq.close();
byte[] bytes = byteArrayOutputStream.toByteArray();
return DatatypeConverter.printHexBinary(bytes);
}