External process execution in Java

Thing you want to do

I want to import a p12 file into cacert (Java keystore) with keytool. Manually executing import from the command line in actual operation is time-consuming and unrealistic, so I would like to include it as a process of the running Web application.

Advance preparation

Since it was set so that only the administrator can rewrite cacerts, the authority is also given to the user who is running the web application. (The certificate could not be imported unless the command prompt was executed with administrator privileges.)

Sample source

sample1.java


String JRE_PATH = "C:\\Program Files\\Java\\jre1.8.0_144";
String KEYSTORE_PATH = JRE_PATH + "\\lib\\security\\cacerts"; //cacerts path
String CERTFILE_PATH = "cert.p12"; //p12 file path
String ALIAS = "test"; //alias
String KEYSTORE_PASS = "changeit"; //Keystore password
String PRIVATEKEY_PASS = "root"; //Certificate password

//Command to import certificate
ProcessBuilder importCertPb = new ProcessBuilder( "keytool", "-importkeystore", "-keystore",
                "\"" + KEYSTORE_PATH + "\"", "-srckeystore",
                CERTFILE_PATH, "-srcstoretype", "PKCS12", "-srcstorepass", PRIVATEKEY_PASS, "-deststorepass",
                KEYSTORE_PASS );

//Since it is imported with alias "1", give it an alias
// XXXX:Alias (like a name that uniquely identifies a certificate)
ProcessBuilder changeAliasPb = new ProcessBuilder( "keytool", "-changealias", "-alias", "1", "-destalias", XXXX,
        "-keystore", "\"" + KEYSTORE_PATH + "\"", "-keypass", PRIVATEKEY_PASS, "-storepass", KEYSTORE_PASS );

//Command to check if the certificate has been imported
ProcessBuilder checkExistCertPb = new ProcessBuilder( "keytool", "-list", "-alias", XXXXX,
        "-keystore", "\"" + KEYSTORE_PATH + "\"", "-storepass", KEYSTORE_PASS );

//External process execution
Process importCertPbSt = importCertPb.start();

//Wait until the process ends
importCertPbSt.waitFor();

Get and output the result

java::sample2.java


//Alias: Execute command to get certificate matching XXXX
Process checkExistCertPbSt = checkExistCertPb.start();
checkExistCertPbSt.waitFor();
InputStream in = checkExistCertPbSt.getInputStream();

try {
    String line = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (Exception e) {
    e.printStackTrace();
}

Related article

File path is not recognized when executing an external command from a Java application

Recommended Posts

External process execution in Java
Run an external process in Java
Parallel execution in Java
thread safe process in java language
Java buffering process
Changes in Java 11
Rock-paper-scissors in Java
Java check process
Process every arbitrary number in Java List
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
Java instruction execution statement
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
POST JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Initializing HashMap in Java
What I learned in Java (Part 3) Instruction execution statement
Try using RocksDB in Java
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
Edit ini in Java: ini4j
Java history in this world
Java debug execution [for Java beginners]
Try calling JavaScript in Java
Try functional type in Java! ①
I made roulette in Java.
Java and Iterator Part 1 External Iterator
Create hyperlinks in Java PowerPoint
[Implementation] Java Process class notes
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Topic Analysis (LDA) in Java
Identify threads in the Java process that are wasting CPU
NEologd preprocessing in Java neologdn-java
Change java encoding in windows
Java Stream API in 5 minutes
Cannot find javax.annotation.Generated in Java 11