如何在Java的命令提示符中输入字符串作为值,而不使用Scanner。

huangapple 未分类评论43阅读模式
英文:

how to enter string as value in inputs requested in command prompt in java without scanner

问题

以下是翻译好的部分:

我对在Java中处理cmd.exe不太熟悉。我试图自动化一个通过命令行界面运行的应用程序。

我想在命令提示符要求用户输入时传递字符串值。在这种情况下,提出的问题是 输入区域设置:

我想将 en-us 作为一个字符串传递给命令提示符中提出的问题。

我不想使用scanner类,因为它会需要人工干预。

这是我的代码:

public class run {
    public static void main(String[] args) throws IOException, InterruptedException {

        String[] commands = {"cd C:\\Users\\Aniket Shikhare\\Documents\\contentstack-cli-new",
                            "csdx cm:export --auth-token",
                            "en-us"};

        for(String command : commands) {

            execute(command);
        }

    }


    public static void execute(String command) throws InterruptedException, IOException {
        ProcessBuilder builder = new ProcessBuilder("cmd.exe","/c",command);
        Process process = builder.inheritIO().start();
        process.waitFor();

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

    }
}

我的代码不能在提示问题时输入 en-us 。有人可以帮助我如何在提示中输入这个吗?

以下图像是我的代码输出的结果。
cmd提示符

英文:

I am new to handling cmd.exe in java. I am trying to automate one application which is run by CLI.

I want to pass string values when the command prompt has asked for input from user. In this case the question asked is enter locale:

I want to pass en-us as a string in the question asked in the command prompt.

I don't wanna use scanner class since it will require human intervention.

This is my code :

public class run {
	public static void main(String[] args) throws IOException, InterruptedException {

		String[] commands = {"cd C:\\Users\\Aniket Shikhare\\Documents\\contentstack-cli-new","csdx cm:export --auth-token","en-us"};

		for(String command : commands) {

			execute(command);
		}

	}


	public static void execute(String command) throws InterruptedException, IOException {
		ProcessBuilder builder = new ProcessBuilder("cmd.exe","/c",command);
		Process process = builder.inheritIO().start();
		process.waitFor();

		BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
		
	}
}

My code is not able to enter en-us when the question is prompted. Can anyone help me how to enter this in the prompt ?

Below image is the output of my code.
cmd prompt

huangapple
  • 本文由 发表于 2020年7月26日 02:46:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/63092216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定