Sublime Text Compile/ Build and Run Codes
I recently started with Sublime Text Editor, it's an amazing text editor with elegance and beauty. This editor has some of the most advanced features I have seen so far. For people who are using notepad++ this is a good alternative to choose from.
When I started I was little confused as to how to compile and Run codes with this editor.Here is a walk through.
When I started I was little confused as to how to compile and Run codes with this editor.Here is a walk through.
Compiling/ Building
I shall take java as an example rest languages can be configured similarly
- First open a new File and save it with a proper name say "Test.java"
- Open command Prompt and type javac to check if the java compiler path is correctly set.Type java at cmd and check if the command is recognizable.If any error comes set the Path environment variable for Java first.
- Then Goto Tools>Build System and check the Automatic or JavaC option.
- Then Press ctrl+B to build the console will display Finished in [time ] message.
- Now to Run you can either goto the directory containing the compiles file and open cmd there and type
- "java Test" to run or proceed to next step and learn how to setup a Build system to Run from sublime text.
Running The Compiled File / Making the New Build System
- Sublime uses its build-system files of corresponding languages to build the source code.So we can either make a new build system or edit the already exiting one.
- To make a new build system goto Tools>Build System>New Build System. Then Type the new Build System commands.
- But as I am using java here for which sublime text already has the build system, I prefer editing the current one , usually the file is located at
- C:\Users\{username}\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.sublime-build
- Open the file JavaC.sublime-build in sublime text and them add the below code snippet
{
"cmd": ["javac", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"variants": [
{ "cmd": ["javac", "-Xlint", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"name": "Java Lintter"
},
{ "cmd": ["java", "$file_base_name"],
"name": "Run Java"
}
]
}
Over here we are just adding two variant to the build system the default being the first one. The Last one with the name "Run Java" is the one that is gonna Run the compiled file. So that it Runs it is necessary that the file name and the class name in the source name with the main (taking about java here) should be same or else it wont find the file.
Save the file and then now to compile the code you need to press ctrl+b but to Run you just need to type ctrl+shift+p and then there type "Run java". That's it. The output will be displayed in the sublime text console.
Over here we are just adding two variant to the build system the default being the first one. The Last one with the name "Run Java" is the one that is gonna Run the compiled file. So that it Runs it is necessary that the file name and the class name in the source name with the main (taking about java here) should be same or else it wont find the file.
Save the file and then now to compile the code you need to press ctrl+b but to Run you just need to type ctrl+shift+p and then there type "Run java". That's it. The output will be displayed in the sublime text console.
0 comments