Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 326
o.o Avatar asked Mar 27 '26 01:03

o.o


1 Answers

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.

like image 116
MattDMo Avatar answered Mar 28 '26 14:03

MattDMo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!