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
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.
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.
"cmd": ["javac", "$file"],
"cmd": ["runJava.bat", "$file"],
Done!
Write programs and Run using CTRL + B
Note: Instructions are different for Sublime 3.
This is how I did it with these easy steps:
Setup a new build system:
Tools > Build System > New Build System
Replace the default code with the following:
{
"cmd": ["javac","$file_name","&&","java","$file_base_name"],
"path": "C:\\Program Files\\Java\\jdk1.7.0_25\\bin\\",
"shell": true
}
// locate the path of your jdk installation and replace it with 'path'
Save the file by giving it a name (I named mine "Java")
Activate the build system:
By following the steps below, you will have 2 Build Systems in sublime - "JavaC" and "JavaC_Input".
"JavaC" would let you run code that doesn't require user input and display the results in sublime's terminal simulator, which is convenient and nice-looking. "JavaC_Input" lets you run code that requires user input in a separate terminal window, it's able to accept user input. You can also run non-input-requiring code in this build system, so if you don't mind the pop-up, you can just stick with this build system and don't switch. You switch between build systems from Tools -> Build System. And you compile&run code using ctrl+b.
Here are the steps to achieve this:
(note: Make sure you already have the basic setup of the java system: install JDK and set up correct CLASSPATH and PATH, I won't elaborate on this)
"JavaC" build system setup
1, Make a bat file with the following code, and save it under C:\Program Files\Java\jdk*\bin\ to keep everything together. Name the file "javacexec.bat".
@ECHO OFF
cd %~dp1
javac %~nx1
java %~n1
2, Then edit C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.sublime-build (if there isn't any, create one), the contents will be
{
"cmd": ["javacexec.bat", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
"JavaC_Input" build system setup
1, Install Cygwin [http://www.cygwin.com/]
2, Go to C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\, then create a file called "JavaC_Input.sublime-build" with the following content
{
"cmd": ["javacexec_input.bat", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
3, Make a bat file with the following code, and save it under C:\Program Files\Java\jdk*\bin\ to keep everything together. Name the file "javacexec_input.bat".
@echo off
javac -Xlint:unchecked %~n1.java
start cmd /k java -ea %~n1
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
If you are mac user than follow this steps:
open terminal go to your root dictionary by typing
cd ..
repeatedly. use
ls
to see if u have reach the root
you will see Library folder Now follow this path Library/Java/JVM/bin Once you get into bin you can see JavaC file
Now u need to get the path of this folder for that just write this command
pwd
Copy paste it to your sublime JavaC file and build java code in sublime by cmd+b.
The Build System JavaC works like a charm but fails when you want to give input from stdin in Sublime-text. But you can edit the build system to make it receive input from user. This is the modified JavaC build I'm using on Ubuntu 18.04 LTS. You can edit the build System or create a new build system.
To Create a new build system.
Copy Paste the Below code and File>>Save.
{
"shell_cmd": "javac \"$file\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"variants":
[
{
"shell_cmd":"bash -c \"javac $file\" && gnome-terminal -- bash -c \"java $file_base_name ;read\"",
"name": "Run"
}
]
}
To Edit the existing Java C build file