简体   繁体   English

从Java将字符串传递到命令行程序

[英]passing Strings to a command line program from java

I'm currently writing a java program which automates my daily work with Android apps. 我目前正在编写一个Java程序,该程序可以自动执行Android应用程序的日常工作。 The main Task of the program is to run serveral external tools via the windows command line and it works fine as long as I don't have to interact with the called cmdline tool. 该程序的主要任务是通过Windows命令行运行服务器外部工具,只要我不必与调用的cmdline工具进行交互,它就可以正常工作。 I have Problems with creating a keystore using ''keytool''. 我在使用“ keytool”创建密钥库时遇到问题。 During the execution of ''keytool'' the commandline prompts me to type in my name, passwords etc. Is it possible to read this information from a file? 在执行“ keytool”期间,命令行会提示我输入我的姓名,密码等。是否可以从文件中读取此信息? I don't know if it helps, but this is the class which handles the execution of my commands. 我不知道是否有帮助,但这是处理命令执行的类。

private static void executeCmd(String command, PrintStream output) throws IOException, InterruptedException {
    final Runtime r = Runtime.getRuntime();
    final Process p = r.exec(command);

    java.util.List<StreamWriter> reader = Arrays.asList(
            new StreamWriter(p.getInputStream(), output).startAndGet(),
            new StreamWriter(p.getErrorStream(), output).startAndGet()
    );

    if (output != null)
        output.println("waiting for: " + command);
    p.waitFor();
    reader.forEach(StreamWriter::joinSilent);
    if (output != null)
        output.println("waiting done");
}

check keytool -help. 检查keytool-帮助。 you can pass alias name and password in keytool arguments itself. 您可以在keytool参数本身中传递别名和密码。 for example 例如

keytool -list -alias TEST -keystore "C:\java\jdk\jre\lib\security\cacerts" -storepass changeit 

Check the link from oracle for more examples 检查来自oracle的链接以获取更多示例

You may prepare input file for the command and pass it to the standard input. 您可以为命令准备输入文件,并将其传递到标准输入。 It could be done using ProcessBuilder instead of Runtime.exec : 可以使用ProcessBuilder代替Runtime.exec来完成:

final Process process = new ProcessBuilder(command)
        .redirectInput(new File(standardInputFileName))
        .start();

But, actually, you don't need input redirection to use keytool . 但是,实际上,您不需要输入重定向即可使用keytool You may pass all required information as command-line arguments. 您可以将所有必需的信息作为命令行参数传递。 For example, here is the command line that generates a new RSA key (spitted in lines for clarity): 例如,以下是生成新的RSA密钥的命令行(为清晰起见,在各行中都加了一行):

keytool -genkeypair
    -keystore "<key store file>"
    -alias "<key alias>"
    -keyalg "RSA"
    -keypass "<key password>"
    -storepass "<store password>"
    -dname "<distingushed name>"

Where distinguished name has a form (again, splitted in lines for clarity) 可分辨名称具有格式的位置(再次为清晰起见,以行分隔)

CN=<common name>, 
OU=<orgamization unit>,
O=<organization>, 
L=<location>, 
ST=<state>,
C=<two-letter country code>

Working example that generates a new key without user interaction: 在没有用户交互的情况下生成新密钥的工作示例:

keytool -genkeypair -keystore my.store -alias my.key -keyalg RSA -storepass storage-pass -keypass key-pass -dname "CN=Nobody, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=US"

More information is available on keytool documentation page . 有关更多信息,请参见keytool文档页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM