Preethi Raju Preethi Raju -4 years ago 55
Java Question

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

I am new to windows batch scripting. Need help with this. My question is, I am running a jar file which asks for inputs. 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

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). 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.

But when i create a file manually with the 3 arguments it works fine. When i try to create my input.txt from the batch file, it is getting failed.

Please help me with the above.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download