27

I'm trying to run the command sudo apt update on my terminal in MacOS

I'm getting this message in response: The operation couldn’t be completed. Unable to locate a Java Runtime that supports apt. Please visit http://www.java.com for information on installing Java.

I saw a similar question here, however even though I made sure to install the JDK like the solution suggested I'm still getting the same response.

I also tried pasting

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"

Into my .zshrc.save folder and had no luck.

When I run java -version in the terminal this is what I get back:

java version "15.0.2" 2021-01-19
Java(TM) SE Runtime Environment (build 15.0.2+7-27)
Java HotSpot(TM) 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing)
Rachid O
  • 13,013
  • 15
  • 66
  • 92
User9123
  • 675
  • 1
  • 8
  • 20
  • Maybe it is getting confused with the old path of jdk. Did you delete the old the jdk or at least removed it from the path and JAVA_HOME. – AllMightyGoat Feb 18 '21 at 20:15
  • 1
    For clarity, a comment on a now-deleted answer shows that OP is trying to follow https://phoenixnap.com/kb/update-node-js-version which is a guide for linux, and the `apt` they mention is referring to debian/ubuntu package manager, not java's annotation processing tool. – rzwitserloot Feb 18 '21 at 20:22

4 Answers4

74

20 years ago, java shipped with a tool called apt: Annotation Processor Tool. This tool was obsolete not much later.

What that update-node-js-version is talking about, is a completely and totally unrelated tool: It's the Advanced Package Tool, which is a tool to manage installations on debian and ubuntu - linux distros. You do not want to run this on a mac, and the instructions you found are therefore completely useless: That is how to update node-js on linux. Your machine isn't linux.

Search around for answers involving brew, which is the go-to equivalent of apt on mac. And completely forget about java - this has NOTHING to do with java - that was just a pure coincidence.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • 1
    Thanks! That makes so much more sense now. For reference to anybody who didn't see earlier I was trying to follow this tutorial: https://phoenixnap.com/kb/update-node-js-version – User9123 Feb 18 '21 at 20:25
  • 1
    How do we use brew to do it instead of sudo apt? – Francisco Gutierrez Ramirez Nov 27 '21 at 05:17
  • The question does not mention what 'it' is, @FranciscoGutierrezRamirez – rzwitserloot Nov 27 '21 at 14:00
  • 2
    This is the answer I needed - was trying to use `sudo apt install certbot` stupidly and needed to use `brew install certbot` instead. Even Java can't name their stuff properly ‍♂️ – Ermiya Eskandary Dec 14 '21 at 12:17
  • @ErmiyaEskandary I don't think the debian team gets to just claim a 3-letter acronym. It was apple and linux distributions that decided to pile __every__ tool in a JDK's `bin` dir into your path which was never the intended design. Tragic, nobody is to blame for any of this however. – rzwitserloot Dec 14 '21 at 15:36
  • 1
    @rzwitserloot Agreed but these days, anyone saying `apt` will probably only think of the package manager, not the Java `apt` tool – Ermiya Eskandary Dec 14 '21 at 15:41
  • @ErmiyaEskandary That was true when oracle added 'apt' to the `bin` dir of OpenJDK distros too, but there are ~50 executables in there and we just do not name executables e.g. `org.openjdk.java-compiler-v16`. We name them `javac`. That's not OpenJDK's choosing. They didn't mess up this name. Nobody is individually 'at fault'. – rzwitserloot Dec 14 '21 at 16:27
  • ...uh...folks...all of the above and everything else here makes good sense. But maybe someone can explain how THIS fits into it all: https://formulae.brew.sh/formula/apt – CryptoFool Feb 28 '22 at 04:50
  • ugh thank you for this context!! this is exactly what i needed to know – Tina Aug 14 '22 at 04:19
  • @CryptoFool `apt` is an executable that does things. Specifically, it lets you interact with apt repositories. On its own it is useless unless you add apt repo sources. __On linux__, it ships out of the box with useful repo sources pre-configured. apt repo sources exist with packages ready to install on e.g. debian linux distributions. `brew install apt` installs the tool. This is a completely useless tool without a repo. Sometimes folks make their own apt repos (it's open source after all). `brew install apt` is always entirely useless. – rzwitserloot May 04 '23 at 23:55
  • @rzwitserloot - I don't understand the point of your response to me. I know exactly what "apt" is on a Debian-style Linux host, and I know that it is useless without its package repos. I was asking what the purpose of the "apt" tool is that exists in Brew...that is, is there an appropriate use for this tool on a Mac? Maybe I could have made my question clearer. Telling me that it is completely useless doesn't answer my question as to why there is a build of the tool in Brew. – CryptoFool May 05 '23 at 20:11
  • hmm, unfortunate typo, I meant to write '__almost__ entirely useless'. It's useful if you want to write and maintain your own mini apt repos. Hence the second-to-last-line: "Sometimes folks make their own apt repos". – rzwitserloot May 05 '23 at 20:18
4

Install Homebrew on your Mac Machine

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

For the system Java wrappers to find this JDK, symlink it with

sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

If you need to have openjdk first in your PATH, run:

echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.profile

For compilers to find openjdk you may need to set:

 export CPPFLAGS="-I/usr/local/opt/openjdk/include"
Nitin Jha
  • 1,485
  • 2
  • 10
  • 14
0

The below commands worked for me.

First, install the homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then set the Android Studio Java path to the Home(If you have Android Studio). If not then you take the respective Java path & export it to the JAVA Home path.

export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home

Bharat Lalwani
  • 1,277
  • 15
  • 17
0

Use brew instead of sudo apt, if you're using Mac.

zhxh
  • 51
  • 1
  • 3