It is a really long story, but essentially:
- Binary executable files are always platform-dependent. Usually we
say the OS+Architecture to denote platform. Examples: Windows x86,
Linux x86-64, etc. This is because they mostly execute with the help of operating systems which provide abstractions for the available hardware.
- Bytecode (not executable) interpretable files can be independently executed because they are interpreted by a binary platform-dependent one that is already installed on the machine.
Java code is converted to a bytecode interpretable file (.class). It is interpreted and executed by a platform-dependent Java Virtual Machine that can be installed on Windows, Linux, Mac platforms.
However, you can put effort to make your C/C++ code very platform-independent, but since you have different OS (and sometimes different Architecture), you must compile that (same) code on every system you want to run it, in order to build proper binaries that will run in that specific machines.
OBS: To make your code more independent you should use libraries that are available on all systems.
OBS2: To know more about executable files a good "questions" with good "answers" is: What does executable file actually contain?