How to use Sublime Text 3 to compile and execute Java programmes?
Sublime Text 3 is a powerful source code editor with beautiful and customisable user interface, extraordinary features and amazing performances, and hence becomes a popular editing tool for Java. This post will introduce to you how to use Sublime Text 3 to compile and execute Java programmes directly without having to use Command Prompt (or Terminal for Mac users).
1. Compilation
2. Execution
Click "Tools", select "Build System" and click "New Build System". You should see something like this:
{ "shell_cmd": "make"
}
Change it into:
{ "shell_cmd": "java $file_base_name" }
Then save the file to the default path and rename it to the name of build system you want to see (e.g. JavaR). Now select "Build System" and you should see "JavaR". Build your code with this!
One problem with this solution is that it does not allow user input (restriction of Sublime Text 3). To solve this problem and achieve a perfect one-stop solution (compile and execute together in Sublime Text 3), I will introduce another way.
3. Compile and execute together!
I would recommend usage of "Terminus", an external package provided by Sublime Text 3, which allows Command Prompt (Terminal) to be run inside Sublime Text 3. To install it, follow the steps below:
- Check whether you have installed "package control". If so, you should be able to see it under "Preferences". If not, you could click "install package control" under "Tools".
- Click "package control" and select "install package".
- Key in "Terminus" and install it.
After successfully installing "Terminus", add a new customised build system (follow the steps in Method 2 Execution) as follows:
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "javac $file && cd $file_path && java $file_base_name",
}
Save it and now you should be able to see a terminal appear at the bottom when building with this. It looks like this:
Done! Happy programming :)
Comments
Post a Comment