dongyoudi1342 2013-06-30 09:29
浏览 122
已采纳

从Go执行第三方二进制文件

I made a little hack in Go for my needs to run 3rd party executable with dynamic flags (which depend on server settings and some hardware specs, so different on each machine and each time).

I'm using some neat library to help me out find which path is Go executable located in. The 3rd party binary is in same folder as Go one.

  path, err := osext.ExecutableFolder()
  if err != nil {
    log.Fatal(err)
  }
  path += "3rdparty.exe"

I than run the fmt's Sprintf method, to put path and flags into single string called Command.

I try to invoke it like so:

out, err := exec.Command(Command).Output()
  if err != nil {
    fmt.Println("Command execution failed:", err)
  }

However err isn't nil. I can't copy error out of vmware (setup windows there just to compile and test), but it goes like: Command execution failed: "C:\\PATH\\TO\\3rdparty.exe --flags-omitted" file does not exist

However when I copy C:\\PATH\\TO\\3rdparty.exe --flags-omitted into cmd it runs perfectly.

Any ideas?

  • 写回答

1条回答 默认 最新

  • douke1905 2013-06-30 10:09
    关注

    The command and its parametets must be separate strings, do not join them into a single string.

    At a closer look, the error message actually is clear about it (note where the quotes are):

    Command execution failed: "C:\\PATH\\TO\\3rdparty.exe --flags-omitted" file does not exist
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?