简体   繁体   English

使用在运行时创建的批处理文件并将其提供为jar文件的输入

[英]Using a batch file created at runtime and providing it as input to jar file

I am new to windows batch scripting. 我是Windows批处理脚本的新手。 Need help with this. 需要帮助。 My question is, I am running a jar file which asks for inputs. 我的问题是,我正在运行一个jar文件,要求输入。 I am providing the input in the text file and i am able to execute it with the below command successfully. 我在文本文件中提供输入,并且能够使用以下命令成功执行它。

java -cp abc.jar<input.txt

Input.txt: Y 1 Path_of_file_along_with_file_name Input.txt: Y 1路径文件名称与文件名称

So, the issue arises at this point. 因此,此时出现了问题。 The 3rd argument gets changed at each and every execution. 第三个参数在每次执行时都会更改。 3rd argument will be the name of the file which was modified. 第三个参数将是被修改的文件的名称。 I am able to come up with the code which retrieves the modified file and write the same into another file(out.txt). 我能够拿出代码来检索修改后的文件,并将其写入另一个文件(out.txt)。 But when i use the below piece of code to create my input file dynamically: 但是当我使用下面的代码动态创建我的输入文件时:

@echo off
echo Y >> input.txt
echo 1 >> input.txt
type out.txt >> input.txt

I tried with copy command as well. 我也尝试了复制命令。 But my jar file does not take this as input and it is getting failed. 但是我的jar文件没有将此作为输入,并且失败了。

But when i create a file manually with the 3 arguments it works fine. 但是,当我使用3个参数手动创建文件时,它可以正常工作。 When i try to create my input.txt from the batch file, it is getting failed. 当我尝试从批处理文件创建我的input.txt时,它失败了。

Please help me with the above. 请帮助我以上。

Try with 试试看

@echo off
> input.txt (
    echo Y
    echo 1
    type out.txt
)

Or 要么

@echo off
 > input.txt echo Y
>> input.txt echo 1
>> input.txt type out.txt

The main "problem" with your code is that the >> operator will append to the file. 您的代码的主要“问题”是>>运算符将附加到文件中。 If it already exist, the new data is appended after the previous data. 如果已经存在,则将新数据附加在先前数据之后。 You need to first remove the file or ensure it is overwritten. 您需要先删除文件或确保文件被覆盖。

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

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