Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java console input in Sublime Text 2?

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.

like image 616
Quinnette Ribeiro'college Avatar asked Mar 08 '13 08:03

Quinnette Ribeiro'college


People also ask

What is Java console input?

The Java Console class is be used to get input from console. It provides methods to read texts and passwords. If you read password using Console class, it will not be displayed to the user.


2 Answers

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
like image 196
AsyncMoksha Avatar answered Oct 07 '22 00:10

AsyncMoksha


I was able to figure this out on Windows 8, I haven't tested it on any other platform, hope this works for you.

  1. Install Cygwin [http://www.cygwin.com/]

  2. Make sure the start commands uses the drive:\Path_to_cygwin_installation\bin\start

    • You can verify this with the where command :
          where start
  3. 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.
  4. Create your javac.bat file and make it available on your environment path

    
    @echo off
    javac  -Xlint:unchecked %~n1.java 
    start java -ea %~n1
    
    
  5. 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.

like image 38
lac_dev Avatar answered Oct 06 '22 23:10

lac_dev