Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Creating a KeyStore using java

Posted on 2006-04-05
10
Medium Priority
?
489 Views
Last Modified: 2012-08-13
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();
0
Comment
Question by:tbboyett
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16387159
may be waiting for input
or the output buffer is blocked. you need to read any output sent to stdout/stderr
0
 
LVL 5

Author Comment

by:tbboyett
ID: 16387324
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 16387592
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 16387601
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
 
LVL 5

Author Comment

by:tbboyett
ID: 16387680
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16387687
>>but it came back with this question:
What is your first and last name?

That's a bit cheeky isn't it? ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16387703
>>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
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 16387737
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
 
LVL 5

Author Comment

by:tbboyett
ID: 16387748
You're both great thanks, works like a dream now
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16387791
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question