Java and VSCode: Tips and Tricks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so hi everyone welcome thanks for tuning in um so my name is layani i'm a soft engineer at citibank and today i would like to share with you a few tips and tricks so you can use java on visual studio code and i hope these tips can help you you know if you haven't adopted yet or maybe even give it a try so i have um prepared here four different types of topics that i would like to go through with you um so first things first setting up the environment and this goes if you are already an experienced java developer and you you only want to try it out to use visual studio code or if maybe you're a student and you're you want to start getting uh you want to start learning java so this tip is going to be for both of you also i'm going to talk about some useful extensions of the basic extensions and some other ones that i also used and some basic java functionality when i posted this on twitter what you would like to see in this presentation and a lot of people reply they would like to see some of the common functionalities that are already available in other ide such as eclipse and intellij idea so if they could use the same thing here on visual studio code and spoiler the answer is yes and i'm gonna show you a few things and uh last um we're gonna see how we can create and one run projects different kind of projects and also how we can run and debug tests and hopefully i'll have enough time to show you everything that i have prepared for so first things first before we get started documentation is your best friend so whenever we are learning a new tool or new language or technology it's always a good idea to go first to the documentation and visual studio code documentation is really good so when we open up the page and here you have a screenshot of the the java documentation for visual studio code the first thing that you see is how to install visual studio code for java now if you don't have the jdk installed if you have never done any hello world with java before and you want to get started this tip is for you because um as you can see on the screen we have two special coding packs for java this means that when you install this coding pack for java uh it's gonna install the open jdk so you're gonna install the java development toolkit that is required for you to compile and run the java programs locally in your computer second thing is going to be visual pseudo code of course and the third thing are the basic extension pack that is required in order for visual studio code to have the support for java and right now we have windows and mac last year when we does when we this was released we only had a support for windows now we also have support for mac os and i hope in the future we also have uh support for linux and so we can have coverage for all the three operating systems that are supported by uh visual studio code okay so if you have never worked with java before this is the way to go however um if you already have java installed maybe the jdk if you're using any other editors or ides and you like to try it a visual studio code how do you do that so first things first if you already have visual studio code installed the first thing to do is to install some extensions back now we have two major extension specs here that i love the first one is the java extension pack which adds the basic functionality so this is all that you need in order to get started working with java in vs code which is the java extension pack by microsoft the second one is uh of the spring boot extension pack this one i really like because i'm also a spring boot developer so if you're using micro profile or quarkus there is also an extension pack for that but for spring boot especially this is the way to go and i've ended up creating my own extension pack just making a reference to the existing uh the existing extensions with a few other extensions that i find very useful such as tonkat if you're not a tomcat fan if you prefer dirty jetty you can also install jetty there's also xml because especially for legacy projects we're always gonna end up needing xml lombok and i know some people love lombok other people don't like it so if you don't like it you have to install it but if you do use lombok we do have an extension to add the support the same thing as we have in other ides um the break it pair colorize colorizer this is going to make easier to read java code especially when we have a lot of nested loops or a lot of if elves if else's and streams reactive programming with spring webflux and also some lambda expressions so that's really helpful and help us to read the code more easily um sumnerlind for me is a must-have well i work for a corporate uh company so sonarli sonarlint is something that we cannot even get past to uh in the to merge the pr if you don't get everything fixed and of course check style for java just to make sure that all developers are following the code conventions and of course docker so if you're using spring boot especially the latest version the the latest versions there is also a good probability that you're also going to need docker or if you don't that's always a good opportunity for you to learn and get started with some new technology and also if you already have the jdk installed what you can do you can go back to the settings in visual studio code and you can set your java home so you have the current version and especially if you're working with multiple projects that do require different versions of java you can also configure what are the different versions in my case i have java 8 11 and the latest one which is java 15 installed and i have a few other customizations as well and everything is available in visual studio code and i'm gonna show you in just a minute um one tip um so one tip independently if you are if you have existing projects or if you're just getting started so this is something that i always have to open up whenever i'm opening a new java project and um if you see right here we have this checkbox so make sure you have that checked because what it does is every time that you open a java project in visual studio code these will show up and here you have all the major shortcuts and also of course you have the link for the documentation remember again documentation is your best friend in this case where you can always go back to and you know read something that you need to to to reference um and create new projects and it also shows some of the extensions that you don't have installed so in my case for this particular workspace i don't have a micro profile in quercus but of course if you use quarkus and microprofile you can also install those as well and with just a click it's easy to to do that now um going to the basic functionality that we have in visual studio code compared to other uh editors and ide since this this was a question a reply that i got a lot um and we the answer is yes we do have this basically the same functionality that we have in other ides the first one uh for instance whenever we are creating just plain java classes the the pojos or the models right um we just declare the class and all the attributes and then the first thing that you're going to do if you're not using lombok is of course create your getters and setters constructors that you need uh the methods to string equals hash code and whatnot and we can do that so all you have to do is right click and in visual studio code you're going to learn that we do a lot of things with the right click so right click and then you have the options available for you in this case is source action excuse me um and we can select what's the option that we need do you want to generate getters and setters do we want to generate a constructor so you don't have to code anything manually the ide can in vs code can do that for you and we always get that pop-up on the top with the option so we can customize our selection and get the code exactly as we need and of course if you're working especially with legacy projects or big projects where we're going to have design patterns implemented so we have a lot of interfaces and you have implementation of those interfaces and you are making a reference of the interfaces in your classes but you're actually looking for for the actual implementation um so we do have all this functionality also just right click and you're good to go go to the implementations go to the definition find all the references find all the implementations you can see also the the three with all the uh with all the implementations all the places where a particular class or a particular method is being used so the same thing that we get in the other ides basically and the other tip and this is something that i really really like too because i never memorized especially when i'm working with spring boot projects and i do like properties as bruno i don't like yom very much so i do prefer to use properties but you get the same thing on a yaml file as well but in case of properties file if you don't remember what are the options that are available but you do remember only a like a a word or something that you you want to make a reference to in that case what are the hibernate options that we have in a property file in the springbook project so we can just start typing hibernate and we get the full intellisense as well and this for me is a must-have and it helps me a lot because i never remember what to put in this property files because i'm not creating java projects every day but mostly maintaining existing projects so that's really helpful okay so this was just a gist that i wanted to show to you so i want to spend the next 10 minutes show you hands-on how we actually can use visual studio code with java development and i'm quickly going to um switch here to visual studio code i have this project right here this is a servlet crud so this is a basic crud using java ee or jakarta ee as where uh we um say nowadays and as i mentioned this java overview is really uh helpful because if you have your project uh if you're missing something uh or if you wanna install the keybinding so if you are very used to using the eclipse shortcuts or intellij shortcuts you can install the same shortcuts here in visual studio code and i just installed the klipsky bindings here for me so i can use the same thing for example in eclipse one thing that i use a lot to find all the classes is the ctrl shift r where i can look up for a particular class name so i can do the same thing right here i just type ctrl shift r and i have all the information and i can search for the for the class that i'm looking for and of course vs code also has the one by default which is comment p or ctrl p which basically does the same thing so you can even use both if you want to now so as i mentioned before this is a uh jakarta ee or java ee project that we have right here so we have a pum.xml and i did create this project entirely using vs code um so basically what i did is i have the maving extension installed because i did install the java extension pack by microsoft so i can just come here the create maving project and i do get this pop-up on the top which is usually where we have all the information where uh whenever we are using something or an extension in in vs code so i can select for example the quick start what's the maven version and then it's going to ask me to select uh where i want to create this project and then i'm gonna i'm gonna get a few questions here in the terminal to answer um for example the name of the project what is the package you know all those maven questions that we usually have to uh to answer before we create a project and then you can just come here and uh start creating your java classes now there are two ways for us to create uh some some classes as in any other ide we usually have two views the first one are all the files as we can see right here but we also have this one which is the java projects with this java project one what we can do we can see all the classes in this case this is not showing up right here but i'm going to show you a project where you have where we can see all the classes and all the properties that we have in this particular java java project another thing that we have also is amazing so i do use mavin personally it's what i prefer to use but if you do prefer grotto uh gretel you also have something similar for gretel as well so here in maving we can expand and see all the tasks that are available and uh let's say since this is is generating a warp uh war file i can just come here and if i hover this option i see the run button and then i can ask mavin to execute this particular task this is also good because you don't have to remember the comment that you have to enter in the terminal or in the command line so once that's done we get our war file so now how exactly can we execute this war file we need an application server remember that we're not using docker we're not using microservices here yet uh we're talking a little bit about some legacy projects or maybe new projects that that we're gonna create in a more old school way the good thing is we can just right click remember the right click in in vs code and we can run or debug if you have jetty installed it's gonna have it's gonna show you the options to run or debug on jetty and if you have tonkat as i do it's going to show you for the tonkat and of course we also have the support for tonkat all i have to do just click on this plus icon and open what is your tonkat home and visual studio code is going to do the rest for you so once you have here and this is something that i also like i think this is developer friendly because it's something easy it's a easy way for you to configure tonkat to run the way that you have to so again if you right click always remember though right click um you'll see the server configuration so you can edit the server.xml for tongkat you can also customize the gvm options in this case what i have here uh this particular project i'm using a few um a few uh functionalities they are uh not ready yes so so they are feature functionalities from java 14 and java 15 such as records and the pattern matching and the the template strings so i do have the enable preview flag here as well and uh if you want to you can also enable the helpful new pointer exceptions which were also introduced in java 14 if i'm not mistaken um so there you go you have all all your jvm options and again right click again you can start or debug when you start or when where when you debug of course you get all the logs here as well so you know you can just check the logs see if everything is okay and you know just do whatever you have to do and um you can also open in the browser so if you forget uh what port is tomcat running so usually localhost 8080 so you can also open this in the browser now uh so this is usually for uh maybe in projects and you get the gist it's all that you you can do now let's switch to spring because spring is is a framework that i i personally love and uh we had a talk before about a spring as well and you can leverage although everything that was mentioned also using visual studio code if you like so for um for spring what we usually do and i have here the the spring initializer we usually go to start dot spring dot io we enter all the information here do all our selections and we select all the dependencies let's say sprinkle dev tools and i have lombok here you can add the web and whatever other dependencies that you need you click on generate this is gonna um download a zip file what do we usually do is unzip the file and open this project in your favorite ide so in visual studio code we do have an extension for that which is the spring initializer if you go here to the java overview in case you close it you can always go back to the command palette if you don't know where it is it is in view command palette or you can use the shortcut which is ctrl shift p or command shift p on mac os and the java overview so you click on spring boot project you can choose between maven or gradle what's the the springboot version that you want the default package now i have this also customized to always use my custom package and the name of my project and again this is what i think that's really nice because i can see here also what is all the um all the dependencies that i need so i'm gonna use lombok i'm gonna use let's say um reactive uh spring data reactive mongodb i'm going to use is spring web reactive so i want to create a reactive spring project so i have all my dependencies here and i can press enter or i can click here and again select the project and i'm going to create my project now i have already a project created here i'm quickly going to switch it up and as you can see i have my test here and again this is a java project so it opened the java overview for me now one thing that i find really useful especially with java projects is we created the java the spring project and we selected a few of the dependencies but something that usually happens to me is i always forget something so what we usually do is we go to google and uh or to to to bing and we search what's dependency you can add that manually or uh what you can do and this is something that i really really love about visual studio code you can open up your palm.xml file um so just to give you a context this is a reactive spring boot spring web flux project and i'm using mongodb so now suppose i wanna deploy this project on azure so in this case i will need azure support for cosmos db and also of course to deploy this project on azure so i would need the uh the azure dependency here as well so what i can do right click always remember the right click again add starters and i love this option and here we go i have all the dependencies here again and what i can do i can select the azure so this is gonna get me a support to cosmos db select the dependencies i'm gonna proceed because i wanna um i want to refresh all my project it's gonna refresh the palm configuration and now it's gonna ask if i wanted to synchronize the java class path as well so yes please do synchronize and i'm gonna add this new dependency to my project and uh this covers the the the mapping file now we're done creating the project right so it's also important for us to run the project again what i wanted to show you before was the the uh the java dependency view what i can do here since i have all my uh packages here i can create a java class here and this is really nice let me see here demo controller and i have a class interface in them and even records because i can you know i can use the newest functionalities that are available in java that are instilling preview so i can create a record here as well class interface and so on and i do have the intellisense i do have the automatic imports uh automatic imports of the classes and everything else that i do need for running the project this is if you if you open any class that we have the public static void main uh you usually get this run and debug now this can be any kind of project doesn't need to be a spring project it can be just a sim simple java project so you do get the run and debug in case you're trying to open or run a big project whenever you open the project in vs code it might take a few seconds because what vs code is going it's going to do is just analyze all your source codes so we can actually show you what exactly what you can run but you do have the option right here and you can click on run and debug however if you do have the spring package extension installed you get this spring dashboard if you look here on the top um right now the jdconf logo is on top of it but if you look here let me see if i can no i cannot but um on behind the jdconf logo that we have here right now we do have the buttons but if you can access those buttons you also have the spring dashboard here so what you can do is uh just click on run and then it's gonna try to resolve the classes and compile and then there we go we have running the project and we're going to run the project the same thing that you can do is you can debug as well and debugging you have the exact exactly same environment as we get in other ides you know you can um you can watch variables you can have you have your call stack you have all the breaking points that you you want to track here and as you can see we have a project up and running and once we have this project up and running again you can go back to the browser and you can also open uh the your project in the browser you can stop and when it comes to tests what you can do you also have this icon right here for tests i have a few tasks that i have prepared for this presentation you can try to run all the tests and i'm gonna make sure that this is stopped you can try to run all the tests and again this is gonna take a few seconds to run all the tests and once the tests are executed you start to see the green uh bullets right here that the tests have passed so if you choose to open the test you're also gonna get the these check marks right here you get an x in case they are not uh they did not pass and once you open this you can actually get a nice report now the only thing that for me that's still missing is some kind of tool where i can see all the code coverage that i have in my project but maybe this is something that we can expect for the future since there is already a github issue for that um you might be you might be thinking if if you really want to adopt vs code as your main tool it's ready you can go ahead we have all the functionality that we need here but if if you're still not sure one thing that i still advise you is please give it a try and the reason because it we still have the github code spaces and in github code spaces which we're going to have a talk later today we can use all these extensions in github code spaces as well so take leverage of that so you can increase your productivity let me quickly go back here to my my slides this is all that i wanted to show to you thank you very much there are many more tips and tricks that are available so i'm gonna leave it up uh some links so maybe you can check them out uh and learn more about how we can use even more functionalities in in vs code and also the source code that i have presented and thank you very much much for your time there was a lot to unpack uh the java support on vs code only continues to grow we see key extensions if we only got the key extensions that bring java support to vs code the number of downloads are already above 20 million downloads and one thing that i like to go is i go to vs code and i select show me the popular extensions and the java extension is already on the top five so you know java developers are using vs code especially when they are web developers that have to deal with javascript front-end and css and html it's it's easier for them to just stick on the same editor and just plug those java extensions and write java code i i do have to ask do you actually use vs code uh eventually or is it something that you were still experimenting in learning and uh bringing that content to the developers that follow you i do i actually do depending on what i have to do on the day i actually open vs code for my uh we mostly use uh spring at work so i usually especially with with spring nice what is what is the uh regardless of java on vs code what is the one thing on vs code that you like most i think is the option to customize however i need because i also use for other technologies that i work with and i can customize re according to the workspace so whenever i'm working with java i have all my java extensions and when i switch it to angular which is the other technology that i work with i can you know customize to only use the angular extensions that i need and the java ones will be disabled so i think this is really cool
Info
Channel: Microsoft Developer
Views: 12,364
Rating: undefined out of 5
Keywords: Microsoft, Developer
Id: m3SQs3fb_0g
Channel Id: undefined
Length: 27min 10sec (1630 seconds)
Published: Mon Nov 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.