Home > Software design >  How to provide command line values to Java args within VS Code?
How to provide command line values to Java args within VS Code?

Time:02-23

I try to run a Java program within VS Code and keep on getting the following error due to not able to pass in values for args in main() by clicking the "run java" icon.

'C:\Program Files\Java\jdk-17\bin\java.exe' '--enable-preview' '-XX: ShowCodeDetailsInExceptionMessages' '-cp' 'C:\Users\llee\AppData\Roaming\Code\User\workspaceStorage\6a201af46b5315f239f3bba771ed2cc7\redhat.java\jdt_ws\Lecture 1_b97312ef\bin' 'Sort_Three' 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 
out of bounds for length 0
        at Sort_Three.main(Sort_Three.java:3)

I understand that variable args is null and there is no value passed to the program. So, what should I do to pass values for VS Code by clicking on the "run java" icon?

If I run the program outside VS Code on a command line, the program works fine.

PS C:\Users\Desktop\Exercises\Lecture 1> java Sort_Three 3 1 2
1
2
3

CodePudding user response:

You need to create a Run/Debug Configuration that includes an args array

It'll create a .vscode/launch.json file with at least this content

{
... 
"type": "java",
"mainClass": "Sort_Three",
"args": [3, 1, 2]
}
  • Related