简体   繁体   English

如何在 powershell 脚本中为 java 程序传递用户输入

[英]How to pass user inputs for a java program in powershell script

I have a java program abc.java which prompts the user for some input.我有一个 java 程序 abc.java 提示用户输入。 Now I am calling this java file in my powershell script.现在我在我的 powershell 脚本中调用这个 java 文件。 When I am executing the powershell, it comes to the prompt for the input.当我执行 powershell 时,会出现输入提示。 So now how can I enter the user input here?那么现在如何在这里输入用户输入呢?

My powershell script is as follows:我的 powershell 脚本如下:

cd 'C:\LDAP\test1'
java abc

The output of the above run is as follows:上面运行的output如下:

PS C:\> C:\LDAP\sample.ps1
Enter the prefix:

Now how to enter the value?现在如何输入值? I am ok with either entering the input during the runtime or even hardcode the input in the powershell script.我可以在运行时输入输入,甚至可以在 powershell 脚本中对输入进行硬编码。

System.out.println("Enter the prefix:"); 

String strPrefix = objScanner.nextLine(); 

System.out.println("Enter the Country:"); 

String strLocation = objScanner.nextLine();

I wrote abc.java like this.我这样写 abc.java 。

import java.util.Scanner;        

class abc {
    public static void main(String[] args) {
        try {
            Scanner objScanner = new Scanner(System.in);
            System.out.print("Enter the prefix:"); 
            String strPrefix = objScanner.nextLine(); 
            System.out.println("Prefix is \"" + strPrefix + "\"");

            System.out.print("Enter the Country:"); 
            String strLocation = objScanner.nextLine();
            System.out.println("Country is \"" + strLocation + "\"");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

And my powershell seems as follows.而我的 powershell 似乎如下。

PS C:\> C:\LDAP\sample.ps1
Enter the prefix:Mr
Prefix is "Mr"
Enter the Country:America
Country is "America"

If you use如果你使用

System.out.println("Enter the prefix:"); 

and

System.out.println("Enter the Country:"); 

,powershell seems as follows. ,powershell 看起来如下。

PS C:\> C:\LDAP\sample.ps1
Enter the prefix:
Mr
Prefix is "Mr"
Enter the Country:
America
Country is "America"

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

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