Editors Choice

3/recent/post-list

Sublime Text Java Build Configuration with External Console (Windows & Linux)

Sublime Text Java Build Configuration with External Console (Windows & Linux)

Frustrated with Java programs closing instantly in Sublime Text? Want proper console input and persistent output? This guide will show you how to configure a Sublime Text Build System for Java that runs your programs in an external terminal—working seamlessly on both Windows and Linux!

Why Use an External Console for Java?

  • Proper Scanner input (no more skipped nextLine() calls)
  • Complete stack traces (full error visibility)
  • Persistent output (console stays open after execution)
  • Better debugging (see all your System.out messages)

Step 1: Create a Java Build System in Sublime Text

  1. Open Sublime Text
  2. Navigate to:
    • Tools → Build System → New Build System...
  3. Paste the appropriate configuration below

Windows Configuration:

{
    "shell_cmd": "javac \"${file}\"",
    "working_dir": "${file_path}",
    "selector": "source.java",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "shell_cmd": "start cmd /c \"java -cp \"${file_path}\" ${file_base_name} & pause\""
        },
        {
            "name": "Build & Run",
            "shell_cmd": "javac \"${file}\" && start cmd /c \"java -cp \"${file_path}\" ${file_base_name} & pause\""
        }
    ]
}

Linux Configuration:

{
    "shell_cmd": "javac \"${file}\"",
    "working_dir": "${file_path}",
    "selector": "source.java",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "shell_cmd": "gnome-terminal -- bash -c 'java -cp \"${file_path}\" ${file_base_name}; read -p \"Press Enter to exit\"'"
        },
        {
            "name": "Build & Run",
            "shell_cmd": "javac \"${file}\" && gnome-terminal -- bash -c 'java -cp \"${file_path}\" ${file_base_name}; read -p \"Press Enter to exit\"'"
        }
    ]
}
  1. Save the file as Java_External_Console.sublime-build

Step 2: Save Location for Build File

Windows Path:

C:\Users\<YourUsername>\AppData\Roaming\Sublime Text\Packages\User\

Linux Path:

~/.config/sublime-text/Packages/User/

Step 3: Using Your Java Build System

  1. Open a Java file (.java extension) in Sublime Text
  2. Select the build system:
    • Tools → Build System → Java_External_Console
  3. Execute your program:
    • Ctrl + B → Compile only
    • Ctrl + Shift + B → Choose "Build & Run" or "Run"

Bonus: Java-Specific Troubleshooting

"javac not recognized" error?

  • Install Java Development Kit (JDK)
  • Windows/Linux: Download JDK from Adoptium
  • Linux: sudo apt install default-jdk
  • Ensure Java is in your PATH (check with javac -version)

"Could not find or load main class"?

  • Ensure your class name matches the filename exactly
  • Check for proper package declarations
  • Verify the working directory is correct in build system

Advanced: Package Support

For projects using packages, modify the Run command to handle package paths:

"shell_cmd": "javac \"${file}\" && gnome-terminal -- bash -c 'java -cp \"${file_path}/../\" ${file_base_name}; read'"

Or for Windows:

"shell_cmd": "javac \"${file}\" && start cmd /c \"java -cp \"${file_path}/../\" ${file_base_name} & pause\""

Conclusion

You now have a fully functional Java development environment in Sublime Text with proper external console support! No more disappearing output or input issues. 🚀

🔹 Pro Tip: Combine this with the C++ build system for a powerful multi-language IDE setup!

Have questions or improvements? Share your thoughts below! 💬

Post a Comment

0 Comments