5 Setting Up VSCode and Java
In this lab you will walk through the set up of running a Java program in VSCode. You will need to be able to do this to implement HCCMs in Jaamsim. You don’t need to get this lab signed off, but you will need to have VSCode and the extensions installed to complete the next few labs.
If you do not already have VSCode installed on your machine, download and install the version appropriate for your operating system from here.
If you are using your own laptop, it is best to install a recent version of the Java JDK (unless you are confident you already have a recent version). We recommend Amazon Corretto 25. You can download it from here, and then install it. If you are using a lab computer we will use a version of Java that is already installed, which is Amazon Corretto 25.
To see short videos of the steps described below please visit this site.
First we will set up the folder structure that you will use to contain the files, not just for this lab but all the ENGSCI 355 labs. Create a new folder called ENGSCI355, if you are using a lab computer create this folder in the Documents folder on your University OneDrive. By keeping all of the 355 files on your OneDrive you should be able to access them on any University computer. If you are using your own computer, then create it within Documents or wherever you usually keep University related work. Inside the ENGSCI355 folder create another folder called Java_Example.
Once you have created the folders start VSCode. The first thing we will do in VSCode is create a profile for 355. A VSCode profile allows you to save settings and transfer them when you use VSCode on a different computer. This is useful if you use a different lab computer, or use a lab computer then your personal computer. We will use it mainly to save the extensions we need to run Java programs.
To create a profile click on the Settings icon in the bottom left corner of the screen, then select Profiles. In the window that opens click on the New Profile button. Enter ENGSCI355 for the profile name, then scroll down and click Create. Once the profile has been created we need to activate it by clicking on the tick next to its name. With the profile active we need to install the extensions that we need for Java. On the left-hand side click on the Extensions tab. Search for the ‘Extension Pack for Java’ and install it. Once it is installed, go back to the Profiles page by clicking on the name of the tab or by clicking on the Settings icon again and selecting Profiles. Then click on the three dots to the right of the ENGSCI355 profile, an select Export from the pop-up menu. From the export options provided select “Local file”. Then browse to the ENGSCI355 folder on your OneDrive you create earlier and click Save. You should now have a ENGSCI355.code-profile file saved on your OneDrive that includes the extensions needed to run Java programs (like Jaamsim). This means that if you work on a different computer in the future you can load this profile by clicking on the Settings icon, selecting Profiles, clicking the arrow next to the New Profile button, selecting Import Profile, and browsing to this ENGSCI355.code-profile file.
Then in VSCode open the folder you created earlier by going File -> Open Folder, then navigating to the Java_Example folder.
Once you have opened the folder in VSCode, create a new file in it called Hello.java. Once you have that file (or any .java file) open VSCode should detect that you are editing a java file and, if there isn’t one already, create a Java Project in the same folder. You should see that the ‘Java Projects’ section has been enabled in the bottom left of the screen.
We now want to make sure that this Java Project is using the correct Java JDK. Hover your mouse over the ‘Java Projects’ title and then click on the three dots that appear on the right hand side (with the tooltip ‘More Actions’), and select ‘Configure Java Runtime’. Use the drop-down menu that appears to select the Amazon Corretto 25 JDK. If it is not on the list, select ‘Find a local JDK’ and browse to the location that you installed the Amazon Corretto JDK.
Now go back to the Hello.java file and add the following code:
To run the program either click on the ‘Run’ button just above the line that declares the main function, or click on the run button in the top right corner of the screen. You should see a line with ‘Hello!’ printed by itself.
You have now created and run a Java program! If you run into any issues, there are more detailed instructions available here.