0

I was thinking of passing the result from Java run from command line into a command line variable. But after searching much for it, I only came across Runtime class which just runs the command.

Therefore, my question, is there anything I can do to get Java result into command line.

Platform of course is Windows.

yoda
  • 539
  • 1
  • 12
  • 32

2 Answers2

3

There are only 2 ways any program (Java or otherwise) can send results back to the "command line":

1) exit code

2) capture the strings output by the program.

#1 can be done in Java using System.exit (or see a couple more options here).

#2 is done by the shell. In Unix, you typically use backticks or $():

OUTPUT=`java app.class`

In Windows Powershell, do something like

$result = & java app.class 2>&1 | Out-String
Community
  • 1
  • 1
theglauber
  • 28,367
  • 7
  • 29
  • 47
0

Possible duplicate of how-do-i-get-the-result-of-a-command-in-a-variable-in-windows

FOR /F "delims=" %i IN ('java Main') DO set today=%i

Also allows today variable to be set.

Community
  • 1
  • 1
peeyush
  • 2,841
  • 3
  • 24
  • 43