I've recently made the switch to sublime text 2 but I cannot seem to find any plugins/resources which will allow me to implement java console inputs into the editor. I've managed to make it compile and execute java files, but whenever my code needs any input(like a scanner input), the code does not compile and I get an error.
I've seen solutions to make this happen for python, but haven'y managed to find anything on Java.
Okay, I've figured out a complete and perfect solution to this "Run java in Sublime" problem, I've only tested this in Windows 7.
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 (mainly the same as @lac_dev 's answer)
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
I was able to figure this out on Windows 8, I haven't tested it on any other platform, hope this works for you.
Install Cygwin [http://www.cygwin.com/]
Make sure the start commands uses the drive:\Path_to_cygwin_installation\bin\start
- You can verify this with the where command :
where start
Create a javac.sublime.build
system
- This is mine, it can be improved
{
"cmd": ["javac.bat", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
- Make sure you update your environment path so it knows where to locate the
javac.bat
; I placed mine under my java_home
installation.
Create your javac.bat
file and make it available on your environment path
@echo off
javac -Xlint:unchecked %~n1.java
start java -ea %~n1
On my example I had to make sure I kept the application opened:
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int input = -1;
while(input != 0)
{
System.out.println("=======================");
System.out.println("What is Your Age? Enter 0 to exit;");
input = reader.nextInt();
System.out.println("You've entered " + input + "\n\n");
}
}
}
You can now compile your java files and get it on a new windows.
Unfortunately, there are no solutions available for Sublime Text 2 or 3.
But I still love it! So what I usually do is just manually specify the input:
System.out.print("Enter Stack size: ");
//int input = br.readLine();
int input = 10;
leaving the necessary parts commented for when I need to run the program with input (almost never, in fact).