2

Because on Apple with M1, M2 ... (Apple Silicon) it is possible to install both JDK for arm (aka arm64) or for Intel CPU (aka amd64), and it will just work, there is problem to tell (to colleague on other mac) how to check

How to check that installed JDK on mac is for arm64 or amd64 ?

  1. From command line?
  2. From within Java program? (not the OS arch System.getProperty("os.arch"))

For example mvn -v used to be enough to tell all details.

mvn -v
Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
Maven home: /opt/homebrew/Cellar/maven/3.9.1/libexec
Java version: 17.0.6, vendor: Amazon.com Inc., runtime: /Users/user/Library/Java/JavaVirtualMachines/corretto-17.0.6-1/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "13.4", arch: "aarch64", family: "mac"

JDK java/javac don't give clue either.

javac -version
javac 17.0.6

java -version
openjdk version "17.0.6" 2023-01-17 LTS
OpenJDK Runtime Environment Corretto-17.0.6.10.1 (build 17.0.6+10-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.6.10.1 (build 17.0.6+10-LTS, mixed mode, sharing)

We also have JDK installed by scripts, JDK installed in docker container. There should be some common way to check.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • 2
    On linux I would do `file $(which javac)` and it would tell me what kind of architecture it was compiled for. Not sure it works on mac but worth a try. – Federico klez Culloca Jun 22 '23 at 15:49
  • When Java was developed I don't think anybody considered that an os may be able to execute more than one architecture. And the common way to check if an app uses Rosetta isn't feasible in Java: https://developer.apple.com/forums/thread/653009. The only way I see is by try and error e.g. by trying to load a library of MacOS you know that is arm64 via `System.load()`. If this works Java is an arm64 version. – Robert Jun 22 '23 at 16:55
  • @FedericoklezCulloca Great, that works on macOS, and could become accepted answer. – Paul Verest Jun 23 '23 at 15:35
  • @PaulVerest and an answer it is, now. – Federico klez Culloca Jun 23 '23 at 15:39
  • @Robert At least it is possibly from command line 'file $JAVA_HOME/bin/javac' So so it would be possible (thought no nice) to run similar from within Java – Paul Verest Jun 23 '23 at 15:40
  • Considering the answer from Federico you could search for a library that allows to parse MachO files and that can handle thin and fat binaries and allows to extract the info what architectures the contained Macho files require. Then you could read the Java binary that is currently executed to get the info. – Robert Jun 23 '23 at 17:37

1 Answers1

3

If you only need to check by hand you can use the file utility which, for executables, should tell you what architecture the binary was built for.

So assuming you want to know about the javac that's on your path you can run

file $(which javac)

Or, more generally

file /path/to/your/binary

And read its output.

An example:

$ file $JAVA_HOME/bin/javac

/Users/user/Library/Java/JavaVirtualMachines/corretto-1.8.0_362/Contents/Home/bin/javac: Mach-O 64-bit executable x86_64

$ file $JAVA_OTHER_HOME/bin/javac

/Users/user/Library/Java/JavaVirtualMachines/corretto-1.8.0_362-1/Contents/Home/bin/javac: Mach-O 64-bit executable arm64

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    Thanks! Sometimes knowing/using the correct words is already half way. Now I could find https://stackoverflow.com/questions/12105253/determine-jre-architecture-32-bit-vs-64-bit and https://stackoverflow.com/questions/2062020/how-can-i-tell-if-im-running-in-64-bit-jvm-or-32-bit-jvm-from-within-a-program that used to be common problem. – Paul Verest Jun 23 '23 at 15:58