Run Java source files with JBang

Run an independent calculator offline, pass and validate arguments, then compare three source-to-program workflows.

This is a fresh Java VM session. All starting files are supplied here; no notebook or earlier lesson is required. Work through the experiments, predict each result, and inspect what actually changed. Resetting or leaving discards guest files.

From notebook calculation to command-line program

The notebook made it easy to change a value and rerun a cell. A command-line program receives values as arguments. This lab supplies a complete Calculator.java so it is independent of your earlier notebook work. Read it before running it; the calculation uses the same small cost-per-hiker idea.

JBang is a separate tool, not the JVM or a language extension. It arranges compilation and execution with a JDK and caches build artifacts. The image includes JBang 0.135.1 and Zulu 27. Every command in this lesson runs locally with --offline; no dependency or JDK download is required.

Inspect the launcher

//JAVA 27 public class Calculator { public static void main(String[] args) { // validate arguments, calculate, and print } }

//JAVA 27 is a Java comment to javac and a directive to JBang. It requests the installed Java feature line. The curriculum examples use a Java 25 baseline, but this guest contains Java 27. Requesting 25 here could require another JDK, so this reproducible offline lesson names 27 explicitly. This program's syntax does not require a Java 27 language feature.

Arguments are text first

jbang --offline -R=-Xint Calculator.java 6 7 passes two strings to main. args[0] is "6" and args[1] is "7". The filename and jbang options are not entries in args. The launcher checks the argument count before indexing, converts each string with Integer.parseInt, and rejects negative values.

The calculation casts hikers to long before multiplication. That means the multiplication uses long arithmetic; casting an already overflowing int result afterward would be too late. The main output for 6 hikers at price 7 is Trip total: 42.

Read each validation branch. A useful error message tells the organiser how to fix the input. A nonzero exit code tells a shell or script that the program failed. Missing arguments produce usage, malformed text produces a number message, and negative values produce the nonnegative rule. None should print a misleading total.

Small changes should change behavior

Try 9 hikers, then zero hikers. The result should follow the inputs, not a fixed example. Next replace the output label with Booking total: and rerun the source. JBang notices changed source and builds the runnable form it needs. You do not manually launch an old .class file in this workflow.

This convenience has a cost: compilation can still occur and first runs can take time in the browser VM. Do not infer production Java performance from the emulator. Wait for the prompt before sending another command.

Three workflows, one calculation

| Workflow | Commands | What you control | | --- | --- | --- | | Explicit compilation and class launch | javac Calculator.java; java Calculator 6 7 | A visible class file and a separate launch | | Java source-file launcher | java -Xint Calculator.java 6 7 | One command compiles source for execution | | JBang | jbang --offline -R=-Xint Calculator.java 6 7 | Tool-managed compilation, cache, and directives |

The guest uses an interpreted Java 27 build, so these tools run without interpreter flags. This lab sets JBANG_JAVA_OPTIONS=-Xint for JBang itself. The -R=-Xint option keeps its launched program in interpreted mode. Wait for the shell prompt between commands.

The source launcher is a feature of the java tool. JBang adds its own workflow around a JDK. Neither changes the meaning of Integer.parseInt or the multiplication expression. Compile the same source explicitly and compare outputs so the distinction stays about the tools, not a different algorithm.

Transfer without earlier state

Copy the supplied source into a clean handover/ directory and run it there with 4 and 8. The fallback file already contains the conventional main method that a notebook expression alone would lack. If you export your own notebook work in the future, put its calculation inside a complete launcher and supply argument validation. You do not need an export from another lesson to pass this one.

No Maven project or external library is involved here. Offline mode fails when required dependencies are unavailable; it does not magically provide missing files. The prepared JBang and JDK assets make these specific commands reproducible.

References: JBang quick start and offline mode, JBang CLI, and Java launcher.

JBang directive

A comment interpreted by JBang to configure its execution workflow.

Exit status

Zero means success; this launcher uses 2 for rejected arguments.

Source launch

java can compile and launch a source file in one command without a manually managed class-launch step.

Inspect the installed tools and complete source

Read jbang version, java --version, and Calculator.java. Identify //JAVA 27, main, args, input checks, and the calculation. Offline version inspection should require no download.

jbang --offline version
java --version
cat Calculator.java

JBang 0.135.1; Java 27

Run the source offline with two arguments

Predict 6 × 7, then run Calculator.java through JBang. The two arguments arrive as strings and are parsed before multiplication. No manual javac command is needed for this step.

jbang --offline -R=-Xint Calculator.java 6 7

Trip total: 42

Change inputs and cover zero

Run 9 hikers at price 7 and zero hikers at price 7. Explain why zero is accepted and returns zero, while a negative count is rejected later.

jbang --offline -R=-Xint Calculator.java 9 7
jbang --offline -R=-Xint Calculator.java 0 7

63, then 0

Observe useful messages for bad input

Run without arguments, with nonnumeric hikers, and with a negative count. These failures are intentional. Immediately print $? after each run: code 2 signals rejected input. Do not run a different command before capturing that status.

jbang --offline -R=-Xint Calculator.java; printf "exit=%s\n" "$?"
jbang --offline -R=-Xint Calculator.java many 7; printf "exit=%s\n" "$?"
jbang --offline -R=-Xint Calculator.java -1 7; printf "exit=%s\n" "$?"

Usage, numeric, and negative-value messages; exit=2 each time

Edit source and rerun through JBang

Replace the output label Trip total with Booking total, then rerun 6 and 7. JBang rebuilds what it needs from the changed source. Confirm the new text and unchanged calculation.

sed -i 's/Trip total:/Booking total:/' Calculator.java
jbang --offline -R=-Xint Calculator.java 6 7

Booking total: 42

Compare with explicit javac and java

Compile the same edited source using javac, inspect Calculator.class, and launch the class with the same arguments. This workflow produces an explicit class file and separates compilation from execution.

javac Calculator.java
ls -l Calculator.class
java Calculator 6 7

Booking total: 42

Compare Java source-file launch

Run java -Xint Calculator.java 6 7. The .java argument selects source-file launch, whereas java Calculator selects the compiled class. The JBang directive is a comment to the Java tools. Explain the identical result despite different launch workflows.

java -Xint Calculator.java 6 7

Booking total: 42

Run the fallback source in a clean handover directory

Create handover, copy only Calculator.java into it, and run through JBang with 4 and 8. The program should not need a copied class file, notebook state, or previous working directory.

mkdir -p handover
cp Calculator.java handover/Calculator.java
cd handover
jbang --offline -R=-Xint Calculator.java 4 8

Booking total: 32 from source in handover

You completed the tooling workflow using real commands and files. Explain each result before moving on; tool output is evidence, not a substitute for the Java rule behind it.

Enable JavaScript to run the live terminal and track your progress.