Debugging Java with Visual Studio Code (VSCode)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's say I want to create a very simple hello world Java program here I can create a Java file using this icon or using file and then new file or I can simply right click here and then click on new file here so I'm going to name my file as hello dot Java and once I do that I will just press enter and once you create this file you can see Visual Studio code is going to automatically detect the file extension and you will be able to see this language here you can also see projects are imported into workspace so let me just create a very simple hello class in this hello.java file so I'm going to just type class here and you can see Visual Studio code is going to detect that you are trying to create a class in this hello.java file and when you just select the second option here you can see Visual Studio code has automatically created this hello class for me now in order to create a main method inside this hello class I can just type main here and once again Visual Studio code is going to show you intellisense and show you all these options and it has detected that I want to create the main method inside this class and I'm going to just select the first option here which is going to create the main method inside this hello class from me automatically so how simple this is right now let's say I want to just type hello world I can just write system dot out dot print line and at every step you will be able to see this intellisense so I will be able to complete my code faster and then let me just print hello volt here and it's that simple to create your first hello world Java program right now how you can run your Java programs so you for that you have few options here the obvious one is this one which is this run button and it's going to run your Java program the second option is to just right click on your Java file and when you scroll down and you will be able to see this run Java option and the bug Java option so you can use these two options to run and debug your Java program or you will see this option here on the top of your main method run or debug so you can also use one of these options to run your program or debug your program you also have this run option here and it's going to allow you to start debugging and all the other options related to debugging are available here also so let's run our program so I'm going to just click on this run button which is going to compile our Java program and for the first time it's going to show me this warning I'm going to just click on allow access and you can see Visual Studio code has already compiled our code and ran our code and the output is printed here now let's say you forget to type the semicolon here Visual Studio code is going to show you this red error and it's also going to show you what can be this error so you see it's a syntax error and we need to insert the semicolon at the last right so it's going to help you to detect the errors in your Java code also now let's say you have written the wrong code which will not compile but then also you want to just compile it then you will see this error here in the problems Tab and you will see the details of the error here right so the exact line number on which the error is coming and when you click on this error it's going to just redirect you to the particular line from which the error is coming so this is how you can detect the errors and fix them Also let's try to debug our code so you can click on this debug option here or you can click on this debug icon here on the left hand side and then you can click on run and debug option so I'm going to click on run and debug which is going to start the Java debugger and because I haven't added any breakpoint my debugger has ran successfully and it has printed hello world let's say I will just add a break point here all my break points I will be able to see here under breakpoints so I can add multiple breakpoints in my code using these red dots and I can keep the track of all the breakpoints under this breakpoints section I can disable all the breakpoint at once using this toggle active breakpoint button which is going to de-activate all the breakpoints if I want to remove all the breakpoints then I can just click on this icon which is going to remove all the breakpoints right let's say I have just added the breakpoint here and then run the debugger once again this time you can see the execution of my code has been stopped on the breakpoint and I will be able to see the argument values here and then I have all these options so I can continue using this button I can step over to the next line I can step into a method or I can step out from a method I can also restart my debugger stop my debugger and this option is for hot code replace option right so I have all these options here so now let me just stop this debugging from here and let me add some codes so let's say I just declared two number number one which will be equal to 5 let's say and then I'm going to declare a second variable which is called num2 and the value of it is 10 let's say right and I'm going to create a third variable which will be called let's say sum here and it will be the sum of num1 plus num 2 right so you will be able to see at each and every Point uh Visual Studio code is going to show us hint and intellisense to complete our code faster and now I want to just print the value of sum here right and now I have more debugging possibilities and let me just start the debugger once again I have added three break points here and here you can see that I will be able to keep the track of all the local variables here I can also watch for some variables here for example I want to watch for a variable called sum I can just add that but the program execution is still not there so right now cannot evaluate because of the compilation error right so let's step over to the next line you can see the value 5 is assigned to a variable num1 step once again now num2 has 10 and then step over once again now we have the sum which is equal to 15 and this variable we were watching right so it was in a watch list so now it shows the value right so you can see the global and local variable lists here you can watch for some variables and you have the track of all these break points here right so this is how you can debug your Java code on your Visual Studio code also have the possibility so let me just stop this debugger so you also have the possibility to create a launch.json file and here you can provide the configuration related to debugging on this launch.json file so you can click on this create a launch dot Json file so just click on this launch.json file which is going to create this launch.json file and you can see all the configuration related to the Java debugging and when you click on the Explorer option you will be able to see this dot vs code folder is created and under that you have this launch dot Json file so this is good when you are just creating your program and then just pushing it to some GitHub repository and then somebody else will be able to take your launch configuration and he will be able to use it for debugging your Java program right you also have this GitHub versioning for your source code so when you have get installed Visual Studio code is going to track all the changes which you make on your project in your Java files and it will allow you to commit or post your code or pull some repository from GitHub or any other repository right you can also search for some code using the search option and if these extensions are not enough you can search more Java related Extensions by just typing Java and and then you will be able to find more Java related extensions here so you can install them and use them for developing different Java framework so let us say I want to add the support for spring so I can just install the this extension and then develop spring based app in my visual studio code editor so this is how you can set up and use Java in your Visual Studio code editor I hope you've enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 18,396
Rating: undefined out of 5
Keywords: java debugger, how to run java code in vs code terminal, visual studio code java, java debugger online, vscode-java debugger not working, java debug command line, how to run java in vscode, launch.json vscode java
Id: R1F5ihQKL4U
Channel Id: undefined
Length: 11min 4sec (664 seconds)
Published: Tue Apr 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.