Sublime Forum

Java console input in Sublime Text 2 in MAC OS

#1

Hi all,
I’m a newbie in Java.
When I need input something from keyboard, Sublime Text 2 seems not supporting to do that.
For example:

import java.util.*;
class Scan{
	public static void main(String] args) {
			Scanner ca = new Scanner(System.in);
			System.out.print("Input your name: ");
			String ten = ca.nextLine();
			System.out.println(ten);
			}		
}

I got the following error:

Input your name: Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1540)
	at Scan.main(Scan.java:6)
[Finished in 1.3s with exit code 1]

I found out that No console input causes this problem.
How can I overcome this problem?
I searched and found this topic on Stackoverflow http://stackoverflow.com/questions/15289352/java-console-input-in-sublime-text-2 , but it’s useful for Windows’s users. I’m using MAC OS !!!

Any one has an idea for MAC OS??
Thank you for reading !!!

0 Likes

#2

Maybe we have the same goal.
github.com/joeyespo/sublimetext-console-exec

I just issue a pull request and work nicely :smiley:

0 Likes

#3

Thanks jfcherng!
I did:

  • Select Package Control: Install Package from the command palette
  • Locate ConsoleExec and press enter to install it
  • Change java.sublime-build file to:
{ 
  "cmd": "javac \"$file_name\" && java \"$file_base_name\""],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)", 
  "selector": "source.java", 
   "target": "console_exec"
} 

But when I BUILD, the Sublime just gave me the message: compile finished !!
No console appears :frowning:

Could you please tell me more detail? Remember that I’m using MAC OS.

0 Likes

#4

Could you test the following?
I don’t know what is the shortcut to Build and Run for Mac.
It’s Ctrl+Shift+B for Sublime Text 2 under Windows.
But you may found it under the menu bar Tools>>Build and Run.

{ "cmd": "javac", "$file_name"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java", "target": "console_exec", "variants": { "name": "Run", "cmd": "java", "$file_base_name"] } ] }

Reference: stackoverflow.com/questions/1994 … ime-text-2

0 Likes

#5

Thanks jfcherng!
It works fine in Windows. But on Mac OS it seems not working.
I will see more carefull on reference link and let you know.

0 Likes

#6

You may open the Sublime Text console (Ctrl+`) and see if there are some error message or not.

0 Likes

#7

There is no error in console.
Below is my Java build file. Could you tell me where I was wrong??

{ 
  "cmd": "javac \"$file_name\""],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)", 
  "selector": "source.java", 
   "shell":true,
   "target": "console_exec",
   "variants": 
        {   
            "name": "Run",
            "cmd": "java \"$file_base_name\""]
           
        }
    ]   
} 
0 Likes

#8

[quote=“phuongcei”]There is no error in console.
Below is my Java build file. Could you tell me where I was wrong??

[code]
{
“cmd”: “javac “$file_name””],
“file_regex”: “^(…?):([0-9]):?([0-9]*)”,
“selector”: “source.java”,
“shell”:true,
“target”: “console_exec”,
“variants”:
{
“name”: “Run”,
“cmd”: “java “$file_base_name””]

    }
]   

}
[/code][/quote]

Did you ever get this to work on Mac OS? I’m running into the same issue you were after installing the Console Exec package and trying to get it to accept console input…

0 Likes