--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4c63befc-3532-4ab0-b2a3-fb61f4d3b51an%40googlegroups.com.
I tried this for setting Java, but not working.CMD:= exec.Command("bash","-c","export PATH=/home/user/jdk8/bin")Can you please help me with this?
On Wed, 3 Aug, 2022, 7:48 pm Eli Bendersky, <eli...@gmail.com> wrote:--You should be able to use the os/exec package (https://pkg.go.dev/os/exec) to invoke external programs from Go.On Wed, Aug 3, 2022 at 5:45 AM PK <princ...@gmail.com> wrote:Hi everyone,
I am new to golang. I was writing a script, with which I want to run a java program.
Can anyone please let me know how to run java file using go scripts.
Thanks --
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4c63befc-3532-4ab0-b2a3-fb61f4d3b51an%40googlegroups.com.
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEMj0xqdy7xd1_uJ6nqxx5vGogdoonmwkt4qNYz7%3DekaXQmUpQ%40mail.gmail.com.
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
Thank you so much, I am able to add java path. I also need to add some other path as well, so Can I do like this:
os.Setenv("PATH","/home/sdk/jdk-11.0.16/bin:/home/temp/jtreg/bin:$PATH")
cmd,err:=exec.Command("java","-version").CombinedOutput()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(cmd))Or Is there any way to add multiple path just we like do with export such as:export PATH= /home/sdk/jdk-11build/jdk-11.0.16/bin: /home/temp/jtreg/bin:$PATH