Java
In this class we will use Java (Standard Edition). Install this first. In particular, you must install the JDK (Java Development Kit) and not just the Java Runtime Environment (JRE is used in web browsers). If you already have the JDK installed, it doesn't hurt to reinstall, I recommend you do so.
NOTE: Oracle changed the licensing for Java as of January 2019. The instructions below are for the commercial version of Java supported by Oracle. You can use this version without cost for personal code development and test, but you may need to pay a license if you develop software for commercial, business, or production use. This blog gives a description of the licensing changes. You can install an open source version of Java by following these instructions rather the instructions shown next, but the open source version can cause issues with webcam access.
Java installation instructions:
- Download and install the official Oracle Java 16.0.2 JDK at the Oracle download site. NOTE: version 16 is not the latest version of Java but we will use your laptop's web camera and version 16 works well with it on Windows and Macs (even newer chipsets). Please ensure you install Java 16
- Click the link for your platform (Mac or Windows). You may have to create an account with Oracle to download
- Download the installer (DMG for Mac with either Arm (Apple's M1 / M2 /M3 chip) or x86 (Intel chip) depending on your processor or .exe for Windows). [screenshot]
- Run the installer in the usual way for your platform, following the instructions.
Official documentation and tutorials.
IntelliJ IDEA IDE
We will use the IntelliJ IDEA Integrated Development Environment (IDE) for developing our Java programs.
IntelliJ IDEA installation instructions:
- Install Java first, as described above
- Download the IntelliJ Community Edition IDE (scroll down a little to find the free community edition) from JetBrains. Note that there is also an Ultimate Edition that is free for students, but despite multiple emails to JetBrains, for some reason they block Dartmouth email addresses :-(. The free and open source Community Edition will suffice for CS10
- After downloading, follow the installation instructions
- Start IntelliJ and set up a Project to hold your CS10 code
- Start IntelliJ
- When a start up screen appears, click "Create new project", otherwise click File->New->Project
- Enter project Name as "cs10" [screenshot]
- Set project Location to "Documents/IdeaProjects" (or another directory you prefer)
- Keep the defaults (Language: Java, Build system: IntelliJ)
- Select JDK: 16.02
- Click Create
- Choose to open in "This Window"
- Set up folders to organize your CS10 code within the Project
- Click on the Project name (e.g., "cs10" or the Project name you chose above) in the left pane
- Click File->New->Directory and give the directory a name (e.g., "day1")
- Right click on the directory you just created (e.g., "day1") and select Mark directory as -> Sources root
- Do not skip the preceding step or your code may not run from IntelliJ
- Download code from course web page
- Go to the Schedule tab of the course web page and click on the class date to see the detail page for that class period
- All of the code for each day will be listed at the top of the detail page
- Right click on the code you want to download (e.g., "JavaVariables0.java" from the first day of class)
- Select “Save link as”
- Select folder where you want to save the code (e.g., "Documents/IdeaProjects/cs10/day1")
- Click “Save”
- The file will appear in the left pane under the folder you chose (e.g., "JavaVariables0.java" will be under "day1")
- Create your own Java classes
- Click on the folder where you’d like to store you code (e.g., "day1") in the left pane
- Click File->New->Java class
- Give your new class a name that begins with a capital letter (e.g., "HelloWorld") and click "Ok"
- IntelliJ will create a file called "<your class name>.java" in the directory you chose and will stub out the class for you (e.g., the file will contain "public class <your class name> {}"
- To add a "main" method (where execution begins)
- Click inside the curly braces ("{}") of your class in the right pane
- Type "main" and press enter
- IntelliJ will expand "main" to "public static void main(String[] args) {}" for you (or you could type this string yourself instead)
- To add a print statement
- Type "sout" and press enter
- IntelliJ will expand "sout" to “System.out.println();” for you (which will save you a lot of typing!)
- You can add what to print inside the parenthesis “System.out.println(“Hello World!”);”
- To run your class (assuming it has a "main" method)
- Right click on the code in the right pane
- Select "Run <your class name>.main()"
- The output will appear at the bottom of the IntelliJ window
- You may also want to set up IntelliJ to automatically add import statements
- In Settings/Perferences select Editor->General->Auto Import
- Check the "Add unambiguous imports on the fly" checkbox
JavaCV
The "video processing" lecture and a portion of Problem Set 1 use your computer's built-in camera. We will use JavaCV, a Java wrapper around a computer vision tool kit called OpenCV, to access your machine's camera.
JavaCV installation instructions:
- Download the opencv zip archive: opencv.zip
- Expand zip file (I put mine under Documents/IdeaProjects/cs10/libs) from Windows File Explorer or Mac Finder
- From IntelliJ click File->Project Structure->SDKs (on left under Platform Settings)->Classpath (tab at top on right) [screenshot] (note Windows screen looks slight different)
- Click ‘+’ near the list of files
- Add the following JAR files (you may find it easiest to click the "+" once for each file) from the directory where you expanded opencv.zip (e.g., Documents/IdeaProjects/cs10/libs)
- "opencv.jar", javacv.jar", "openblas-[sys].jar", and "opencv-[sys].jar" where [sys] is windows-x86_64 or macosx-x86_64 depending on your system
- (optional, used later in the class) "json-simple-1.1.1.jar", and "net-datastructures-4-0.jar"
- To test the webcam, save the file WebcamTest.java (see item #6 above for how to save from course web page).
- If there are red underlines, there's an installation problem, most likely the JAR files aren't correctly added
Note: on some systems you may be able to clear errors by selecting File->Invalidate Caches/Restart, then selecting clear caches and restart - When you run the code (next step) and you are asked to allow IntelliJ IDEA to have access to your camera -- ALLOW ACCESS. Otherwise when you run your code it will exit with code 134 and you'll likely have to reinstall IntelliJ IDEA
- Right click on the code and select "Run WebcamTest.main()" to run it, and cross your fingers. You should see a live feed from your computer's camera after a short while. If not, and your issue has not previously been reported, please post on Slack.
- Note to Windows 10 users: in Settings — Privacy — Camera, make sure "Let apps use my camera" is switched to "On".
If you are having trouble getting WebcamTest.java to run, don't worry, we will get it sorted out.