Click here to Skip to main content
15,884,875 members
Articles / Programming Languages / PHP
Article

Compile and execute Java, C# and PHP from your favourite text editors - Sublime Text 2 and Notepad++

Rate me:
Please Sign up or sign in to vote.
4.65/5 (5 votes)
19 Sep 2016MIT7 min read 40.5K   16   6
Compile and Execute Java, C#, and PHP From Your Favorite Text Editors for REPL (Read-Evaluate-Print-Loop) use cases


Note: The instructions below are based on Windows environment, but are adaptable for Mac/Linux environments.

Sublime Text 2

Compile and execute Java from Sublime Text 2:

Assuming JDK to be present, else you need to download that first. You can download it from this link: JDK8
 
Step 1. Add the directory that contains your "javac" executable to "PATH" environment variable:
Open "Command Prompt" with administrator rights i.e "Run as administrator".
Execute the following command: SETX /M PATH "%PATH%;C:\Program Files\Java\jdk1.8.0_20\bin"
My javac executable was located in: C:\Program Files\Java\jdk1.8.0_20\
Note: The SETX command will truncate the path to 1024 characters, so it could be destructive.
 
Alternative approach :
1. Press Win+Pause key.
2. Follow as directed in the screenshot below:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Step 2. Customize Sublime's build configuration for Java:
1. Click on Preferences -> Browse Packages
2. Navigate to Java Folder -> JavaC.sublime-build
3. Modify the contents of this file as:
   {
      "cmd": ["javac", "$file"],
      "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
      "selector": "source.java"
   }
change to
   {
      "cmd": ["javac $file && java $file_base_name"],
      "shell": true,
      "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
      "selector": "source.java"
   }
Note: I have modified the value for "cmd" key and changed it to first compile/build the java program with "javac" executable and then run it with "java" executable.
 
Step 3. Restart Sublime Text.
 
Step 4. Create/open a Java file and press Ctrl+B to build and execute the java program. See the example below:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Execute PHP from Sublime Text 2:

Assuming PHP to be present, else you need to download that first. You can download it from http://windows.php.net/download/ or http://php.net/downloads.php. PHP is already installed with WAMP, XAMPP and other stacks.
 
Step 1. Add the directory that contains your "php" executable to "PATH" environment variable.
Follow the same steps as demonstrated above.
My "php" executable was located in: C:\Servers\xampp\php
 
Step 2. Create a new build system for PHP. Follow as directed in the screenshot below:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Step 3. Clicking "New Build System..." will open a new file named "untitled.sublime-build". Replace the contents of this file with:
{
    "cmd": ["php $file"],
    "shell": true,
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.php"
}
 
Step 4. Press Ctrl+S to save this file, Rename "untitled.sublime-build" to "PHP.sublime-build"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Step 5. Restart Sublime Text.
 
Step 6. Create/open a PHP file and press Ctrl+B to build and execute the PHP script. See the example below:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Compile and execute C# from Sublime Text 2:

Assuming .NET framework to be present, else you need to download that first. You can download it from https://www.microsoft.com/en-in/download/details.aspx?id=30653.
 
Step 1. Add the directory that contains your "csc" executable to the "PATH" environment variable.
Follow the same steps as demonstrated above.
My "csc" executable was located in: C:\Windows\Microsoft.NET\Framework64\v4.0.30319
 
Step 2. Create a new build system for C#. Click on Tools->Build System->New Build System... in Sublime Text.
 
Step 3. Clicking "New Build System..." will open a new file named "untitled.sublime-build". Replace the contents of this file with:
{
    "cmd": ["csc $file && $file_base_name"],
    "shell": true,
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.cs"
}
 
Step 4. Press Ctrl+S to save this file, Rename "untitled.sublime-build" to "CSharp.sublime-build"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Step 5. Restart Sublime Text.
 
Step 6. Create/open a C# file and press Ctrl+B to build and execute the C# program. See the example below:

























 

Notepad++

Step 1. Install NPPExec plugin. Follow as directed in the screenshots below:

 
 

 

 

 

 

 

 

 

 

 

 

Step 2. (Optional) Configure NPPExec plugin to not display internal messages.

 

 

 

 

 

 

 

 

 

 

Compile and execute Java from Notepad++:

Once NPPExec plugin is installed and configured, we can move on to create "Execute" script for compiling and executing Java.
 
Step 1. Add the directory that contains your "javac" executable to "PATH" environment variable.
Follow the steps as demonstrated at the start of this article to configure the PATH.
 
Step 2. Create an "Execute" script to compile and run java.






















Step 3. Write the following commands in the "Command(s)" section and type in "JavaExecute" (can be any name here) in the "Script Name" section:
cd $(CURRENT_DIRECTORY)
javac $(FILE_NAME)
java $(NAME_PART)
For more information on Notepad++ environment variable, refer to this link: http://docs.notepad-plus-plus.org/index.php/External_Programs
 
 
 






















Step 4. Configure the "JavaExecute" script created above, to be listed as a macro. Follow as directed in the screenshots below:
 






















Step 5. Restart Notepad++.

Step 6. Once the "JavaExecute" script is listed as a macro, we need to map a keyboard shortcut to execute this macro so that we can compile and execute java directly. Follow as directed in the screenshots below:
 






















