Compiling and Running Java Code in Sublime Text 2
Solution 1:
So this is what i added to the JavaC.sublime-build file
{
"cmd": ["javac", "-Xlint", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"variants": [
{ "cmd": ["javac", "-Xlint", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"name": "Java Lintter"
},
{ "cmd": ["java", "$file_base_name"],
"name": "Run Java"
}
]
}
What this does is that it creates variants to the regular build command (ctrl+b). With ctrl+b you will still be able to compile your code. If you do shift+ctrl+b the first variant will be executed, which in this case is javac with the -Xlint option. The second and final variant is the java command itself. you can place this as your first variant and shift+ctrl+b will actually execute the java code.
Also, notice that each variant as a "name". This basically allows this specific "build" option to show up in the shift+ctrl+p option. So using this configuration, you can simply do shift+ctrl+p and type "Run Java" and hit enter, and your code will execute.
Hope this helped.
Solution 2:
I find the method in the post Compile and Run Java programs with Sublime Text 2 works well and is a little more convenient than the other methods. Here is a link to the archived page.
For Windows:
Step 1:
Create runJava.bat
with the following code.
@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 jdk bin directory.
Step 2:
- Open Sublime package directory using Preferences > Browse Packages..
- Go to Java Folder
- Open JavaC.sublime-build and replace line
"cmd": ["javac", "$file"],
with"cmd": ["runJava.bat", "$file"],
Done!
Write programs and Run using CTRL + B
Note: Instructions are different for Sublime 3.
Solution 3:
Sublime Text 3 has a slightly different solution. This is a modification of vijay's answer, which I was using before.
{
"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}\""
}
]
}
Paste the above into a new file called JavaC.sublime-build
and put it into your User packages. This can be found in C:\Users\You\AppData\Roaming\Sublime Text 3\Packages\User
.
Ctrl-B will compile. Ctrl-Shift-B will run.
Solution 4:
This version of JavaC.sublime-build which is edited from vijay's answer works for me on both Windows 7 and Mac for Sublime Text 3.
I edited it so Ctrl+b or command+b is sufficient for both build + run.
{
"shell_cmd": "javac -Xlint $file && java $file_base_name",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"shell": true
}
'&&' ensures that the second part runs only when first part succeeds ie only when the class file is generated. You can find more related info here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
Solution 5:
compile and running as per documentation of sublime text 2 nd 3
step- 1: set environment variables for java as u know already or refer somewhere
strp-2: open new document and copy paste code below
{
"cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.java",
"shell":true }
step-3: save the document as userjavaC.sublime-build in directory C:\Users\myLapi\AppData\Roaming\Sublime Text 3\Packages\User
step-4:
after done select as tools->build systems->userjavaC
to both compile and run press ctrl+b