I am trying to compile and run Java code in Sublime Text 2. Don\'t just tell me to do it manually in the Command Prompt. Can anyone tell me how?
Btw, I am on Windows
For Sublime Text 3
in "C:\Program Files\Sublime Text 3\Packages" you get java.sublime-package copy it to another folder change its extension from .sublime-package to zip and extract it you get JavaC.sublime-build file for your Modifications as above.
after all modifications extracted java folder again convert to .zip and change its extension .zip to .sublime-package. after that copy and paste this file to C:\Program Files\Sublime Text 3\Packages.
this will help you!
(even you get my file from http://upfile.mobi/363820 or http://www.4shared.com/file/73SkzY-Kba/Java.html link I use to run java code i use trick of "Wesley Baugh" so you need to copy runJava.bat file to your C:\Program Files (x86)\Java\jdk1.8.0\bin directory as he says. and copy above linked file to C:\Program Files\Sublime Text 3\Packages)
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.
I followed a post in this thread and got it working perfectly:
Make the bat file with the following, and save it anywhere in your PATH. I suggest C:\Program Files\Java\jdk*\bin\ to keep everything together.
@ECHO OFF
cd %~dp1
javac %~nx1
java %~n1
then edit C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.sublime-build, the contents will be
{
"cmd": ["javac", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
replace "javac" with the name of your bat file (for instance, javacexec.bat) and save it.
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
I am using Windows 7. The below solution works for me!!
**Open** the file JavaC.sublime-build and replace all the code in the file with the code below:
{
"cmd": ["javac", "$file_name","&&","java", "$file_base_name"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
**"path": "C:\\Program Files\\Java\\jdk1.6.0\\bin\\",**
"selector": "source.java",
"shell": true
}
Remember to replace "C:\Program Files\Java\jdk1.6.0\bin\" with the path where you put your jdk. And make sure to add the path of you java JDK to the environment variable "PATH". Refer to bunnyDrug's post to set up the environment variable. Best!!
Alex Yao's Answer is the simplest solution, if you just want to build and run Java program w/o taking any input from the console use the solution provided by Alex Yao. However if you would like to take input from the console then refer to the following link
Java console input in Sublime Text 2?