Creating a KeyStore using java

I have the below code and am trying to generate a keystore using keytool, but all it does is continues to wait.  Am i going about this the wrong way or is the code wrong?  Any help always appreciated

   String stringPassword = "mypassword";
   String stringKeyStore = "./mykeyStore.keystore";
   File fileKeyStore = new File(stringKeyStore);
   if (fileKeyStore.exists() == false)
   {
     System.out.println("Creating keystore...");

     String [] arstringCommand = new String []
     {
       System.getProperty("java.home") + File.separator + "bin" + File.separator + "keytool",
       "-genkey",
       "-keyalg", "RSA",
       "-keysize", "1024",
       "-alias", "myKey",
       "-validity", "365",
       "-keystore", stringKeyStore,
       "-keypass", stringPassword,
       "-storetype", "JKS",
       "-storepass", stringPassword
     };

     Process process = Runtime.getRuntime().exec(arstringCommand);
     process.waitFor();
LVL 5
tbboyettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
may be waiting for input
or the output buffer is blocked. you need to read any output sent to stdout/stderr
0
tbboyettAuthor Commented:
I'm assuming I read this input and output by the following?  

OutputStream outputstream = process.getOutputStream();
InputStream inputstream = process.getInputStream();
InputStream inputstream2 = process.getErrorStream();

This may sound kinda dumb but what do I need to do with the inputs and outputs or am I understand wrong?
0
CEHJCommented:
You could use the following:

RunAsync.main(arstringCommand);

//=========================================================




import java.io.*;

/**
 *  Description of the Class
 *
 * @author     CEHJ
 * @created    23 February 2004
 */
public class RunAsync {

      /**
       *  Description of the Method
       *
       * @param  args  Description of the Parameter
       */
      public static void main(String args[]) {

            try {

                  if (args.length < 1) {
                        System.out.println("Usage: java RunAsync <command string>");
                        System.exit(-1);
                  }
                  Process pro = null;
                  if (args.length > 1) {
                        pro = Runtime.getRuntime().exec(args);
                  }
                  else {
                        pro = Runtime.getRuntime().exec(args[0]);
                  }
                  InputStream error = pro.getErrorStream();
                  InputStream output = pro.getInputStream();
                  Thread err = new Thread(new OutErrReader(error));
                  Thread out = new Thread(new OutErrReader(output));
                  out.start();
                  err.start();
                  pro.waitFor();
            }
            catch (java.io.IOException e) {
                  e.printStackTrace();
            }
            catch (java.lang.InterruptedException e) {
                  e.printStackTrace();
            }

      }


      /**
       *  Description of the Class
       *
       * @author     Administrator
       * @created    23 February 2004
       */
      static class OutErrReader implements Runnable {
            InputStream is;


            /**
             *Constructor for the OutErrReader object
             *
             * @param  is  Description of the Parameter
             */
            public OutErrReader(InputStream is) {
                  this.is = is;
            }


            /**
             *  Main processing method for the OutErrReader object
             */
            public void run() {
                  try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(is));
                        String temp = null;
                        while ((temp = in.readLine()) != null) {
                              System.out.println(temp);
                        }
                        is.close();
                  }
                  catch (Exception e) {
                        e.printStackTrace();
                  }
            }
      }
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

objectsCommented:
if the process expects input then you need to write it to outputstream
If it prduces output you need to read it from inputstream

BufferedReader in = new BufferedReader(new InputStreamReader(inputstream));
String line = null;
while (null!=(line=in.readLine()) {
   System.out.println(line);
}

theres a generic example here:

http://www.objects.com.au:8088/objects/java/examples/util/ConsoleExec.do
0
tbboyettAuthor Commented:
thanks for the responses guys.

CEHJ, I tried your RunAsync and it appears to be working great but it came back with this question:
What is your first and last name?

Can i use the -dname option or something like that to give it this input?
0
CEHJCommented:
>>but it came back with this question:
What is your first and last name?

That's a bit cheeky isn't it? ;-)
0
CEHJCommented:
>>Can i use the -dname option or something like that to give it this input?

Not sure to tell you the truth. This kind of program is enough to make me (a command line aficionado) pine for a GUI
0
objectsCommented:
should be able to

eg.

keytool -genkey -dname "cn=Mark Jones, ou=JavaSoft, o=Sun, c=US"
      -alias business -keypass kpi135 -keystore C:\working\mykeystore
      -storepass ab987c -validity 180
0
tbboyettAuthor Commented:
You're both great thanks, works like a dream now
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.