How to open .JAVA files on Mac

To open .JAVA files on Mac, to view or edit: control-click the .java file → Open With → choose a code editor/IDE (or TextEdit configured for plain text).

Step-by-step instructions

  1. To view or edit: control-click the .java file → Open With → choose a code editor/IDE (or TextEdit configured for plain text).
  2. To compile: install a JDK, open Terminal in the file’s folder, and run: javac ClassName.java.
  3. If the file opens with the wrong app, change the default by using Get Info → Open with → Change All.

Common issues

The .java file opens in the wrong application

Because .java is plain text, the OS may associate it with a generic text app or an unrelated program.

  1. Use “Open with” and choose your preferred code editor/IDE.
  2. Set it as the default app for .java files (Windows: Properties → Opens with; macOS: Get Info → Open with → Change All; Linux: file manager defaults for the MIME type).

javac fails: file name and public class name do not match

Java conventions (and common compiler expectations) require the source file name to match the public class name (for example, public class Hello should be in Hello.java).

  1. Open the file and check the public class name.
  2. Rename the file to match (ClassName.java), or change the public class to match the file name, then re-run javac.

Command not found: javac

The Java compiler (javac) is provided by a JDK; it won’t be available if only a runtime is installed or if your PATH isn’t set.

  1. Install a Java Development Kit (JDK) for your platform.
  2. Verify javac is available in your terminal (and ensure your environment PATH includes the JDK’s bin directory), then compile again.

MIME type or file association issues on Linux

Linux desktop environments typically rely on the shared MIME-info database and local overrides; outdated or incorrect MIME info can lead to wrong associations.

  1. Check what your desktop thinks the file type is and adjust the default app for that MIME type.
  2. Update/refresh your desktop’s MIME database and file associations if they are outdated or misconfigured.

Security note

.java files are source code (plain text), but they can still be risky if you compile and run code you do not trust—treat unknown .java files like untrusted programs.

Back to .JAVA extension page