Click to See Complete Forum and Search --> : Run program with batch file
blinksumgreen
September 29th, 2009, 11:53 AM
I know when using C++ you can do the following to read in a list of commands from a text file, run them against the program, and output the results to a text file:
ExampleProg < test.txt > results.txt
Can anyone tell me how to get the same result, but using a Java program. I do most programming in an IDE, so I'm not all that great with knowing what commands need to be done at the command line.
Is it something like the following?
java ExampleProg < test.txt > results.txt
postmortem
September 29th, 2009, 11:55 PM
yeah, it is same thing
if you have JAR build you can use
java -jar your_file test.txt >> output.txt
or if you have compiled class files
java classfile.class test.txt >> output.txt
Java also uses main() function, there your arguments can be parsed.
blinksumgreen
September 30th, 2009, 08:35 PM
if you have JAR build you can use
java - jar your_file test.txt >> output.txt
When I try this method it says that it cannot create the java virtual machine. I've attached an image of what it tells me.
Batch file consists of: java - jar printStatement Input.txt >> output.txt
I'm not sure how the '1' that shows up after the input.txt in the image I attached gets in there.
postmortem
September 30th, 2009, 11:34 PM
java -jar your_file test.txt >> output.txt
sorry, there is no space between '-' and 'jar'. make sure you follow rest of whitespaces. printStatement has to be JAR file.
jcaccia
October 1st, 2009, 06:07 AM
If you use the -jar option make sure you don't forget the .jar extension (printStatement.jar in this case). Also use redirection for the input file (<Input.txt) unless you read the file name from the arguments list and open the file yourself. With redirection you just use System.in.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.