Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Compile and Execute Java, C#, and PHP From Your Favorite Text Editors

DZone's Guide to

Compile and Execute Java, C#, and PHP From Your Favorite Text Editors

How to use Sublime Text and Notepad as compilers to execute Java, C#, and PHP code.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 installed, otherise 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:

  1. Open "Command Prompt" with administrator rights i.e "Run as administrator".

  2. Execute the following command: SETX /M PATH "%PATH%;C:\Program Files\Java\jdk1.8.0_20\bin"

  3. My javac executable was located in: C:\Program Files\Java\jdk1.8.0_20\

  4. 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:

Change To:

{
  "cmd": ["javac $file && java $file_base_name"],
  "shell": true,
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.java"
}

I have modified the value for "cmd" key and changed it to first compile and build the Java program with the "javac" executable, then to run it with "Java" executable.

Step 3

Restart Sublime Text.

Step 4

Create or 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 is installed, otherwise 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, then 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, then rename "untitled.sublime-build" to "PHP.sublime-build"

Step 5

Restart Sublime Text.

Step 6

Create or 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 is present, otherwise 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, then rename "untitled.sublime-build" to "CSharp.sublime-build"

Step 5

Restart Sublime Text.

Step 6

Create or open a C# file and press Ctrl+B to build and execute the C# program. See the example below:

Notepad++

Step 1

Install the 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 the NPPExec plugin is installed and configured, we can move on to create an "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 or 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 the 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 the screenshot in Step 6 in the "Compile and Execute Java From Notepad++" section):

Step 7

Once the shortcut has been assigned, create or 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 the NPPExec plugin is installed and configured, we can move on to create an "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 the "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 the Settings-Shortcut Mapper... (Refer to screenshot in Step#6 in the "Compile and Execute Java From Notepad++" section):

Step 7

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

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,c# ,php ,sublime text ,notepad++

Published at DZone with permission of Robin Rizvi. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}