Sublime Text 2 is my favorite text editor to encode, and if you try to use it, you'll like it. In this article we will discuss how to compile and run Java programs in Sublime Text 2.
First step: Set the Java PATH variable
This is the first and most basic step to setting up a storage path for compiling and running Java Program basic commands such as Javac and Java.
To set the path path under Windows:
- Right button "My Computer"
- Select "Properties"
- Enter Advanced system settings
- Find and click the "Environment variables" button
- Enter the system variable and locate the PATH variable
- Paste the JDK's Bin directory to the end of the PATH variable value
For example, your JDK's bin path is C:\Program Files\java\jdk1.6.0_17\bin, and the pasted effect is shown in the following illustration
Set the PATH variable under Ubuntu
1. Enter the/ETC directory in the console
2. Open the Environment file with Administrator privileges:
Sudo gedit/etc/environment
3. Paste the JDK's Bin directory to the end of the path variable and save the file Exit editor
4. Log off and log in again
Step #2. Create a batch or shell script
to build a Java program to run, you need to create a batch or shell script
For Windows
Create a file using the following code Runjava.bat
@ECHO off
cd%~dp1
ECHO compiling%~nx1
.... If EXIST%~n1.class (
DEL%~n1.class
)
javac%~nx1
if EXIST%~n1.class (
ECHO----------- OUTPUT-----------
java%~n1
)
Copy this file to the JDK's Bin directory
For Ubuntu
Create a file using the following code runjava.sh
[f ' $1.class] && rm $1.class for
file in $1.java do
echo compiling $file ... "
Javac $ File
done
if [f "$1.class"]
then
echo "-----------OUTPUT-----------"
Java $
Else
echo ""
fi
→note: If you want to compile all the Java files, you need to replace the $1.java in the second row with *.java
Use the following command to move this script file to the JDK's Bin directory
Sudo MV Runjava.sh/usr/lib/jvm/jdk1.6.0_17/bin
The executable permission to set the file after the move is 755 to make sure that the file is executable, and you can select the executable option on the tab of the permission by right-clicking the property.
Step #3-Modify Javac.sublime-build
follow these steps to modify the sublime Text 2 compilation system script.
- In Tab Preferences > Browse Packages ... Open the Sublime package directory
- Go to Java Folder
- Open Javac.sublime-build Replace the following command line
"cmd": ["Javac", "$file"],
Use the following command under Windows to replace the
"cmd": ["Runjava.bat", "$file"],
Use the following commands under Ubuntu to replace
"cmd": ["runjava.sh", "$file _base_name"],
Step #4 – now write a test program, use Ctrl+b to run the next try it!
You can see that the console compiles and runs programs