Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide user input to java code during Debug mode in Visual Studio Code

I'm using Visual Studio Code to develop some code in Java. I tried a simple code as shown below :

    int no_friends;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter values");
    no_friends = sc.nextInt();
    System.out.println("Value entered is : " + no_friends);

I placed a breakpoint on the sc.nextInt() line and the debugger successfully stopped this line. However, I did not find a way to give an input to the program while it is in debug mode. Is there a way to pass user input values to the code during debug mode ?

like image 581
Johan Fernandes Avatar asked Mar 08 '23 11:03

Johan Fernandes


2 Answers

Replace the part in the launch.json with

{
    "type": "java",
    "name": "Debug (Launch)",
    "request": "launch",
    "cwd": "${workspaceFolder}",
    "console": "externalTerminal",
    "stopOnEntry": false,
    "mainClass": "",
    "args": ""
}
like image 56
andres murcia Avatar answered Mar 10 '23 01:03

andres murcia


Changing the console property to "integratedTerminal" in launch.json fixed the issue for for me.

"console": "integratedTerminal"

Hope that helps.

like image 38
levenshtein Avatar answered Mar 10 '23 00:03

levenshtein