0

I am getting the above error in this particular line of code:

KeyFactory keyFactory=keyFactory.getInstance(keyAlgorithm);

        EncodedKeySpec privateKeySpec=new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey1=keyFactory.generatePrivate(privateKeySpec); 

To be precise this is the error I get:

GeneratePublicPrivateKeys.java:35: error: non-static method generatePublic(KeySp
ec) cannot be referenced from a static context
PublicKey publicKey1=KeyFactory.generatePublic(publicKey
Spec);

Note: I have gone through these posts
“Non-static method cannot be referenced from a static context” error
non static method cannot be referenced from a static context
and some more and I have tried the solutions provided there,but no they do not solve my problem

What should I do?
Thankyou in advance:)

1
  • keyFactory.generatePublic, not KeyFactory.generatePublic.
    – thejh
    Apr 5, 2013 at 13:10

2 Answers 2

0

You need to change

KeyFactory.generatePublic(...)

to be:

keyFactory.generatePublic(...)

At the moment your code is trying to call the generatePublic method as if it were static (ie associated with the KeyFactory class, rather than with an object of that type). You need to call this method on a specific instance of the class - presumably the keyFactory object refered to in your first code snippet.

0

Try to change this line:

PublicKey publicKey1=KeyFactory.generatePublic(publicKey
Spec);

to

PublicKey publicKey1=keyFactory.generatePublic(publicKey
Spec);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.