This guide will help you set up Eclipse (or IntelliJ) and Maven to work on your personal computer. This is by no means a comprehensive guide, but hopefully it will set you on the right track. Feel free to email firstname.lastname@example.org with any questions about this guide.
Always make sure to run your code on a department machine before turning it in. If we can't run your code, then you will receive deductions. Refer to the end of this guide for an SSH tip that may be useful when remotely testing your code on a department machine.
Installing the Java JDK
In CS 32 we're going to be using the latest version of Java, Java 8 (also referred to as Java 1.8). Throughout the course you'll learn a lot about the new features introduced in this new version and how you can use them to your advantage. To code in Java 8 you'll need to download the JDK (Java Development Kit) here for whatever computer you're working on. Then, unzip the file and install.
Setting up Maven and JAVA_HOME
Next we're going to set up Maven and your JAVA_HOME environment variable. Maven is a build automation tool that we're using to manage libraries and build your projects. Check out the Boggle assignment for more information about Maven. The JAVA_HOME variable is an environment variable that you can set on your computer to tell it where the Java compiler exists in the file system.
We will also being using bash scripts to run our programs from the command line. Since Windows doesn't natively run shell scripts, you'll need to download a tool that does. One option is Git Bash, an emulator that acts like a Linux command prompt. It comes with the normal installation of git, which you'll need for this course later anyway. You can download Git here. Another option is cygwin. Both are acceptable to use throughout this course. Once one of these is installed, you should be able to open it up and use it as a normal terminal.
Mac OS X
To set your JAVA_HOME variable, open up your ~/.bash_profile file in a text
editor and add this line to the end of the file:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
The JDK installs to /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/ by default
on Mac, and we want to set JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home. However, this would fail if you were to update
your version of Java to say jdk1.8.0_26.jdk. Luckily the command,
/usr/libexec/java_home -v 1.8
returns the correct JAVA_HOME for any Java version 1.8. Wrapping it in
$( then saves the result of the command as a string
which we can set JAVA_HOME to.
Once you've done this, run the following to make sure the variable is set properly.
$ source ~/.profile $ java -version java version "1.8.0_25" Java(TM) SE Runtime Environment (build 1.8.0_25-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
If the version is 1.8 then you've set the variable properly.
We strongly recommend using homebrew to install Maven. Homebrew is a package manager for OS X that makes installing packages as easy as a simple terminal command. To get homebrew run the following command in your terminal:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Great! Now you have homebrew! That was simple! Now, you can use homebrew
to install Maven and a ton of other packages. To read more about it, check
out their website here. If you just want to install
Maven run the following command from your terminal:
$ brew install maven
And now you should have Maven installed! To double check, run
mvn -version and you should get something like this:
$ mvn -version Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T11:41:47-05:00) Maven home: /usr/local/Cellar/maven/3.3.9/libexec Java version: 1.8.0_121, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "10.12.2", arch: "x86_64", family: "mac"
Debian & Ubuntu
If you've previously have Maven 2 installed, then you may need to run
$ sudo apt-get remove maven2
Then for either distros you can run,
$ sudo apt-get install maven
to get Maven 3, and you can double check that its the right version the same
way as we mentioned above for Mac.
If Maven complains about JAVA_HOME, then you can set JAVA_HOME in your
although there's a good chance that this may not be needed. Your JDK will
be installed in /usr/lib/jvm/.
Setting up Eclipse
To set up Eclipse, download Eclipse Neon here and install it on your computer. Neon is the latest version of Eclipse (4.6), but earlier versions like Luna (4.4) and Mars (4.5) should work just as well.
Now, open up Eclipse and set your workspace. First, you want to add Java 8 to Eclipse. Do this by going to
Eclipse → Window → Preferences → Java → Installed JREs → Add. Then set the directory as the directory that you installed the JDK to and name it Java SE 8.
To change Eclipse to run with Java 8, go to Eclipse→Preferences→Compiler and then set the compiler compliance level to 1.8. Now, your Eclipse is ready to go!
Our standards for code style are outlined in our style guide, and we use the Checkstyle plugin for Maven to detect errors (
mvn site). You can use Eclipse to auto-format your code upon saving: this will save you time fixing small style errors such as too-long lines or trailing whitespace. Note that the options below won’t fix all Checkstyle errors (more complicated fixes aren’t automated), so you should still run
mvn site before handing in your project.
- Install the Checkstyle plugin . You might
need to download the source code if installing from the marketplace doesn't work. Then in Eclipse, go to
Help → Install New Software → Add → Local, and look for the downloaded folder.
- Right click
Project → Properties → Checkstyle
Local Check Configurations → New → (name of your choice) → Import config/cs32_checks.xml
Main → Check Checkstyleactive for this project, Add the local check configuration you just created
- (This step may only be necessary if you're using Eclipse Luna.) Right click the project you have open in the sidebar, and click Checkstyle → Create Formatter-profile.
- Right click
Project → Properties → Java Code Style → Formatter. You should have a profile called something like “eclipse-cs …”
Java Editor → Save Actions → Format all lines
- Right click on
Project → Properties
Java Code Style → Formatter
- Create new profile, then Edit:
- Indentation → Spaces only, Indentation size = 2
- Line Wrapping → Maximum Line Width 80
Java Code Style → Clean Up
- Edit active profile, check Format source code, Remove trailing whitespace (all lines), Correct indentation, Organize Imports
Java Editor → Save Actions → Format all lines
- Repeat the steps for "Clean up" in
Java Editor → Save Actions → Additional Save Actions
Setting up Intellij
Intellij is an alternative IDE to java that you may prefer. To use it first head here to apply for a student JetBeans account. Then download Intellij Ultimate and follow instructions, including adding your student account. You can do a lot with Intellij. One of the first things might be modifying Java code style. You can do this at Preferences/Editor/Code Style/Java. Here are a few changes that will help you follow our check style:
- Tabs and Indents → Indent 2
- Wrapping and Braces → Right Margin (columns): 80
SSH & Port Forwarding
If you need to test your servers remotely on a department machine, you'll notice that your server will be hosted at localhost:4567 on the department machine, but you will have no way to access it on your own computer. To get the remote machine's port bound to your own machine's do the following when SSH-ing:
- ssh as you would normally and start up your project.
- Take note of what machine your server is running on (e.g. cslab4g, hertz, mslab-1, etc).
ssh <username>@ssh.cs.brown.edu -L 4567:<machine name>:4567
Now if you visit localhost:4567 on your local machine, you should be able to visit your page - which is running on the department machine, with no problem!
Alternatively you can also run,
ssh -f <username>@ssh.cs.brown.edu -L 4567:<machine name>:4567 -N
To execute the port binding in the background - if you don't want to have a
useless terminal laying around. If you do it this way be sure to kill this
process manually when you're done with it. Namely, you can track it down with:
ps aux | grep ssh
and kill it by its PID.