How to Set Up Java Development in Visual Studio Code on Mac | VSCode Java Development Basics (MacOS)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can set up the Java development environment with your Visual Studio code editor on your Mac operating system so let's get started and let's see how we can do it so you will need two things in order to set up the Java development environment with your Visual Studio code the first one is the obvious one you need to have Java jdk installed on your Mac operating system so let's see how we can install Java jdk so for that open your favorite browser and search for download Oracle Java on Mac and the first link which will appear here will be from oracle.com SL jaava Technologies for/ download I'm going to click on this link maybe I will also put this link in the description of this video so you can directly come to this website now at the time of making this video you can see Java 21 and Java 17 are available now what we are going to do is we are going to download the version Java 21 now when you scroll down a little you will see three sections for Linux Mac OS and windows we are going to click on the Mac version and then you will see uh different variants which you can download we are interested in downloading the DMG installer but here there are two variants of DMG installer also so if you are using M1 or M2 Mach then you need to select this one which is arm 64 DMG installer if you are using Intel backed Mac operating system then you need to choose the last uh one which is x64 DMG installer I am having having the M2 Chip on my Mac operating system so I'm going to choose the second option which is arm 64 DMG installer so I'm going to click on this link which is for the download of this DMG file you can see the download of this G DMG file has been started so it's not going to take much time to download just wait for the download to complete and once the download is complete you can see this DMG file is download Lo it so I can directly click on this DMG file here and let me minimize the browser and you will see this kind of window here in order to install Java jdk using this window you just need to click on this PKG file so I'm going to click on this PKG file which is going to start the installation process let me minimize this window also so this is the installer window we are going to just click on continue on the first window here and then it says this will take around 336 megabyte of space on your computer I'm okay with it so I'm going to click on install here and then I need to give my macbook's password so I'm going to provide the password here and then click on install software which is going to start the installation of Java jdk on my Mac operating system and in few seconds this window will appear which says install succeed so now I can close this window so basic installation of java has been done when I press on close it says do you want to move jdk installer to bin yes because the Java installation is finished we can move this uh DMG file to the bin so I'm going to just click on that and that file will be moved to the bin now Java jdk is installed on my Mac operating system now the next step in this process is to set the Java home so for that what we need to do we need to first of all find out where Java is installed on our Mac operating system so for that we are going to open the terminal so just click on this Spotlight icon and then search for terminal and then open the terminal from here so generally once you install Java jdk its location is for slash USR slash libexec for slash Java home so Java uncore home and then you need to give hyphen and then capital V and it's going to give you the version of java which you have installed so this is the version which we have installed which is Java 20 1.0.1 once we know the version of uh Java so now in The Next Step what we need to do is we need need to set or export the path of java home in our bash RC file or zshrc file now if you are using the newer version of Mac operating system most probably by default your Mac terminal is using zsh so we are going to set the Java home in the zshrc file now this zshrc file is located in your home folder so when you do PWD and when you open the terminal for the first time this is the home folder of your Mech operating system or for your particular user right and this dot zshrc is located in this directory so how to uh know that you can do LS space hyphen Al and then press enter and you will be able to see this kind of file which says do zshrc now if this file is not present you don't need to worry about it we will create a new zshrc file so if this file is present we will add our location to it if it's not present we will first create this file and then we are going to add our export now you can use any Editor to uh edit or create this file if this zshrc file is not there you can uh use this touch command and then write Dot zshrc and then press enter and it's going to create your dot zshrc file in my case it's already present so I'm not going to give this command the next command is using a text editor you need to open this file you can use Nano to open the dot zsh file or if you are the fan of text editor then you can use open also so just write open and then write do zshrc which is the name of this file and then press enter which is going to open this zsh file using the text editor let me make this uh text little bit bigger so you can see here so I already have one export here in your case if you have created this file for the first time you will not see any export here it will be blank what you need to do here is you need to copy and paste these two lines here I'm going to paste these two lines in the description of this video also so you can directly copy them and paste it into your terminal so the first command is export Java uncore home is equal to dollar and then this is the path which we have uh seen in in the terminal so we have used this path right user SL lib exact SL Java home space hyphen version and we got this version in my case right 21.0 one so here we have given the same path hyphen version and instead of this version you need to give the version which you see after giving this command so in my case the version is 21.0 point1 so I'm going to replace this version with 2.0.1 so let me just write 2.0.1 here and then in the next line you need to uh give this command which is export space path is equal to Dollar Java home/ bin colon dollar path right so these two lines you need to copy and paste and don't forget to change the version with the version which you have once you are done you can save this file so I'm going to just uh click on the file option and then click on Save which is going to save this shrc file and then I'm going to close this once this is closed you can verify that your changes are saved by just giving the same command once again is op space do zshrc and I can see that my changes are already there so I know that my changes are saved right once your changes are saved the next command you need to write here is this one so let me clear the terminal because uh it's full so after uh saving your export you just need to give this command command which is source and then give the name of your file which is zshrc and then press enter if you don't give this command then do zshrc will not load in the current terminal so if you don't give this command just close your terminal and then reopen the terminal once again okay so you can also do this so I'm going to just close this terminal and then uh quit from this terminal and then once again open the terminal and once the terminal is open I can check the Java home is set or Not by giving Echo command so I'm going to just give echo dollar Java uncore home and then press enter and it's going to give you the location of java home in my case it's uh Library SL Java SL Java virtual machine SL jdk 21. jdk contents slome okay so if you have followed all the step which I have shown you you will see this kind of output now once uh Java home is set and Java is installed we can also check the version of java so you can give this command which is Java space hyphen version and then press enter and it's going to give you the version of java which is 21.0 point1 in my case now once you have Java jtk installed I'm hoping that you already have installed Visual Studio code editor on your Mac operating system so once you have Java and visual studio code let's open the visual studio code editor and let's develop some Java code so what I'm going to do is I'm going to open the terminal first of all so let me open the terminal and using this terminal first of of all I want to create a directory where my Java programs will be saved and then we will open the same directory using the command line and then we are going to open the visual studio code so let's say I want to create this uh folder in my documents directory so I'm going to CD into the documents directory and here I'm going to create a directory to create a directory you can give this command mkd and then the name of the directory I'm going to name my directory as sample and then I'm going to CD into the sample directory here in the sample directory I want to open the visual studio code so I can type this command which is code space Dot and then press enter and it's going to open the sample directory in your Visual Studio code Now to create a new file you can uh press this button which is for creating the new file or you can click on file and then then click on new file so let's create a new file and I'm going to name this file as hello. Java and then press enter and as soon as I create this file Visual Studio code recognizes that I have created the Java file so it's going to suggest me to install the extension pack for Java so you can directly click on this uh install button or if you want to install the extension pack using the extensions section you can click on this section which says extension and then search for Java here and it's going to search for the extension pack for Java so the first uh result you will see here will be for extension pack for Java and it's from microsoft.com and it has been downloaded around 23 million times so it's an official package which you can install which will help you to develop Java on your Visual Studio code so I'm going to click on install here this package installs several different packages for example it's going to install the debugger for Java test runner for Java project management for Java Maven for Java so these are all the packages which are going to be installed when you click on the install extension pack for Java now here I'm going to just close these uh notification windows and then I will go to the Explorer once again I can close this uh also and then welcome window also I can close and once I have installed the extension pack for Java let's create some uh Java class and then we are going to run it so in your Java file when you type class and then when you click on the first option here so at every point when you type something Visual Studio code is going to show you the intelligence right so when I type class and then press on the first option it's going to create a hello class for me automatically right now inside this hello class I want to create the main method so if I type main here once again Visual Studio code is going to recognize that I want to create a main method so I'm going to just uh click on the first option here and it's going to create the main method for me now let's say I want to print hello world I can just type system.out do print line and then inside the parenthesis I can just type hello space World here okay so once I type hello world here let's save our program to save your code just press command s on your Mac operating system or you can just uh go here and then click on Save once your program is saved let's see how we can run it so the obvious option you will see here to run your program is this option which is for running your Java program you will also see uh these kind of uh highlighted run and debug options on top of your main method you can click one of them and it's going to run your program also or you can click on run section here at the top and you can start the debugging of your program or you can run your program without the debugging we are going to choose this option which is for run button so I'm going to click on this run button which is going to compile my program first and it's going to run this program and I can see the hello world is printed here so this is how you can run your program now if you want to debug your program you can uh just click on this run section and then click on start debugging but before debugging we need to set some break points so that our program execution stops at those break points so let's uh set the break point at line number six and then I'm going to press on run and then start debugging and my debugger will start and then you can see my program execution stops at the break point you will also see some buttons at the top so these are the few buttons you can use use to move into your uh code using continue button step over button step into button and then you can restart your debugging and you can stop your debugging now to explain the debugging in more details let me just uh add some more variables here so I'm going to write int a is equal to let's say five and then I'm going to once again declare our second variable which is B is equal to 10 for example right and then I want to print the sum of these two uh variables so I'm going to once again declare a variable called sum and then I'm going to just add these two numbers so A + B now in the next line I'm going to print the value of sum in the next line right so now uh the program is really simple you know what it's doing it's just adding two numbers and printing it right now in order to start the debugging let's set the break point at the line number six 7 8 and 10 okay now let's start the debugging so I'm going to uh just click on this debug option which is going to start the debugging and then you can see the program execution stops at my first breakpoint which is at line number six now I want to step over to to the next breakpoint so I can click on the step over button when I click on the step over button you can see this variable a is assigned the value of five and our program execution stops at B at line number seven so so assignment of 10 to B variable is not yet finished so when we step over once again you can see B is assigned the value of 10 so this is how your program execution stops at different points now I can also click on the continue button which is going to stop at the next break point so when I click on continue you can see the program execution stops at the next break point and now the value of sum is displayed here you can also see the intelligence values here so a is equal to 5 b is equal to 10 sum is equal to 15 a is equal to 5 b is equal to 10 so at every point you will also see this highlighted uh indication of each and every variables here once again I will just click on continue which is going to finish my uh execution right so when you debug your program you will be able to see the value of variables under the debug section this is the debug section now if you want to create a launch. Json file for your programs you can go to the debug section and then click on create a launch. Json file so when you click on the launch. Json file it's going to create this launch. Json file in your vs code folder so it's going to create a new folder called vscode and then under vs code you will see the launch. Json file where your launch configuration will be present this kind of launch configuration is useful when you are using some kind of Version Control with your code code so that when you commit and post your code to let's say GitHub the other developer can uh clone or pull that code and he will be able to have the same kind of configuration so once again when I go to the debug section now from here also I can click on this start debugging uh button and my debugging will start here again uh I can restart the debugging using this option and I can also stop the debugging I also have the hot code replace option so when I click on hot code replace it says cannot find any changes class for hot code replace so when you have some kind of changes it's going to help you now let me uh stop this debugging here you can also find the problems so let's say I just forgot this uh semicolon at line number 10 so let me save this program and then I'm going to run my program and it's going to show me the problem right so you can see under problem section it says that at line number 10 column 72 I have forgotten the semicolon so when you have some kind of syntax error in your code Visual Studio code is also going to show those problems and with the help of those hints you can solve your build errors also when you set the break point for debugging you will be able to see the list of all those break points here under the breakpoints uh section you can disable those break points from here so just uncheck these check boxes if you want to remove the break points you can just click on the cross icon here you can also toggle the active breakpoints so all the break points will be deactivated at once or you can uh just uh close these break points one by one you can also check these check boxes which are going to show you the uncaught exception and caught exceptions while you are debugging your code so this is also useful for that purpose so I think that's it for this video this is how you can set up the Java development environment with your Visual Studio code on your Mac operating system I hope you've enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 20,303
Rating: undefined out of 5
Keywords: Java Development, Visual Studio Code, vsCode, Java, Set Up Java, Visual Studio, Java (Programming Language), Java (Software), Ubuntu (Operating System), OS X, Mac, Mac OS X, How do I run a Java program in VS Code on a Mac?, How to setup JavaFX in Visual Studio Code? How to compile Java code in VS Code?, visual studio code java, visual studio code java project, java in visual studio community, java jdk, visual studio code java pack installer, visual studio 2024 java
Id: qTaZO_Ou7Zo
Channel Id: undefined
Length: 23min 27sec (1407 seconds)
Published: Sat Jan 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.