Ollama4J
A simple Java library for interacting with Ollama server.
Last verified:
What is Ollama4J?
Ollama4j is a Java library (wrapper/binding) designed to integrate Ollama server with Java applications. It provides clean and simple APIs for interacting with Ollama, enabling Java developers to leverage large language models (LLMs) running locally on their machines without directly calling Ollama's REST APIs.
Key features include model management (list, pull, create, delete models), text generation, chat API for multi-turn conversations, embeddings generation, tool/function calling with MCP tools support, authentication support (Basic and Bearer), and image/vision model support for chat with images. The library supports tool-calling models like Mistral, Llama3.x, and Qwen, and offers annotation-based tool registration using @ToolSpec and @ToolProperty annotations.
Ollama4j is designed for Java developers who want to build AI-powered applications using local LLMs. It is suitable for building chatbots, AI agents, database query tools, desktop UI applications (like the Ollama4j UI built with Swing), Minecraft mods, Telegram bots, Jenkins tools with AI error analysis, and research applications. The library requires Ollama server, Oracle JDK or OpenJDK 11.0+, and Maven.
Ollama4J pricing
Pricing model: Freemium
Open source and completely free under MIT License. No paid plans or tiers. The library is available on Maven Central Repository and GitHub package repositories at no cost. Users only need to provide their own Ollama server (which is also free) and Java development environment.
Ollama4J pros
- Open source under MIT License
- Clean and simple Java APIs for Ollama interaction
- Supports model management (list, pull, create, delete)
- Multi-turn chat API with conversation support
- Tool/function calling with MCP tools support
- Annotation-based tool registration with @ToolSpec and @ToolProperty
- Supports vision/image models for chat with images
- Authentication support (Basic and Bearer) for proxy deployment
- Streaming API support for tool calling
- Available on Maven Central and GitHub package repositories
- Works with tool-calling models like Mistral, Llama3.x, Qwen
- Client-managed tool calls with useTools flag for custom control
- Testcontainers support for integration testing
- Active GitHub repository with 375+ stars
- Comprehensive javadoc documentation available
- Supports both CPU and GPU Ollama server modes
- Works with Docker-based Ollama server deployment
Ollama4J cons
- Requires Ollama server to be running separately
- Requires JDK 11.0 or above (not compatible with older Java versions)
- Requires Maven for project setup
- Depends on Docker for easiest Ollama server setup
- Needs SLF4J logger implementation (slf4j-jdk14 or logback-classic) added separately
- Only works with Ollama server (not other LLM providers)
- Limited to local models hosted on Ollama
- Windows users need Chocolatey to install make for development
- Integration tests require Docker running
Frequently asked questions about Ollama4J
What is Ollama4j?
Ollama4j is a Java library (wrapper/binding) for Ollama server. It was built for the simple purpose of integrating Ollama with Java applications, providing clean and simple APIs for interacting with Ollama including model management, chat functionalities, and authentication capability.
What do I need to get started with Ollama4j?
You need three things: Ollama server (running locally at http://localhost:11434/), Oracle JDK or OpenJDK 11.0 or above, and Maven for project setup. The easiest way to start Ollama server is with Docker.
How do I install Ollama4j?
For Maven, add the dependency to your pom.xml: <dependency><groupId>io.github.ollama4j</groupId><artifactId>ollama4j</artifactId><version>1.0.100</version></dependency>. For Gradle, add: implementation 'io.github.ollama4j:ollama4j:1.0.100'. The library is published on both Maven Central and GitHub package repositories.
What models does Ollama4j support?
Ollama4j works with any model supported by Ollama server, including Llama 2, Llama3.x, Mistral, Qwen, and vision/image models. It particularly supports tool-calling models like Mistral, Llama3.x, and Qwen for function calling features.
How do I use tool calling with Ollama4j?
Ollama4j supports tool/function calling including MCP tools. You can integrate tools directly into the chat() method for natural back-and-forth chat, or use generateWithTools() method. Tool registration can be done declaratively using @ToolSpec and @ToolProperty annotations with @OllamaToolService.
Can I chat with images using Ollama4j?
Yes, Ollama4j supports interactions with vision/image models, allowing you to build applications that can understand and respond to visual content. You can upload images directly into chat sessions and receive intelligent, context-aware replies.
How do I authenticate with Ollama server?
Ollama4j supports authentication when Ollama is deployed behind a proxy server. It provides support for both Basic authentication and Bearer authentication capability.
What is the difference between auto-executing tools and client-managed tools?
By default, ollama4j automatically executes tool calls returned by the model during chat and appends results back into conversation. When useTools flag is set to true, ollama4j stops auto-executing tools and returns tool calls inside the assistant message, allowing you to inspect and execute them manually for custom orchestration.
How do I run Ollama server with Docker?
For CPU mode: docker run -it -v ~/ollama:/root/.ollama -p 11434:11434 ollama/ollama. For GPU mode: docker run -it --gpus=all -v ~/ollama:/root/.ollama -p 11434:11434 ollama/ollama. This runs the Ollama server locally at http://localhost:11434/.
Is Ollama4j free to use?
Yes, Ollama4j is completely free and open source under the MIT License. There are no paid plans or tiers. The library is available on Maven Central Repository and GitHub package repositories at no cost.