Chunk 1, Module 2: Loading External Classes in NetBeans
Procedure for transferring and debugging external Java code
As you begin learning Java, you'll often want to run other folks' code, and this short module provides step-by-step procedures for doing so.
Exercise 1: Accessing Course Resources on GitHub
Exercise type: Procedure Practice
GitHub.com is a widely used online repository for free and open source code which you can modify and use in your programs. This course, CIT-111, has a repository on GitHub where all of our course code is posted for your review and use. Later in the course, we'll learn how to download the entire repository (aka. "repo") to your hard drive for easy use, but for now, learning to do the transfer manually into NetBeans will serve you well.
- Locate the source code of a Java class you would like to run in NetBeans. You can start by browsing our course's GitHub Repository linked here.
- For this exercise, we'll load a Java class that simulates a quality control mechanism into NetBeans so you can tinker with the random number generator and develop skills for doing so with any other class you'd like. Start by opening up this source file page from GitHub.
- Our goal is to be able to run this code in NetBeans. To do so, we must create a Java file in Netbeans to serve as a container for the code we're going to pull from GitHub. Start by creating a new project in NetBeans called "transferPractice" and a package inside that project also called "transferPractice."
- Next, review the source code you'd like to transfer to NetBeans and locate its class name: it should be located at the top of the file after the keywords: public class. In our case, the name of the class is QualityControl.
- Our Java source code file in NetBeans must have exactly this same name (caps counts!) so that the compiler knows what class to run when you ask it to run the file. So inside the package "transferPractice", right click the package >> new >> Java Class. Name the class "QualityControl". NOTE that NetBeans will automatically append the ".java" ending to the file.
- At this point, your project setup should look exactly like this:
- Since the source file we are transferring already has a class defined, we must delete the class declaration created by NetBeans automatically each time we create a Java class. To do this, delete all the lines after the package declaration in the NetBeans file. In this case, our package declaration line read package transferPractice; and now should be the only line of code in the NetBeans file.
- We're now ready to transfer the source code from GitHub into NetBeans. The best way to do this is to click the "Raw" button in the upper right corner of the source code file. This will bring up a page that is only the source code text which we can easily copy and paste.
- With the text of the source code in your web browser, select all of it by holding control and tapping "a" for select all. Copy the text to the clip board (the name of the magical place that stuff sits when we copy it) with control + c. Then, in NetBeans, place your cursor under the package declaration line and paste in the text with control + v.
- You should now see that at the top of your NetBeans file, you have two lines that declare packages. Since a class cannot be in two packages at once, the pre-compiler will likely highlight the second package declaration in red, alerting you to an error:
- We can fix this error easily! Just delete the package declaration line that was moved over from GitHub because the file is not in whatever package it was created in by the author of the code. It's in our new package. You can delete lines in NetBeans with control + x (which is actually cutting the line and storing it in the clip board, but we're not going to use it anywhere else.)
- Once this step is done, save the file with control + s. The pre-compiler will do a scan of the file and look for potential errors. Since this particular class does not have errors, you should see a little green triangle appear in the icon next to the name of the class file in the tab at the top of the editor and in the package viewer. This means NetBeans thinks the file is ready to run.
- If there is no green triangle, there will be a red exclamation point in the class file icon AND a exclamation point next to the line with an error and the actual words that are causing the error will have red highlights or red squiggles. If this happens, hover your mouse over that error for a second and a tip will pop up. Read that tip. If you understand it, fix it.
- Compile and run this file by selecting the Run menu >> run file (or control + F6). If all is well, your output should be like what is printed in the image below. The way the file is written, the program will simulate the production and testing of 10,000 units! Look how fast Java can process so many individual evaluations! Super Cool! Tinker with the number of tests by finding the line of code that sets the max number of units to make.
- The last line of the output provides a summary of the entire production simulation. When I ran this, 64% of the units were below the quality threshold set in the code. Try changing the quality threshold value a few times and see how the random number generator creates tests against this value. It's pretty cool.
- Practice this skill by finding another class file in the CIT111 GitHub repository. See if you can get it to run and tinker with it some!
warningEvery character counts--including capitalization! The Java compiler looks at each letter of each word and each character. "qualityControl" is not the same to the compiler as "QualityControl". Learn to be very through in your typing.
There's a good chance that this early on, you won't understand what an error message means. You can use the folks on the internet to help you. The best way to get help is to type the exact text of the error from NetBeans into a search engine followed by the word "netbeans". For example, if i didn't know what the above error means, I can easily see that there are many helpful hints online. Take your time and read the responses carefully. This is a new language, and going slow is important.
warningA common error that arises is mismatched class names. Folks sometimes create a class file in NetBeans whose name is not EXACTLY the same as the class they are transferring. This will make your code uncompilable. Don't Panic! You can rename a file in NetBeans by right-clicking the .java file in the project pane and holding control and tapping "r" for rename. Then all should be well.