Skip to content

About

my Java AI book

Resources

Stars

3 stars

Watchers

2 watching

Forks

Latest commit

 

History

163 Commits

Folders and files

Repository files navigation

Source Code and Manuscript Files for the October 2026 edition of "Practical Artificial Intelligence With Java"

The new October 2026 edition is largely a refresh fro new library and tool versions and new LLM model versions.

Leanpub Link for latest edition · read the book free online

Book Cover

This book is a combination of

  • new coverage of LLMs
  • new coverage of deep learning
  • new material: creating and using knowledge graphs
  • examples from my discontinued book "Power Java": anomaly detection, linked data, using DBPedia, OpenNLP, and web scraping
  • examples from the original editions of this book: genetic algorithms and search algorithms
  • a few examples updated from my discontinued book "Practical Semantic Web and Linked Data Applications, Java Edition"

This Github Repo contains both the book manuscript and source code for the examples.

  • manuscript/ — the book chapters as Markdown. manuscript/Book.txt lists the chapter order used by Leanpub, and SOURCE_CODE_MAP.md maps each chapter to its example project.
  • source-code/ — one Maven project per chapter, plus the shared prompts/ directory of prompt templates.

Getting Started

1. Prerequisites

Tool Version Notes
JDK 21 or newer Every pom.xml compiles with --release 21; verified with Temurin 21 and 27. The optional formatter targets need a JDK 21 toolchain — see the note in section 5.
Maven 3.9 or newer Resolves dependencies and runs the examples.
GNU Make any recent version Optional convenience wrapper; every project also builds with plain Maven.
Ollama current release Only needed for the two local-LLM projects, ollama-llm-client and langchain4j-ollama.

Confirm your toolchain before starting:

java -version
mvn -version

2. API keys and environment variables

The cloud LLM examples read credentials from environment variables. Nothing needs to be set to build the code or to run the fully offline examples.

Variable Used by Purpose
OPENAI_API_KEY source-code/openai-llm-client OpenAI key (read automatically by the official OpenAI Java SDK).
GOOGLE_API_KEY source-code/gemini-llm-client Gemini API key from Google AI Studio.
GEMINI_API_KEY source-code/AgentScope_gemini Gemini key used by the AgentScope ReActAgent examples.
OLLAMA_BASE_URL source-code/langchain4j-ollama Optional; overrides the default http://localhost:11434.
GEONAMES source-code/info_gathering Optional; your free GeoNames account name for the GeoNames web-service example.

For example:

export OPENAI_API_KEY=sk-...
export GOOGLE_API_KEY=...
export GEMINI_API_KEY=...

3. One-time setup: make install

cd source-code
make install

make install runs mvn dependency:go-offline and then compiles (or packages) each project. This primes your local ~/.m2 repository so that later builds can run with Maven's offline flag (-o), and it installs the shared libraries (ner_dbpedia, semantic_web_apache_jena) before the projects that depend on them (kgc, kgn). This step needs network access the first time you run it. Setup deliberately does not run the formatter — make fmt rewrites files, so it stays a separate, optional target.

4. Fast compile check: make check

cd source-code
make check

make check compiles the main and test sources of all 15 projects (mvn -o -q test-compile) without running any tests. After make install has completed, this is fully offline.

5. Run examples and tests

Run one project:

cd source-code/openai-llm-client
make run          # or: mvn test -q

Run every project's tests:

cd source-code
make test

make test is the one target that is not run in Maven's offline mode. Surefire resolves its JUnit Platform provider at test time, and mvn dependency:go-offline cannot pre-cache it, so the first make test fetches that provider; after that it is cached like everything else. make check (compile only) stays fully offline.

Other top-level targets: make fmt (format with Spotless), make fmt-check (verify formatting), make verify (fmt-check + check), and make clean.

One toolchain caveat: the google-java-format version pinned by these projects does not run on JDK 27, so make fmt, make fmt-check, and make verify cannot run there. Those targets need a JDK 21 toolchain, and even then make fmt-check reports drift in the checked-in sources (the code is written in 4-space style while the config asks for Google's 2-space style), so make fmt will rewrite files rather than confirm them. Treat formatting as optional and use make check / make test as the real gates; those work on newer JDKs.

Which examples run fully offline, and which need keys

The table below describes what make test / make run needs. Note that make check (compile only) is offline for every project once make install has run.

Project Offline? Requirements for tests / examples
anomaly_detection Yes —
genetic-algorithms Yes —
neuralnetworks Yes — (starts a Swing GUI)
nlp Yes —
ner_dbpedia Yes Install before kgc / kgn
kgc Yes Depends on ner_dbpedia
search Yes — (starts a Swing GUI)
ollama-llm-client Locally A running Ollama server (v0.35.1+) with a small model pulled, e.g. ollama pull qwen3.5:2b
langchain4j-ollama Locally A running Ollama server with a small model pulled
openai-llm-client No OPENAI_API_KEY + network access
gemini-llm-client No GOOGLE_API_KEY + network access
AgentScope_gemini No GEMINI_API_KEY + network access
info_gathering No Network access (web scraping); GEONAMES for the GeoNames example
semantic_web_apache_jena Partly Network access for the remote DBpedia SPARQL queries; local queries are offline
kgn Partly Network access for remote SPARQL; build ner_dbpedia and semantic_web_apache_jena first

"Locally" means no cloud API key is required, but the examples still need the Ollama server running on your machine.

A note on LLM model names and versions

Model identifiers, provider APIs, and library versions change quickly. The LLM chapters and examples were audited in October 2026; where a chapter recommends a specific model or dependency version, treat it as a starting point and check the provider's current documentation before relying on it. The dependency versions that actually matter for building are the ones in each project's pom.xml.

About

my Java AI book

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages