Input to java process from batch file

If I have a simple java program that processes lines of text from standard input, I can run it with the following script:

@Echo off
java Test < file.txt
pause
exit

script redirects input lines from file.txt to java program.

Is there a way to avoid using a separate file? Or is this the easiest way?

Thank.

+3
source share
2 answers

Use pipe .

This trivial Java application simply prints lines from stdin:

public class Dump {
  public static void main(String[] args) {
    java.util.Scanner scanner = new java.util.Scanner(System.in);
    int line = 0;
    while (scanner.hasNextLine()) {
      System.out.format("%d: %s%n", line++, scanner.nextLine());
    }
  }
}

Called with this batch file:

@ECHO OFF
(ECHO This is line one
ECHO This is line two; the next is empty
ECHO.
ECHO This is line four)| java -cp bin Dump
PAUSE

... he will print:

0: This is line one
1: This is line two; the next is empty
2:
3: This is line four
+4
source

You can make simple:

$ java Test < file.txt

pause, script Test.

0

Source: https://habr.com/ru/post/1743710/


All Articles