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 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:
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:
Press Win+Pause key.
Follow as directed in the screenshot below:
Step 2
Customise Sublime's build configuration for Java:
Click on Preferences->Browse Packages
Navigate to Java Folder->JavaC.sublime-build
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: