How to Create a Self Signed Certificate using Java Keytool

Java Keytool LogoSecuring your Java application with an SSL certificate can be extremely important. Fortunately, it is (usually) quite simple to do using Java Keytool. Most situations require that you buy a trusted certificate, but there are many cases when you can generate and use a self signed certificate for free.

When to Use a Keytool Self Signed Certificate

An SSL certificate serves two essential purposes: distributing the public key and verifying the identity of the server so users know they aren't sending their information to the wrong server. It can only properly verify the identity of the server when it is signed by a trusted third party. A self signed certificate is a certificate that is signed by itself rather than a trusted authority. Since any attacker can create a self signed certificate and launch a man-in-the-middle attack, a user can't know whether they are sending their encrypted information to the server or an attacker. Because of this, you will almost never want to use a self signed certificate on a public Java server that requires anonymous visitors to connect to your site. However, self signed certificates have their place:

Never use a self signed certificate on an e-commerce site or any site that transfers valuable personal information like credit cards, social security numbers, etc.

  • An Intranet. When clients only have to go through a local Intranet to get to the server, there is virtually no chance of a man-in-the-middle attack.
  • A Java development server. There is no need to spend extra cash buying a trusted certificate when you are just developing or testing an application.
  • Personal sites with few visitors. If you have a small personal site that transfers non-critical information, there is very little incentive for someone to attack the connection.

Just keep in mind that visitors will see a warning in their browsers (like the one below) when connecting to a server that uses a self signed certificate until it is permanently stored in their certificate store.

Apache Self signed Certificate Error in Firefox

Generate a Self Signed Certificate using Java Keytool

Now that you know when to use a Keytool self signed certificate, let's create one using a simple Java Keytool command:

  1. Open the command console on whatever operating system you are using and navigate to the directory where keytool.exe is located (usually where the JRE is located, e.g. c:\Program Files\Java\jre6\bin on Windows machines).
  2. Run the following command (where validity is the number of days before the certificate will expire):
    keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
  3. Fill in the prompts for your organization information. When it asks for your first and last name, enter the domain name of the server that users will be entering to connect to your application (e.g.

Java Keytool Self Signed Certificate command

This will create a keystore.jks file containing a private key and your sparklingly fresh self signed certificate. Now you just need to configure your Java application to use the .jks file. If you are using Tomcat, you can follow our Tomcat SSL Installation Instructions.

For more information on creating a Java Keytool Self Signed Certificate, see the following links:

Originally posted on Sat Oct 30, 2010

Comments (23)

  1. kalpana:
    Mar 03, 2014 at 02:14 AM

    I am working in an application where https page is shown in an iframe.So i want to skip the SSL browser warning page? Can some one suggest how to do that?

  2. Mal:
    Jan 29, 2014 at 08:08 AM

    I have created a selfsigned certificate using your command and can see in the keystore -list a selfsigned certicate. but when i use jarsigner to apply to my .Jar file it gives me error: Certificate chain not found for keystore.jks. keystore.jks must reference a valid keystore key entry containing a private key and corresponding pulbic key certificate chain.

  3. jojo:
    Jan 15, 2014 at 08:28 AM

    keytool error Reply #2 on : Tue March 06, 2012, 23:16:06 Hi, I followed your step exactly the way you typed but I get an keytool error when I tried to create a self-signed cert. The error shows: "keytool error: keystore.jks

  4. Puneet:
    Oct 10, 2013 at 12:07 AM

    Hi, i have tried generating keys but getting the below exception. "keytool error: keystore.jks (Access is denied)" Do i need the admin rights for generating the keys. TIA Puneet Pahuja

  5. Suma Gopalakrishna:
    Jul 19, 2013 at 05:48 AM

    If you are using a build.xml and running ant, you would have mentioned a property tag with a file attribute = or any other filename where you store some variables to be read by ant. Now in the add the following lines,\\suma\\MYAPP\\VER1.0\\keystore.jks key.alias=selfsigned where should just give absolute path name of the .jks file generated and alias should be assigned whatever you mentioned for -alias in the keytool command. keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048

  6. zac:
    Jun 03, 2013 at 12:14 AM

    Hi, The the .jks file was created. What's now? Youu have got this question already, but i did not founf teh answer for it. Thanks

  7. Marco:
    May 08, 2013 at 04:09 AM

    I have done all your steps. I have created the .jks file but now i don't now what to do. Can you explain please?

    Apr 18, 2013 at 12:15 AM

    is there any code available for generate keys via key tool by java code... If it is available, please share it..

  9. Dellanio Alencar:
    Mar 15, 2013 at 10:21 AM

    Anyone can tell me if it's possible to sign a Java Applet with selfsign certificate?

  10. pavol:
    Feb 11, 2013 at 05:06 AM

    during generating the self-signed root certificate, key pair is generated. but not in separate fiels but somewhere part of certificate. Is is possible to export both keys? thanks Pavol

  11. Kumar:
    Dec 13, 2012 at 02:53 PM

    Enter key password for (RETURN if same as keystore password): keytool error: D\tmp\keystore.jks (The system can not find the path specified)

  12. Joe:
    Jul 24, 2012 at 07:43 PM

    Good stuff, thanks. Can you explain why neither my Firefox nor Explorer browsers are encrypting the info (as reported by FF's Page Info)? I created the cert and changed the Tomcat server.xml and that seems to be working fine. I was under the impression that a self-signed cert would allow me to send data encrypted, granted from an untrusted site, but if I "accept the risks", shouldn't the data be encrypted?

  13. Shelly:
    Jun 05, 2012 at 12:15 PM

    I need to generate the secured links that can be emailed to the customer. When user clicks on the email link it should perform a Single Sign-On. Could you advise how I can send encrypted URL to customer via email using SSL? I need it urgently. Please help!

  14. Robert:
    May 13, 2012 at 06:46 AM

    No .crt file is created. The certificate and the key are both contained in the .jks file.

  15. Shawn:
    May 11, 2012 at 03:59 AM

    OK so ive tried this over and over and my key is nowhere to be found. I used jre7\bin to store it and after I created it, its not showing up in that directory? Nor can It be found when I search windows.

  1. 1
  2. 2

Allowed tags: <b><i><br>Add a new comment: