Of course! Setting up Java development on a Mac using Sublime Text is a very popular and efficient workflow. Here’s a comprehensive guide covering everything from installation to running your first program.

Overview of the Workflow
The core idea is to use Sublime Text as a powerful code editor and the Terminal (which comes built-in with macOS) for compiling and running Java code.
- Write Code: You'll write your Java code in Sublime Text.
- Save the File: You'll save the file with a
.javaextension (e.g.,HelloWorld.java). - Compile & Run: You'll open the Terminal, navigate to the directory where you saved the file, and use the Java compiler (
javac) and the Java Virtual Machine (java) to build and run your program.
Step 1: Install the Java Development Kit (JDK)
You can't compile or run Java code without the JDK. The JDK contains the javac (compiler) and java (virtual machine) tools.
-
Check if Java is already installed: Open the Terminal (you can find it in
Applications/Utilitiesor search for it with SpotlightCmd + Space). Type the following command and press Enter:java -version
If you see a version number (e.g.,
java version "17.0.2"), you're all set! Skip to Step 2.
(图片来源网络,侵删) -
Install the JDK (if not installed): The easiest way to manage Java versions on a Mac is using a tool called Homebrew.
- Install Homebrew (if you don't have it): Paste this command into your Terminal and press Enter. It will install the Homebrew package manager.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install the JDK using Homebrew: Once Homebrew is installed, run this command to install the latest LTS (Long-Term Support) version of the JDK (currently OpenJDK 17).
brew install openjdk@17
- Set up the
JAVA_HOMEenvironment variable: This is a crucial step that tells your system where to find Java. You need to add a line to your shell's configuration file.- Open your shell's config file. For modern macOS, it's likely
zsh. If you're not sure, just run this command:open ~/.zshrc
(If you use an older system, it might be
~/.bash_profile. If that file exists, open it instead). - Add the following line to the very end of the file. Note: The path might be slightly different depending on your version. You can find the exact path by running
brew info openjdk@17in your terminal.export JAVA_HOME=$(/usr/libexec/java_home -v 17)
- Save the file and close the editor.
- Apply the changes by either restarting your Terminal or running the
sourcecommand:source ~/.zshrc
- Open your shell's config file. For modern macOS, it's likely
- Verify the installation: Run
java -versionandjavac -versionagain in a new Terminal window. You should now see version numbers for both.
- Install Homebrew (if you don't have it): Paste this command into your Terminal and press Enter. It will install the Homebrew package manager.
Step 2: Install Sublime Text
If you don't have Sublime Text yet:
- Go to the Sublime Text Download Page.
- Click "Download for Mac".
- Drag the Sublime Text app into your
Applicationsfolder. - (Optional) To make it easier to launch from the Terminal, you can install the command-line tool:
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Now you can type
subl .in any folder to open Sublime Text to that directory.
(图片来源网络,侵删)
Step 3: Configure Sublime Text for Java
Sublime Text is a general-purpose editor, so we need to give it some help to work well with Java.
Method 1: The Simple Approach (No Plugins)
This method requires no extra software. You'll use Sublime Text to write the code and the Terminal to run it.
-
Create a Java file:
- Open Sublime Text.
- Go to
File > New File. - Save it as
HelloWorld.java(make sure the "Save as type" is "Plain Text").
-
Write your code: Copy and paste this classic "Hello, World!" program into the Sublime Text window.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }Important: The filename must exactly match the public class name. So,
public class HelloWorldmust be saved in a file namedHelloWorld.java. -
Compile and Run from the Terminal:
- Save the file in Sublime Text (
Cmd + S). - Open the Terminal.
- Navigate to the directory where you saved your file. For example, if you saved it on your Desktop:
cd ~/Desktop
- Compile the code: This creates the bytecode file (
HelloWorld.class).javac HelloWorld.java
- Run the program: This executes your compiled code.
java HelloWorld
Note: Notice you don't include the
.classextension when running the program.
You should see the output:
Hello, World! - Save the file in Sublime Text (
Method 2: The Advanced Approach (With Build System)
This method lets you run your code directly from within Sublime Text using a shortcut (Cmd + B), which is much more convenient.
-
Create a Build System:
- In Sublime Text, go to
Tools > Build System > New Build System.... - A new, empty file will open. Delete all the default text and paste the following configuration. This script automatically finds the
javacandjavacommands and handles the file names for you.
{ "shell_cmd": "javac \"$file_name\" && java \"$file_base_name\"", "selector": "source.java", "working_dir": "$file_path", "encoding": "utf-8" }- Save this file with a descriptive name like
Java.sublime-buildinto the default location that Sublime Text suggests (usually aPackages/Userfolder).
- In Sublime Text, go to
-
Use the Build System:
- Make sure your
HelloWorld.javafile is open in Sublime Text. - Go to
Tools > Build Systemand ensureJavais selected. - Now, simply press
Cmd + B. - The Sublime Text console pane at the bottom will show the compilation and execution output, and you'll see
Hello, World!.
- Make sure your
Step 4: (Optional but Recommended) Install a Package for Enhanced Features
Sublime Text's real power comes from its packages. The most popular one for Java development is SublimeLinter, which provides real-time error checking.
-
Install Package Control: If you don't have it, this is the first step. It's the package manager for Sublime Text.
- Open the Sublime Text console:
View > Show Console. - Paste the code for your version of Sublime Text from the official Package Control installation page and press Enter.
- Restart Sublime Text.
- Open the Sublime Text console:
-
Install SublimeLinter and a Java Linter:
- Open the Command Palette:
Cmd + Shift + P. - Type
Package Control: Install Packageand press Enter. - In the new list that appears, type
SublimeLinterand press Enter to install it. - Now, install a specific linter for Java. Go back to the Command Palette (
Cmd + Shift + P), typePackage Control: Install Package, and this time search for and installSublimeLinter-javac.
- Open the Command Palette:
-
See it in Action:
- Open your
HelloWorld.javafile. - Intentionally introduce a syntax error, like removing the semicolon at the end of
System.out.println("Hello, World!");. - You will immediately see a red underline in the editor. SublimeLinter is using
javacin the background to find the error for you, even before you try to compile it.
- Open your
Summary of Your Workflow
| Task | How to Do It |
|---|---|
| Write Code | Open .java files in Sublime Text. |
| Compile & Run (Manual) | Use Terminal: javac YourFile.java, then java YourFile. |
| Compile & Run (Easy) | In Sublime Text: Press Cmd + B (after setting up the build system). |
| Check for Errors | Install SublimeLinter and SublimeLinter-javac for real-time feedback. |
This setup gives you a lightweight, fast, and powerful Java development environment on your Mac. Happy coding
