杰瑞科技汇

Sublime Text如何配置Java开发环境?

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.

Sublime Text如何配置Java开发环境?-图1
(图片来源网络,侵删)

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.

  1. Write Code: You'll write your Java code in Sublime Text.
  2. Save the File: You'll save the file with a .java extension (e.g., HelloWorld.java).
  3. 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.

  1. Check if Java is already installed: Open the Terminal (you can find it in Applications/Utilities or search for it with Spotlight Cmd + 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.

    Sublime Text如何配置Java开发环境?-图2
    (图片来源网络,侵删)
  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_HOME environment 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.
      1. 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).

      2. 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@17 in your terminal.
        export JAVA_HOME=$(/usr/libexec/java_home -v 17)
      3. Save the file and close the editor.
      4. Apply the changes by either restarting your Terminal or running the source command:
        source ~/.zshrc
    • Verify the installation: Run java -version and javac -version again in a new Terminal window. You should now see version numbers for both.

Step 2: Install Sublime Text

If you don't have Sublime Text yet:

  1. Go to the Sublime Text Download Page.
  2. Click "Download for Mac".
  3. Drag the Sublime Text app into your Applications folder.
  4. (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.

    Sublime Text如何配置Java开发环境?-图3
    (图片来源网络,侵删)

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.

  1. 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").
  2. 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 HelloWorld must be saved in a file named HelloWorld.java.

  3. 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 .class extension when running the program.

    You should see the output:

    Hello, World!

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.

  1. 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 javac and java commands 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-build into the default location that Sublime Text suggests (usually a Packages/User folder).
  2. Use the Build System:

    • Make sure your HelloWorld.java file is open in Sublime Text.
    • Go to Tools > Build System and ensure Java is 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!.

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.

  1. Install Package Control: If you don't have it, this is the first step. It's the package manager for Sublime Text.

  2. Install SublimeLinter and a Java Linter:

    • Open the Command Palette: Cmd + Shift + P.
    • Type Package Control: Install Package and press Enter.
    • In the new list that appears, type SublimeLinter and press Enter to install it.
    • Now, install a specific linter for Java. Go back to the Command Palette (Cmd + Shift + P), type Package Control: Install Package, and this time search for and install SublimeLinter-javac.
  3. See it in Action:

    • Open your HelloWorld.java file.
    • 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 javac in the background to find the error for you, even before you try to compile it.

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

分享:
扫描分享到社交APP
上一篇
下一篇