# Java

> JavaBuild compiles Java with javac, tests it with JUnit and builds a jar; naming a main class makes it a program.

**JavaBuild compiles your Java with the JDK your group pins, tests it with JUnit and builds a jar. Naming a main class makes it a program.**

Build system: `JavaBuild`
Start one: `plinto create java-service HelloApp` (or `java-lib`)
Ships as: an archive with its own JDK

## The project {#project}

```text
HelloApp/
  plinto.conf
  src/             your code: src/helloapp/Main.java
  resources/       files that go into the jar as they are
  tst/             tests
  tst-resources/   files the tests read
  configuration/   JavaBuild.config, the build's settings (optional)
```

```conf file=plinto.conf
{
  name = HelloApp
  version = 1.0
  build = JavaBuild
  main = helloapp.Main         # what `plinto run` starts

  build-dependencies: {
    JavaBuild = 1.0
    Java = 25
  }
  dependencies: {
    Slf4jApi = 2.0.17           # imported from Maven Central
  }
  test-dependencies: {
    JUnitJupiter = 6
  }
}
```

`Java = 25` is the JDK, Amazon Corretto, at the release your version group points 25 at. Kotlin files belong in a [Kotlin](kotlin.md) project: JavaBuild refuses them.

## A program {#program}

A project that names its `main`, the class with the `main` method, is a program: `plinto run` starts it, and its release can be packaged to run on a server. Without one it is a library. Nothing is guessed from the code.

```sh
$ plinto run -- world
```

What follows `--` goes to `main`, after the arguments `args = [--port, 8080]` in `plinto.conf` names, which the packaged program starts with too. The JVM's own options go in the settings, as `run.jvm-args`.

## Settings {#settings}

Settings go in `configuration/JavaBuild.config`, one `key = value` per line, or in `plinto.conf` as `java: { release = 21 }`. A key set in both places is an error, and so is a key JavaBuild doesn't know.

| Key | What it does |
|---|---|
| `java.release` | the Java version to compile for (default: the JDK's own, such as 25) |
| `java.compiler-args` | more arguments for javac |
| `run.jvm-args` | what `plinto run` passes to the JVM: `[-Xmx512m]` |

```conf file=configuration/JavaBuild.config
java.release = 21
```

## Libraries {#libraries}

Libraries from Maven Central come in through an import, once per group, and then each project names them by their package name:

```sh
$ plinto import maven org.slf4j:slf4j-api:2.0.17
```

```conf
dependencies: {
  Slf4jApi = 2.0.17
}
```

## Tests {#tests}

Put JUnit tests in `tst/` and `JUnitJupiter` in `test-dependencies`. `plinto build` runs them and prints the tree of what passed.

## What it releases {#releases}

A reproducible jar, `build/libs/HelloApp-1.0.jar`. A program is also packed as an archive, `HelloApp-1.0-<platform>.tar.gz`, with the jar, its libraries and its own JDK, started by `HelloApp/bin/HelloApp`. A `scripts/run.sh` in the project replaces how it starts. See [Deployments](../concepts/deployments.md).

Native binaries with GraalVM are coming.

- [Kotlin](kotlin.md): Kotlin and Java side by side
- [Editors](../guides/editors.md): IntelliJ IDEA and VS Code
- [Packages](../concepts/packages.md): where Java and JUnit come from
