CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Run program with batch file

    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

  2. #2
    Join Date
    Oct 2008
    Posts
    77

    Re: Run program with batch file

    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.
    Last edited by postmortem; September 30th, 2009 at 11:33 PM.

  3. #3
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Run program with batch file

    Quote Originally Posted by postmortem View Post
    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.
    Attached Images Attached Images  
    Last edited by blinksumgreen; September 30th, 2009 at 08:38 PM.

  4. #4
    Join Date
    Oct 2008
    Posts
    77

    Re: Run program with batch file

    Code:
     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.

  5. #5
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Run program with batch file

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured