Wednesday 7 September 2016

Configer Sublime Text Editor to compile and run java program


Sublime Text is an editor which has the balance between simplicity and power. Many programmers are using Sublime for their daily programming.

       By default, Sublime can compile a Java source file as long as the Java compiler (javac) can be found via the PATH environment variable. So make sure you update this variable so Sublime can interact with the Java compiler.

To compile a Java source file from within Sublime make sure you have makred Tools>Build System>javaC as selected then press Ctrl + B (or click Tools > Build). If the Java compiler couldn’t be found, Sublime will display an error message like this:

[Error 2] The system cannot find the file specified
javac not found


Once you corrected the PATH variable, you may need to restart the editor to take effect. Then press Ctrl + B again, you would see the successful message like this:
finished compile



To Run the compiled java class you may need to do some extra steps.

To run a compiled class using sublime text you will need to create your own Tools>Build System>javaC like build configration e.g. Tools>Build System>myCustomeConfige or you can modify the exitsing configrations as well.

Follow the steps given and you will be able to run java program on linux using Sublime Text Editor

Go To Tools >Build System> New Build System
This will open a new file with name untiteld.sublime-build . This is actully a sublime build config file where we can put our own configation to operate any file using console. We will write some code here which will be called while we will press Ctrl + B (or click Tools > Build). To run java program copy and pest the following code in in this build file and save it with your custome config name like MyConfig.sublime-build.


{
"shell_cmd": "javac -Xlint \"${file}\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"working_dir": "${file_path}",
"selector": "source.java",


"variants":
[
{
"name": "Run",
"shell_cmd": "java \"${file_base_name}\""
}
]
}


Save the file on /home/userSk/.config/sublime-text-3/Packages/User/Java , That's it

Now go to Tools>Build System here you will be able to see you own build confige with name MyConfig. Now mark Tools>Build System>MyConfig as selected and press Shift +Ctrl+ B (or click Tools > Build With) . It will show you two commands MyConfig and MyConfig - Run. If you will select MyConfig it will compile your code or if will select MyConfig – Run it will run the class. You can also do Compile and Run in sigle hit of Ctrl + B (or click Tools > Build) by making relevnt configration.


For more information goto the link http://docs.sublimetext.info/en/latest/reference/build_systems/configuration.html

No comments:

Post a Comment

Configer Sublime Text Editor to compile and run java program

Sublime Text is an editor which has the balance between simplicity and power. Many programmers are using Sublime for their daily program...