### Maven Dependency for ADK
Source: https://adk.dev/get-started/java/#create-an-agent-project
Add this dependency to your `pom.xml` file to include the core ADK library in your project. Ensure Maven 3.9 or later is installed.
```xml
com.google.adk
google-adk
1.0.0
```
--------------------------------
### Create Agent Project Structure (Windows)
Source: https://adk.dev/get-started/java/#create-an-agent-project
Use these commands to create the necessary directory structure and empty files for your agent project on Windows.
```bash
mkdir my_agent\src\main\java\com\example\agent
type nul > my_agent\src\main\java\com\example\agent\HelloTimeAgent.java
type nul > my_agent\src\main\java\com\example\agent\AgentCliRunner.java
type nul > my_agent\pom.xml
type nul > my_agent\.env
```
--------------------------------
### Create Agent Project Structure (MacOS/Linux)
Source: https://adk.dev/get-started/java/#create-an-agent-project
Use these commands to create the necessary directory structure and empty files for your agent project on MacOS or Linux.
```bash
mkdir -p my_agent/src/main/java/com/example/agent && \
touch my_agent/src/main/java/com/example/agent/HelloTimeAgent.java && \
touch my_agent/src/main/java/com/example/agent/AgentCliRunner.java && \
touch my_agent/pom.xml my_agent/.env
```
--------------------------------
### Set Gemini API Key Environment Variable
Source: https://adk.dev/get-started/java/#create-an-agent-project
Configures the GOOGLE_API_KEY environment variable for local development.
```bash
echo 'export GOOGLE_API_KEY="YOUR_API_KEY"' > .env
```
```batch
echo 'set GOOGLE_API_KEY="YOUR_API_KEY"' > env.bat
```
--------------------------------
### Define Basic Agent Code (HelloTimeAgent.java)
Source: https://adk.dev/get-started/java/#create-an-agent-project
This Java code defines a basic agent named 'hello-time-agent' that can tell the current time in a specified city using a mock 'getCurrentTime' function tool. Ensure Java 17 or later is used.
```java
package com.example.agent;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.Annotations.Schema;
import com.google.adk.tools.FunctionTool;
import java.util.Map;
public class HelloTimeAgent {
public static BaseAgent ROOT_AGENT = initAgent();
private static BaseAgent initAgent() {
return LlmAgent.builder()
.name("hello-time-agent")
.description("Tells the current time in a specified city")
.instruction("""
You are a helpful assistant that tells the current time in a city.
Use the 'getCurrentTime' tool for this purpose.
""")
.model("gemini-2.5-flash")
.tools(FunctionTool.create(HelloTimeAgent.class, "getCurrentTime"))
.build();
}
/** Mock tool implementation */
@Schema(description = "Get the current time for a given city")
public static Map getCurrentTime(
@Schema(name = "city", description = "Name of the city to get the time for") String city) {
return Map.of(
"city", city,
"forecast", "The time is 10:30am."
);
}
}
```
--------------------------------
### Complete pom.xml Configuration
Source: https://adk.dev/get-started/java/#create-an-agent-project
This is a complete `pom.xml` configuration for an ADK agent project, including Java compiler settings and necessary ADK dependencies. Ensure Java 17 and Maven 3.9+ are used.
```xml
4.0.0
com.example.agent
adk-agents
1.0-SNAPSHOT
17
17
UTF-8
com.google.adk
google-adk
1.0.0
com.google.adk
google-adk-dev
1.0.0
```
--------------------------------
### Run ADK Agent via Maven
Source: https://adk.dev/get-started/java/#create-an-agent-project
Executes the agent using either the custom CLI runner or the built-in ADK web server.
```bash
# Remember to load keys and settings: source .env OR env.bat
mvn compile exec:java -Dexec.mainClass="com.example.agent.AgentCliRunner"
```
```bash
# Remember to load keys and settings: source .env OR env.bat
mvn compile exec:java \
-Dexec.mainClass="com.google.adk.web.AdkWebServer" \
-Dexec.args="--adk.agents.source-dir=target --server.port=8000"
```
--------------------------------
### Create AgentCliRunner Java Class
Source: https://adk.dev/get-started/java/#create-an-agent-project
Implements a command-line interface to interact with an ADK agent using InMemoryRunner and Session services.
```java
package com.example.agent;
import com.google.adk.agents.RunConfig;
import com.google.adk.events.Event;
import com.google.adk.runner.InMemoryRunner;
import com.google.adk.sessions.Session;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Flowable;
import java.util.Scanner;
import static java.nio.charset.StandardCharsets.UTF_8;
public class AgentCliRunner {
public static void main(String[] args) {
RunConfig runConfig = RunConfig.builder().build();
InMemoryRunner runner = new InMemoryRunner(HelloTimeAgent.ROOT_AGENT);
Session session = runner
.sessionService()
.createSession(runner.appName(), "user1234")
.blockingGet();
try (Scanner scanner = new Scanner(System.in, UTF_8)) {
while (true) {
System.out.print("\nYou > ");
String userInput = scanner.nextLine();
if ("quit".equalsIgnoreCase(userInput)) {
break;
}
Content userMsg = Content.fromParts(Part.fromText(userInput));
Flowable events = runner.runAsync(session.userId(), session.id(), userMsg, runConfig);
System.out.print("\nAgent > ");
events.blockingForEach(event -> {
if (event.finalResponse()) {
System.out.println(event.stringifyContent());
}
});
}
}
}
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.