How to run Java
Every Java page in this course says in IDEA, a new file next to Check.java. This is what that means, written once.
In the lab: IntelliJ IDEA and the course project
The course’s files are a repository, https://github.com/cu-cs-labs/cmsc-120-stripe: Stripe.java, Check.java and Version.java, under src/. Once, on a machine where you have not done it yet:
- Open IntelliJ IDEA. It opens the last project you had open: the menu button (☰, top left) → File → New → Project from Version Control….
- Stay on the Repository URL tab and paste the address above. Leave the folder IDEA suggests,
IdeaProjectsinside your own user folder. Press Clone. - When IDEA asks, trust the project and take the JDK it offers.
- Open
src/Stripe.javaand press the green Run arrow besidemain.
Done when the output says Looks shipshape, all 2 checks passed.
No Run arrow beside main? Right-click src, Mark Directory as, Sources Root.
Git is only how the files reach you. Nothing to commit, and no account.
The project is on the machine you sat at and no other: Getting your work off the lab machine says what follows from that.
A new file
Right-click src, New, Java Class, and type the name the page gives — Decisions, say — without .java; IDEA adds it. Delete what IDEA puts in the file, down to nothing, and write it the way the pages show: functions at the top, and a main that ends in Check.done().
// twice the number it is given
int twice(int n) {
return n * 2;
}
void main() {
Check.is(twice(3), 6);
Check.done();
}Run it with the green arrow beside main. Check.java is already in src, so Check.is works in every file there.
Anywhere else: OneCompiler
OneCompiler runs the same Java in a browser, with nothing to install: https://onecompiler.com/java/4544z33u6 holds Stripe.java beside Check.java. Press Run. For a new program, empty Stripe.java and write yours in its place; the file keeps its name there, and that is fine. To hand the program in, put its text in a file with the name the page asks for, on your own computer.
One main per program on OneCompiler: it will not run two files that each have one.
Your own computer
The course’s Java needs Java 25 or newer: the short form, void main() with no class around it, is new in 25, and the Java an IDE already has may be older. Version.java in the project prints which one runs. If IDEA says cannot find symbol: variable IO, or objects to void main(), the Java is older than 25: File → Project Structure → Project, set the SDK to 25 — IDEA offers to download it there — and the language level to match.
The long form
Most Java found online looks different: public class Main around everything, public static void main(String[] args), System.out.println. It is the same language in its long form, it runs here too, and Class 8 showed the two side by side.