If you’ve never installed Java on Mac, then you can simply download and install the latest version of Java from Oracle which is known as Java 8 but rather confusingly officially called Java 1.8 by Oracle. It’s because an application you have installed or a website you’re trying to access requires Java to launch or work properly.
To use the “java” command line tool you need to install a Java SDK