[英]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.