A command-line Java tool can call a local language model without adding an HTTP library. The useful first step is a small client that reports failures clearly, rather than hiding server errors behind a blank result.

What you will build

Send a JSON prompt to a local Ollama server using Java 11 HttpClient, with explicit timeouts and HTTP error handling.

Before you start

JDK 11 or later. Install Ollama locally and run ollama pull llama3.2 before trying the real request.

1. Create LocalAi.java

import java.net.URI;
import java.net.http.*;
import java.time.Duration;

public class LocalAi {
    public static void main(String[] args) throws Exception {
        String endpoint = args.length == 0
            ? "http://127.0.0.1:11434/api/generate" : args[0];
        HttpClient client = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(5)).build();
        String payload = "{\"model\":\"llama3.2\",\"prompt\":\"Explain a Java interface in two sentences.\",\"stream\":false}";
        HttpRequest request = HttpRequest.newBuilder(URI.create(endpoint))
            .timeout(Duration.ofSeconds(60))
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(payload)).build();
        HttpResponse<String> reply = client.send(request,
            HttpResponse.BodyHandlers.ofString());
        if (reply.statusCode() < 200 || reply.statusCode() >= 300) {
            throw new IllegalStateException("AI server HTTP " + reply.statusCode());
        }
        System.out.println(reply.body());
    }
}

2. Run the example

javac LocalAi.java
java LocalAi

Expected result

A JSON response envelope is printed. Its response field contains the model’s explanation. The exact generated explanation is not fixed.

How it works

The connect timeout bounds connection establishment; the request timeout applies to the HTTP exchange. A local model may need time to load, so the two values serve different purposes.

This introductory client prints the response envelope intentionally. If you later extract response using a JSON library, validate that it is a string. Do not split JSON with regular expressions or concatenate unescaped user text into the JSON payload.

The endpoint argument makes local testing possible with a fixture server. In a deployed web service, keep the upstream address in trusted configuration rather than accepting arbitrary URLs from users. Reuse a client across repeated calls.

Test the edge cases

  • Stop Ollama: the program should fail with a connection error rather than claiming success.
  • Try a missing model: a non-2xx response should produce an explicit HTTP error.
  • Keep stream false for this example; streaming requires reading separate response chunks.

Keep learning

Browse related tutorials. Reference: official documentation.

Reviewed 7 November 2026. AI-dependent examples require your own local model; generated text varies.