0

I would like to write a batch file that automatically gives input to a java program that prompts for user input.

My code so far is the following:

set path=C:\Program Files\Java\jdk-13.0.2\bin

javac *.java

java myJavaProgram

The problem is that when myJavaProgram executes it prompts the user for input, so the batch file waits for me to give input in order to continue execution. I would like to be able to pass the input automatically, using the .bat. Is this possible? Thanks for any help!

EDIT: Updated question to include more context.

In more detail, when myJavaProgram is executed from the batch file, it prompts for user input from the console and waits for it. I must give a String. When I write the String, myJavaProgram reads it with a Scanner and does something with it (not important what it does). Then it terminates. I want to change the batch code so that I don't need to write the String when it prompts for it. I want to set a variable with my input String in the batch file, and give this as input to myJavaProgram "bypassing" the console.

kyrant
  • 1
  • 2
  • In general: 1) pass an argument to the program, for example a path to a batch file. 2) Given the existence of the argument, use the argument to get the input. For example, if the program uses a `Scanner`, create the `Scanner` to read from the file represented by the argument (instead of reading from System.In). – Andrew S Dec 14 '20 at 16:13
  • https://stackoverflow.com/questions/1431551/command-line-pipe-input-in-java – Aaj Kaal Dec 14 '20 at 16:28
  • Please don't ever use that syntax, `set path=location`. What it does is remove every piece of string data from the variable values of both the User environment variable, `%Path%`, and the System environment variable `%Path%`. It then temporarily replaces it all with, just `C:\Program Files\Java\jdk-13.0.2\bin`, _(which in my opinion should really be `C:\Program Files\Java\jdk-13.0.2\bin;` anyhow)_. It would probably be better to do it like this instead, `If "%Path:~-1%" == ";" (Set "Path=%Path%%ProgramFiles%\Java\jdk-13.0.2\bin;") Else Set "Path=%Path%;%ProgramFiles%\Java\jdk-13.0.2\bin;"` – Compo Dec 14 '20 at 16:44
  • Also, `javac` isn't technically the right command, as that relies upon another variable. `%PATHEXT%`, not having been modified. The value data under `%PATHEXT%` is a listing of executable extensions which are added, in turn, until a file match is found in the current directory, then if not found, each of those within `%Path%`. Why not save that process from happening, and provide the extension yourself. `javac.exe *.java`. Please also take account that you should also therefore use, `java.exe myClass`, for a class file _(no `.class` extension)_, or `java.exe -jar myJar.jar` to run a jar file. – Compo Dec 14 '20 at 17:29
  • @Compo Thank you for the answer. You are right but this is not the problem that I am trying to solve here. I will use your suggestions though. – kyrant Dec 14 '20 at 18:54

2 Answers2

0

I've found a solution. I first redirect the echo command output (a string) from the command prompt window to file temp.txt using the > operator. The file temp.txt now contains the string that I want to give as input to myJavaProgram. Then I open temp.txt and send the output to the command prompt window as input to myJavaProgram using the < operator. The code is the following:

@echo off
If "%Path:~-1%" == ";" (Set "Path=%Path%%ProgramFiles%\Java\jdk-13.0.2\bin") Else Set "Path=%Path%;%ProgramFiles%\Java\jdk-13.0.2\bin"

javac.exe *.java

echo myString> temp.txt
java.exe myJavaProgram < temp.txt 
Mofi
  • 46,139
  • 17
  • 80
  • 143
kyrant
  • 1
  • 2
0

As suggested by @Mofi, a more straightforward solution uses the pipe | operator, that pipes the output from the first command (echo myString) into the input of the second command (java.exe myJavaProgram).

@echo off
If "%Path:~-1%" == ";" (Set "Path=%Path%%ProgramFiles%\Java\jdk-13.0.2\bin") Else Set "Path=%Path%;%ProgramFiles%\Java\jdk-13.0.2\bin"

javac.exe *.java

echo myString| java.exe myJavaProgram

Thank you for the help.

Mofi
  • 46,139
  • 17
  • 80
  • 143
kyrant
  • 1
  • 2