Step 7. Once the shortcut has been assigned, create/open a Java file and press Ctrl+Shift+B to build and execute the java program. See the example below:





















 

Execute PHP from Notepad++:

Once NPPExec plugin is installed and configured, we can move on to create "Execute" script for compiling and executing Java.
 
Step 1. Add the directory that contains your "php" executable to "PATH" environment variable.
Follow the steps as demonstrated at the start of this article to configure the PATH.
 
Step 2. Create an "Execute" script to run PHP. Click on Plugins-NppExec-Execute.. (Refer to screenshot in Step#2 in Compile and execute Java from Notepad++ section)
 
Step 3. Write the following commands in the "Command(s)" section and type in "PHPExecute" (can be any name here) in the "Script Name" section:
cd $(CURRENT_DIRECTORY)
php $(FILE_NAME)

 

























Step 4. Configure the "PHPExecute" script created above, to be listed as a macro. Follow as directed in the screenshots below. For bringing up the NppExec Advanced Options dialog, click on Plugins-NPPExec-Advanced Options... (Refer to screenshot in Step#4 in Compile and execute Java from Notepad++ section)
 
























Step 5. Restart Notepad++.

Step 6. Once the "PHPExecute" script is listed as a macro, we need to map a keyboard shortcut to execute this macro so that we can execute PHP directly. Follow as directed in the screenshots. For bringing up the Shortcut Mapper dialog, click on Settings-Shortcut Mapper... (Refer to screenshot in Step#6 in Compile and execute Java from Notepad++ section):
 
























Step 7. Once the shortcut has been assigned, create/open a PHP file and press Ctrl+Shift+C to execute the PHP program. See the example below:
 























 

Compile and execute C# from Notepad++:

Once NPPExec plugin is installed and configured, we can move on to create "Execute" script for compiling and executing Java.
 
Step 1. Add the directory that contains your "csc" executable to "PATH" environment variable.
Follow the steps as demonstrated at the start of this article to configure the PATH.
 
Step 2. Create an "Execute" script to compile and execute C#. Click on Plugins-NppExec-Execute.. (Refer to screenshot in Step#2 in Compile and execute Java from Notepad++ section)
 
Step 3. Write the following commands in the "Command(s)" section and type in "CSharpExecute" (can be any name here) in the "Script Name" section:
cd $(CURRENT_DIRECTORY)
csc $(FILE_NAME)
$(NAME_PART)

 
























Step 4. Configure the "CSharpExecute" script created above, to be listed as a macro. Follow as directed in the screenshots below. For bringing up the NppExec Advanced Options dialog, click on Plugins-NPPExec-Advanced Options... (Refer to screenshot in Step#4 in Compile and execute Java from Notepad++ section)
 























Step 5. Restart Notepad++.

Step 6. Once the "CSharpExecute" script is listed as a macro, we need to map a keyboard shortcut to execute this macro so that we can compile and execute C# directly. Follow as directed in the screenshots. For bringing up the Shortcut Mapper dialog, click on Settings-Shortcut Mapper... (Refer to screenshot in Step#6 in Compile and execute Java from Notepad++ section):
 























Step 7. Once the shortcut has been assigned, create/open a C# file and press Ctrl+Shift+N to compile and execute the C# program. See the example below:
 

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer Databorough India
India India
Currently working as software developer for Databorough India - Division of Fresche Legacy.

Developing for the open-source community and writing articles is my way of thanking the community. I have developed commercial as well as non-commercial/open-source projects for the web and windows as my work and hobby. Just trying very hard so that someday I could contribute a little for this world. I would like to send out my regards to all for your rating and comments because these comments keep me going. Thank you all.

Certifications:
Microsoft Certified Professional (Programming in C#)
Microsoft Certified Professional (Programming in HTML5 with JavaScript and CSS3)

Comments and Discussions

 
QuestionC# + Notepad++ alternative Pin
Oleg Shilo19-Sep-16 15:22
Oleg Shilo19-Sep-16 15:22 
Arguably, you will get much better C# support in Notepad++ with "CS-Script (C# Intellisense)". The plugin I presented some time ago here on CodeProject.
  • Single/multi file execution (as script or exe)
  • True Intellisense
  • Debugger (MS Mdbg included)
  • Integration with VS
  • Full VB.NET support (in the latest release)
Read more here: CS-Script for Notepad (C# intellisense and code execution)[^]
AnswerRe: C# + Notepad++ alternative Pin
Robin Rizvi19-Sep-16 23:07
Robin Rizvi19-Sep-16 23:07 
AnswerRe: C# + Notepad++ alternative Pin
Robin Rizvi19-Sep-16 23:09
Robin Rizvi19-Sep-16 23:09 
GeneralRe: C# + Notepad++ alternative Pin
Oleg Shilo20-Sep-16 0:49
Oleg Shilo20-Sep-16 0:49 
GeneralRe: C# + Notepad++ alternative Pin
Robin Rizvi20-Sep-16 1:07
Robin Rizvi20-Sep-16 1:07 
GeneralRe: C# + Notepad++ alternative Pin
Oleg Shilo20-Sep-16 1:21
Oleg Shilo20-Sep-16 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.