简体   繁体   English

Java-从程序获取输出…命令行参数?

[英]Java - Getting Output from the program … Command line arguments?

I am writing a Java program and would be calling this in a DOS batch script....(it is a Swing based GUI application) I wish to get output from the Java program in the calling DOS script. 我正在编写一个Java程序,并会在DOS批处理脚本中调用它。...(它是基于Swing的GUI应用程序)我希望从调用DOS脚本的Java程序中获取输出。 I am planning to get this information out through the use of command line arguments - like 我正计划通过使用命令行参数来获取此信息,例如

(in the DOS script) (在DOS脚本中)

java myProg var java myProg var

if (%var% == "loggedin") { 如果(%var%==“ loggedin”){

} }

var is supposed to be populated with the output from myProg. var应该用myProg的输出填充。 The Java program myProg is supposed to set the value args[0] (in var) and I am assuming this will be remembered when control comes back to the DOS script. Java程序myProg应该设置值args [0](以var为单位),我假设当控制权返回DOS脚本时,将记住该值。 Is this possible? 这可能吗? If not how can I achieve my use-case? 如果没有,如何实现用例? What options do I have? 我有什么选择? How can I pass a flag (as "loggedin") from myProg to the calling DOS batch script? 如何将myProg中的标志(如“ loggedin”)传递给调用DOS批处理脚本?

Edit: try to use dos command "pipe": 编辑:尝试使用DOS命令“管道”:

Command1 | Command2

the output (eg: System.out.println()) of Command1 will be the input argument of Command2 The Command1 is the command which run the java app The Command2 is the batch file which process args as %1, %2 Command1的输出(例如:System.out.println())将成为Command2的输入参数。Command1是运行Java应用程序的命令Command2是将args处理为%1,%2的批处理文件

Command line arguments cannot be modified; 命令行参数不能修改; they are literals. 它们是文字。 The only thing you can do is have text output to stdout or stderr (as yelliver suggested), set an environment variable, write to a log file of some kind, or return an exit code. 唯一可以做的就是将文本输出到stdout或stderr(如yelliver所建议的),设置环境变量,写入某种日志文件或返回退出代码。

The way to "populate var with the output from myProg" is this (in the DOS script): “使用myProg的输出填充var”的方式是这样的(在DOS脚本中):

for /F "delims=" %%a in ('java myProg') do set "var=%%a"

Note that the Java program must "output" the value you want to assign to var, that is, it must show that value in the screen (via Stdout). 请注意,Java程序必须“输出”要分配给var的值,也就是说,它必须在屏幕上显示该值(通过Stdout)。

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

相关问题 Java在命令行提示符下不断更新一行时从命令行执行的程序获取输出 - Java getting output from program executed in command line when it constantly updates a single line in the command prompt 如果子进程是Java程序(带有命令行参数),如何从subprocess.check_output获取输出? - How can I get output from subprocess.check_output if the subprocess is a java program(with Command-line arguments)? Java Runtime.getRuntime():从执行命令行程序中获取输出 - Java Runtime.getRuntime(): getting output from executing a command line program 从命令行在Java程序中传递运行参数会产生错误 - Passing run arguments in a java program from command line gives an error 从命令行获取参数 - Getting arguments from the command line 通过执行命令多行程序获取输出 - getting output from executing a command multi line program 在Java程序中执行命令行参数的问题 - Issue with executing command line arguments in a Java program 解析 Java 命令行程序的参数 - Parsing arguments to a Java command line program 通过命令行参数控制程序 - Control program from command line arguments 如何运行从Java程序中获取命令行参数的Python程序 - How to Run a Python Program that Takes Command-Line arguments from within a Java Program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM