How to run a java file using go script.

449 views
Skip to first unread message

PK

unread,
Aug 3, 2022, 8:44:53 AM8/3/22
to golang-nuts
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

Eli Bendersky

unread,
Aug 3, 2022, 10:19:13 AM8/3/22
to PK, golang-nuts
You should be able to use the os/exec package (https://pkg.go.dev/os/exec) to invoke external programs from Go.

--
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.

TECHAX

unread,
Aug 3, 2022, 2:38:26 PM8/3/22
to Eli Bendersky, golang-nuts
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?

Kurtis Rader

unread,
Aug 3, 2022, 4:16:04 PM8/3/22
to TECHAX, Eli Bendersky, golang-nuts
On Wed, Aug 3, 2022 at 11:38 AM TECHAX <princ...@gmail.com> wrote:
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?

That statement starts a new bash process which modifies its environment and exits, thus throwing away the change to its PATH env var. Env vars are private to each process. They are not global. You cannot modify the env vars of a different process (at least not without its active cooperation). It's not clear what problem you are trying to solve. You can modify the PATH env var of your Go process using https://pkg.go.dev/os#Setenv.

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.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

TECHAX

unread,
Aug 4, 2022, 12:25:01 AM8/4/22
to Kurtis Rader, Eli Bendersky, golang-nuts
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?

Jan Mercl

unread,
Aug 4, 2022, 2:11:57 AM8/4/22
to TECHAX, golang-nuts
On Thu, Aug 4, 2022 at 6:24 AM TECHAX <princ...@gmail.com> 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?

Check and share the value of error returned by Command.Output(). Also,
try CombinedOutput() instead of Output().

TECHAX

unread,
Aug 4, 2022, 3:45:08 AM8/4/22
to Thomas Faughnan, Kurtis Rader, Eli Bendersky, golang-nuts
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

On Thu, Aug 4, 2022 at 11:45 AM Thomas Faughnan <t...@tjf.sh> wrote:
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

Peter Galbavy

unread,
Aug 4, 2022, 3:45:48 AM8/4/22
to golang-nuts
When you use

  cmd, err := exec.Command(...)

you then set-up it's environment before execution by setting cmd.Env:

  cmd.Env = append(os.Environ(), `PATH="..."`)

and only then run the command:

  out, err := cmd.Output()
  if err ... { ... }
  fmt.Println("output:", out)

The above mostly from memory, so excuse syntax.

Thomas Faughnan

unread,
Aug 4, 2022, 1:11:11 PM8/4/22
to TECHAX, Kurtis Rader, Eli Bendersky, golang-nuts
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?

Kurtis Rader

unread,
Aug 4, 2022, 1:23:43 PM8/4/22
to TECHAX, Thomas Faughnan, Eli Bendersky, golang-nuts
On Thu, Aug 4, 2022 at 12:44 AM TECHAX <princ...@gmail.com> wrote:
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
Reply all
Reply to author
Forward
0 new messages