谷歌 Golang exec 命令退出状态 2 和 1

7
我希望你能够使用Go语言在Android SDK platform-tools中执行dexdump。
我已经设置了PATH变量。(我使用的是Ubuntu 12.04)
以下是我的代码:
package main

import (
    "bytes"
    "fmt"
    "log"
    "os/exec"
)

func main() {
    path, err := exec.LookPath("dexdump")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(path)

    cmd := exec.Command(path)
    var out bytes.Buffer
    cmd.Stdout = &out
    err2 := cmd.Run()
    if err2 != nil {
        log.Fatal(err2)
    }
    fmt.Printf("%q\n", out.String())
}

结果: /home/gunwoo/android-sdk-linux/platform-tools/dexdump 2012/10/15 16:44:39 退出状态 2
退出状态 1
为什么Go找不到路径?

"我已经设置了PATH变量",具体是什么方式? - zzzz
我在 .profile 文件中写入了 "export PATH=$PATH:$HOME/android-sdk-linux/platform-tools"。 - user1746360
1个回答

6
您没有为exec.Run dexdump命令提供任何参数,这可能会导致错误,例如:
dexdump: no file specified
dexdump: [-f] [-h] dexfile...

-d : disassemble code sections
-f : display summary information from file header
-h : display file header details
-C : decode (demangle) low-level symbol names
-S : compute sizes only

当您运行以下版本的程序时,会得到什么输出?
package main

import (
    "bytes"
    "fmt"
    "log"
    "os/exec"
)

func main() {
    path, err := exec.LookPath("dexdump")
    if err != nil {
        log.Fatal("LookPath: ", err)
    }
    fmt.Println(path)
    cmd := exec.Command(path)
    var out bytes.Buffer
    cmd.Stdout = &out
    err = cmd.Run()
    fmt.Printf("%s\n", out.String())
    if err != nil {
        log.Fatal("Run: ", err)
    }
}

谢谢!我希望在没有参数的情况下打印dexdump使用注释。 - user1746360
2
使用信息可能会输出到 stderr 而不是 stdout。此外,当程序打印使用信息时返回了非零的退出代码,这会触发您的 log.Fatal(err2) 命令。如果要查看输出,请设置 cmd.Stderr = &out 并将 fmt.Printf("%q\n", out.String()) 移动到您的 if err2 != nil 行之前。 - Matt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接