执行: “firefox”: 在$PATH中找不到可执行文件

huangapple go评论92阅读模式
英文:

exec: "firefox": executable file not found in $PATH

问题

我正在阅读一本关于编写命令行工具的 Go 书籍。在其中一个示例中,我需要使用 Firefox 浏览器打开一个 HTML 文件。运行的命令如下:

browserPath,_ := exec.LookPath("firefox")

// 在浏览器中打开文件
if err := exec.Command(browserPath, "index.html").Start(); err != nil {
	return err
}

但是出现了以下错误:

exec: "firefox": 在 $PATH 中找不到可执行文件

我的 $PATH 设置如下:

$HOME/bin:/usr/local/bin:$PATH

我在 Mac 上使用 zsh。我查看了类似的问题,但还没有解决,有人看出我漏掉了什么吗?

英文:

I am reading a go book to learn go writing command line tools. In one of the examples I need to open firefox browser with an html file. The command run is:

browserPath,_ := exec.LookPath("firefox")

// Open the file on the browser
if err := exec.Command(browserPath, "index.html").Start(); err != nil {
	return err
}

But get the error:

exec: "firefox": executable file not found in $PATH

My $PATH is:

$HOME/bin:/usr/local/bin:$PATH

I am running on mac with zsh. I looked at similar problems but can't solve it yet, anyone see what I am missing?

答案1

得分: 2

首先,感谢您重新打开这个问题,我明白对许多人来说可能很简单,但它可以帮助其他人看到这个答案。

问题是(正如@xarantolus评论的那样),我的PATH没有包含到我的/Applications文件夹的路径。由于我使用的是zsh和Mac,我采取了以下步骤来解决这个问题:

首先找到firefox可执行文件的位置:

$ type -a firefox

打印的路径为:

firefox is /Applications/Firefox.app/Contents/MacOS/firefox

现在打开zshrc文件:

$ vim ~/.zshrc

在文件中,我的$PATH$HOME/bin:/usr/local/bin:$PATH,我添加了/Applications,所以该行最终变成了这样:

export PATH=$HOME/bin:/usr/local/bin:/Applications/:$PATH

注意,在添加新路径后,:$PATH也将位于末尾。

然后运行命令重新加载.zshrc文件:

source ~/.zshrc

如果您不使用zsh,请使用.bashrc文件代替.zshrc。

现在,Go应该能够看到firefox可执行文件,并按预期打开它。

英文:

First of all thank you for reopening the question, I understand it might be trivial to many, but it can help someone else to see this answer.

The issue was (as @xarantolus commented) that my PATH did not contain the route to my /Applications folder. Since I use zsh and mac, I did the following steps to fix it:

First find where firefox executable was:

$ type -a firefox

Printed route:

firefox is /Applications/Firefox.app/Contents/MacOS/firefox

Now open zshrc file:

$ vim ~/.zshrc

Inside the file, my $PATH was $HOME/bin:/usr/local/bin:$PATH, and I added /Applications so the line ended up like this:

export PATH=$HOME/bin:/usr/local/bin:/Applications/:$PATH

Notice that :$PATH will be at end also after adding the new path

Then run command to reload .zshrc

source ~/.zshrc

If you dont use zsh, use the file .bashrc instead of .zshrc

Go could now see the firefox executable and it opened it as expected.

答案2

得分: 1

在Mac上,有一个可用的命令叫做open,它可以打开默认的浏览器。

我和你有同一本书。我希望作者能修复这个问题。无论主机操作系统如何,都能够打开默认的浏览器会很好。可能有一种方法。我没有查找类似于Linux或Windows上的open命令。

https://gist.github.com/ivorscott/3ddba5527e5a96136bf870a57a37e721

英文:

On Mac you have a command available called open which opens the default browser.

I have the same book as you. I hope the author fixes this. Would be nice to open the default browser regardless of the host operating system. There might be a way. I didn't look for a command similar to open on linux or windows.

https://gist.github.com/ivorscott/3ddba5527e5a96136bf870a57a37e721

huangapple
  • 本文由 发表于 2020年6月2日 03:45:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/62139838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定