batch goto :eof not working when called from java

huangapple go评论76阅读模式
英文:

batch goto :eof not working when called from java

问题

以下是翻译好的部分:

我有一个批处理文件,大致代码如下:

@echo off
(
    set /p x=
    set /p y=
) < settings.cdb

IF DEFINED x (
    IF DEFINED y (
        ECHO true
        GoTo :EOF
    )
)
ECHO false
GoTo :EOF

在Java中,我有以下代码来通过命令行调用批处理文件:

ProcessBuilder probuilder = new ProcessBuilder(command);
Process pr = probuilder.start();

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line;
while ((line = input.readLine()) != null)
{
    lines.add(line);
}

XY 是来自配置文件的一些输入参数,可能包含数据,也可能不包含数据。

xy 未定义时,一切都按预期运行,输出 false

问题出现在变量都被定义时。
当通过命令行调用批处理文件时,我得到的输出结果是:true。<br/>这是预期的输出。

但是当我通过Java进程调用相同的批处理文件时,我得到以下输出:false。<br/>这不是我想要的。

删除 @echo off 后,从命令行运行批处理文件时,我得到以下输出:

IF DEFINED x (IF DEFINED y (
ECHO true
 GoTo :EOF
) )
true

但是当我从Java进程运行它时:

IF DEFINED x (IF DEFINED y (
ECHO true  
 GoTo :EOF 
) ) 
ECHO false 
false
GoTo :EOF 

甚至没有输出 echo true

我尝试使用 EXIT /b 0 替代 GoTo :EOF,但结果相同。

所以我在这里漏掉了什么?为什么从Java调用时,尽管有 GoTo :EOF,程序仍然继续运行?<br/>
为什么输出不同?这是Java的问题吗?这是批处理的问题吗?

更新

最后,从加载变量的文件相对于命令行位置而不是批处理文件位置。

英文:

I have a batch file roughly with the following code:

@echo off
(
    set /p x=
    set /p y=
) &lt; settings.cdb

IF DEFINED x (
    IF DEFINED y (
        ECHO true
        GoTo :EOF
    )
)
ECHO false
GoTo :EOF

In Java, I have the following code to call the batch file via command line:

ProcessBuilder probuilder = new ProcessBuilder(command);
Process pr = probuilder.start();

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line;
while ((line = input.readLine()) != null)
{
    lines.add(line);
}

X and Y are some input parameters from a configuration file, which may, or may not, contain any data.

When x or y are not defined everything works as suppose, outputs false.

The problem comes when the variables are both defined.
When the batch file gets called via command line I get the resulting output: true. <br/>Which is the intend output.

When I call the same batch file via Java Process I get the following output:false. <br/>Which is not what I want.

Removing the @echo off I get the following output from running the batch file from command line:

IF DEFINED x (IF DEFINED y (
ECHO true
 GoTo :EOF
) )
true

But when I run it from Java Process:

IF DEFINED x (IF DEFINED y (
ECHO true  
 GoTo :EOF 
) ) 
ECHO false 
false
GoTo :EOF 

It is not even outputting the echo true.

I've tried with EXIT /b 0 instead of GoTo :EOF but with the same result.

So what am I missing here? Why the program, when called from Java, keeps going even though it has a GoTo :EOF?<br/>
Why the different outputs? Is it a Java thing? Is it a batch thing?

UPDATE:

After all the file from where variable were being loaded is relative to the command line location instead of the bat location.

答案1

得分: 1

以下是翻译好的内容:

你说得没错:“它甚至没有输出回显 true”。当你添加调试行时,需要考虑一下它的含义。'x' 没有被定义,或者 'y' 没有被定义(或者很可能两者都没有被定义)。

你可以管理生成进程的环境,使用 ProcessBuilder,在启动之前(例如使用 start())首先设置环境;例如,使用 pb.environment.put("x", "hello");

不过,你为什么要从 Java 运行批处理脚本呢?它们在80年代就已经过时了,现在有大量的替代方案可用。如果你解释一下通过批处理文件外包工作的目的,也许 Stack Overflow 的读者可以提供一些有用的建议。

英文:

Well, you said it: "It is not even outputting the echo true". When you add debug lines, it.. helps to think for a second about what it means. 'x' is not defined, or 'y' is not defined (or most likely both are not defined).

You can manage the environment of the spawned process, use ProcessBuilder, and before launching (with e.g. start()), first set up the environment; for example with pb.environment.put(&quot;x&quot;, &quot;hello&quot;);.

Why are you running batch scripts from java, though? They were bad technology back in the 80s, and there are a ton of alternatives available at this point. If you explain what you're trying to accomplish by farming work out to a batch file, perhaps an enterprising SO reader can make some useful suggestions.

huangapple
  • 本文由 发表于 2020年7月28日 20:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63133858.html
匿名

发表评论

匿名网友

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

确定