Compiling and Running Applications
What this page is for
Writing Java is only half the job. Before your code can run, it first has to be compiled (turned into files the computer can actually execute) and then run. This page gives you the exact commands you will use to compile and run your work all semester. Bookmark it, because you will come back to it often in the first few weeks.
First, Two Things to Know
1. You will use a tool called "ant"
All compiling and building in this course is done with a tool called ant. You have probably never used it before, and that is completely expected. Ant is a build tool: when you type a short command like ant build_labs, it quietly does all of the tedious compiling steps for you so that you can focus on writing Java.
You do not need to learn ant right now
For everything on this page, all you need to do is run the commands exactly as they are shown. We explain what ant is actually doing, and why, on the Beginning to Use Ant page in Week 2. If these commands feel like magic for now, that is fine.
2. "The Analyzer" is your project for Units 1, 2, and 3
You will see the word Analyzer on this page and throughout the first three units, so let's clear up what it means right away.
What is the Analyzer?
The Analyzer is the name of the main project you build across Units 1, 2, and 3. It is a single application that you keep growing and adding to over those three units. Any time you see a command with analyzer in it (like ant build_analyzer or ./runAnalyzer.sh), it is referring to that project. Your labs are separate, smaller practice exercises, so they use their own commands.
Your "projects" Directory
Almost every command on this page is run from the same place, so let's name it once.
- Open Codespaces on your repository in GitHub.
- This is part of Lab 1, so you can start Lab 1 now to get your Codespace environment setup.
- Make sure you are at the root of your repo, which is the folder named
adv-java-projects-githubid.
From here on, this page calls that folder your "projects" directory. Whenever a step says "run this from your projects directory," this is the folder it means.
Why keep .class files separate from .java files?
In this course, your compiled .class files are kept in a different directory from your .java source files, for every lab and project. You could compile a file and then move the .class file by hand each time, but that would be tedious and easy to get wrong. Ant handles this separation for you automatically, which is one of the reasons we use it.
Compiling and Building
Two words show up a lot here: compile and build. They are related but not the same.
- Compile checks your code and turns your
.javafiles into.classfiles. Use it for a quick check that your code compiles. - Build compiles your code and then packages it into a runnable
.jarfile.
When in doubt, use the build command
A build always compiles first, so the build_ commands give you a ready-to-run result. Reach for the compile_ commands mainly when you just want a fast check that things still compile.
Compiling Your Labs
Run these from your projects directory:
| Command | What it does |
|---|---|
ant compile_labs |
Compiles all of your labs. |
ant build_labs |
Compiles your labs and builds the .jar file. Use this most of the time. |
Compiling the Analyzer
Remember, the Analyzer is your project for Units 1 through 3. Run these from your projects directory:
| Command | What it does |
|---|---|
ant compile_analyzer |
Compiles the Analyzer application only (no jar). |
ant build_analyzer |
Compiles and builds analyzer.jar. Use this most of the time. |
Running Your Applications
We use small run scripts to launch applications this semester, and you always run them from the root of your projects directory.
Each run script is set up once. You download it into your projects directory, then make it runnable with chmod +x (a one-time command that gives the script permission to run). After that, you just call the script whenever you want to run the app.
Running the Analyzer
- Download the run script and save it to your computer. Right-click the link and choose "Save Link As...": runAnalyzer.sh.
- Drag and drop this script to your projects directory in Codespaces.
-
Make it runnable (you only need to do this once):
chmod +x runAnalyzer.sh -
Run the Analyzer, giving it the path to the file you want it to analyze:
./runAnalyzer.sh <file path>
Running Unit 1 Labs
- Download the run script and save it to your computer. Right-click the link and choose "Save Link As...": runLabs1.sh.
- Drag and drop this script to your projects directory in Codespaces.
-
Make it runnable (you only need to do this once):
chmod +x runLabs1.sh -
Run the Unit 1 labs:
./runLabs1.sh
Running Demo Code
As you follow along with the video demos, it helps to create your own directory and a run script for any demo code you write. You can find the demo code and its related script in GitHub.
Quick recap
- Compile and build with ant, run with the run scripts, always from your projects directory.
- Use the build commands most of the time:
ant build_labsandant build_analyzer. - The Analyzer is the one project you build across Units 1, 2, and 3.
- New to ant? That is expected. The full explanation is coming up in Beginning to Use Ant.