o.o
o.o

Reputation: 3751

SublimeText3 doesn't recognize Scanner class

I'm using a custom build system to compile and run java programs.

The build system:

{
     "cmd": ["C:\\Program Files (x86)\\Java\\jdk1.8.0\\bin\\javac", "$file"],
     "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
     "selector": "source.java",
     "variants":
     [
     {
          "name": "Run",
          "cmd": ["C:\\Program Files (x86)\\Java\\jdk1.8.0\\bin\\java.exe", "$file_base_name"]
     }
     ]
}

This works perfectly as long as I never ask for input. This is the error I get with this simple program:

Program:

import java.util.Scanner;

public class Test
{
    public static void main(String [] args)
    {
        Scanner leopard = new Scanner(System.in);

        int a = leopard.nextInt();
        System.out.println(a);
    }
}

Error:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Test.main(Test.java:9)
[Finished in 1.3s with exit code 1]
[cmd: ['C:\\Program Files (x86)\\Java\\jdk1.8.0\\bin\\java.exe', 'Test']]
[dir: C:\Users\yayu\Desktop]
[path: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Java\jdk1.8.0\bin]

My question is if there is any way to get the Scanner class working in SublimeText3?

Upvotes: 3

Views: 1648

Answers (1)

MattDMo
MattDMo

Reputation: 102902

No, there is not. As has been documented here extensively, Sublime does not support input when running programs directly inside the editor (such as using the build system you have). However, there is a workaround on Windows: run your program through an instance of cmd.exe first.

{
     "cmd": ["javac", "$file"],
     "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
     "selector": "source.java",
     "shell": true,

     "variants":
     [
     {
          "name": "Run",
          "cmd": ["start", "cmd", "/k", "java", "$file_base_name"],
          "shell": true
     }
     ]
}

The /k option keeps the window open after your program has finished running, so you can inspect output, errors, etc. If you want it to close immediately, just use /c instead. I removed the full path to the javac and java executables as it's already in your PATH, according to the error message. It keeps the build system cleaner and easier to understand.

Upvotes: 2

Related Questions