On Thu Aug 4, 2022 at 12:24 AM EDT, TECHAX wrote:
> I tried the following one but still, it's not working.
>
> *os.Setenv("PATH","/home/sdk/jdk-11.0.16/bin")*
> *cmd,_:=exec.Command("java","-version").Output()*
> *fmt.Println(string(cmd))*
>
> Since the path is set, so it should display the java version, right?

My guess is because `java -version` prints to standard error instead of
standard output (at least on my machine). You can verify this by comparing
the results of `java -version > /dev/null` and `java -version 2> /dev/null`
in a shell. Some programs choose to print their version to standard output,
e.g. `go version`. Output() returns the process's standard output and
possibly an error, but it does not give you the process's standard error.
CombinedOutput should give you both streams combined, or you could fiddle with
StderrPipe and StdoutPipe.

See here for the details: https://pkg.go.dev/os/exec

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CLX0VJTWCI6G.TCHC1U7QMQ1Y%40deepthought.

Reply via email to