11# Java Examples
22
3- This module contains Java implementations of Embabel Agent examples, demonstrating enterprise Java patterns and Spring Boot best practices.
3+ This module contains Java implementations of Embabel Agent examples, demonstrating enterprise Java patterns and Spring
4+ Boot best practices.
45
56## 🚀 Quick Start
67
78### Prerequisites
9+
810- Java 21+
911- Maven 3.9+
1012- API Keys: ` OPENAI_API_KEY ` or ` ANTHROPIC_API_KEY `
@@ -23,7 +25,7 @@ cd ../scripts/java
2325shell.cmd --docker-tools # Windows
2426
2527# Or run directly with Maven
26- mvn spring-boot:run -Dspring-boot.run.main-class=com.embabel.example.AgentShellApplication
28+ mvn spring-boot:run -Dspring-boot.run.main-class=com.embabel.example.JavaAgentShellApplication
2729```
2830
2931## 📂 Project Structure
@@ -45,16 +47,19 @@ examples-java/
4547## 🎯 Available Example
4648
4749### ** Horoscope News Finder** (Beginner-Friendly)
50+
4851Find personalized news based on someone's zodiac sign, combining horoscope readings with current events.
4952
5053** Key Concepts:**
54+
5155- ` @Agent ` and ` @Action ` annotations
5256- Spring dependency injection
5357- LLM integration for data extraction
5458- Web tool usage for news searches
5559- Goal-oriented workflows
5660
5761** Implementation Highlights:**
62+
5863``` java
5964@Agent (description = " Finds personalized news based on zodiac signs" )
6065@Component
@@ -81,6 +86,7 @@ public class StarNewsFinder {
8186```
8287
8388** Try it:**
89+
8490```
8591shell:> execute "Find horoscope news for Bob who is a Scorpio"
8692shell:> execute "What's in the stars for Alice today? She's a Gemini"
@@ -89,13 +95,15 @@ shell:> execute "What's in the stars for Alice today? She's a Gemini"
8995## 🛠️ Configuration
9096
9197### API Keys
98+
9299``` bash
93100export OPENAI_API_KEY=" your-openai-key"
94101# or
95102export ANTHROPIC_API_KEY=" your-anthropic-key"
96103```
97104
98105### Application Configuration
106+
99107``` yaml
100108# application.yml
101109embabel :
@@ -113,6 +121,7 @@ spring:
113121### Interactive Shell (Development)
114122
115123**Basic Shell Mode:**
124+
116125` ` ` java
117126import com.embabel.agent.config.annotation.LoggingThemes;
118127
@@ -127,18 +136,21 @@ public class AgentShellApplication {
127136```
128137
129138Features:
139+
130140- Command-line interface for testing
131141- Star Wars themed logging messages
132142- Real-time execution feedback
133143- Debug options with ` -p ` and ` -r ` flags
134144
135145** Shell with Docker Integration:**
146+
136147``` java
148+
137149@SpringBootApplication
138150@EnableAgentShell
139151@EnableAgents (
140- loggingTheme = LoggingThemes . STAR_WARS ,
141- mcpServers = {McpServers . DOCKER_DESKTOP }
152+ loggingTheme = LoggingThemes . STAR_WARS ,
153+ mcpServers = {McpServers . DOCKER_DESKTOP }
142154)
143155public class AgentShellMcpClientApplication {
144156 public static void main (String [] args ) {
@@ -148,12 +160,14 @@ public class AgentShellMcpClientApplication {
148160```
149161
150162Features:
163+
151164- All basic shell features
152165- Docker Desktop MCP integration
153166- Container execution capabilities
154167- Enhanced tool access
155168
156169### MCP Server (Integration)
170+
157171``` java
158172@SpringBootApplication
159173@EnableAgentMcpServer
@@ -166,6 +180,7 @@ public class AgentMcpApplication {
166180```
167181
168182Features:
183+
169184- JSON-RPC server implementation
170185- Claude Desktop compatibility
171186- Docker integration support
@@ -174,6 +189,7 @@ Features:
174189## 💡 Java Implementation Patterns
175190
176191### Spring Dependency Injection
192+
177193``` java
178194@Component
179195public class StarNewsFinder {
@@ -187,6 +203,7 @@ public class StarNewsFinder {
187203```
188204
189205### Domain Models with Validation
206+
190207``` java
191208public class StarPerson extends Person {
192209 @NotNull
@@ -200,6 +217,7 @@ public class StarPerson extends Person {
200217```
201218
202219### Builder Pattern for Complex Objects
220+
203221``` java
204222public class Writeup {
205223 private final String title;
@@ -214,6 +232,7 @@ public class Writeup {
214232```
215233
216234### Service Layer Abstraction
235+
217236``` java
218237@Service
219238public class HoroscopeService {
@@ -236,6 +255,7 @@ mvn test -Dtest=StarNewsFinderTest
236255```
237256
238257### Test Structure
258+
239259``` java
240260@SpringBootTest
241261@AutoConfigureMockMvc
@@ -253,23 +273,23 @@ class StarNewsFinderTest {
253273
254274## 🐛 Troubleshooting
255275
256- | Issue | Solution |
257- | -------| ----------|
258- | ` No API key found ` | Set ` OPENAI_API_KEY ` or ` ANTHROPIC_API_KEY ` environment variable |
259- | ` ClassNotFoundException ` | Run ` mvn clean install ` from project root |
260- | ` Port already in use ` | Change port with ` --server.port=8081 ` |
261- | ` Bean creation error ` | Check Spring profile configuration |
262- | ` WARNING: Only Basic Agent features ` | Use ` --docker-tools ` parameter to enable Docker integration |
276+ | Issue | Solution |
277+ | -------------------------------------- | -------------------------------------------------------- ----------|
278+ | ` No API key found ` | Set ` OPENAI_API_KEY ` or ` ANTHROPIC_API_KEY ` environment variable |
279+ | ` ClassNotFoundException ` | Run ` mvn clean install ` from project root |
280+ | ` Port already in use ` | Change port with ` --server.port=8081 ` |
281+ | ` Bean creation error ` | Check Spring profile configuration |
282+ | ` WARNING: Only Basic Agent features ` | Use ` --docker-tools ` parameter to enable Docker integration |
263283
264284## 📊 Comparison with Kotlin Examples
265285
266- | Feature | Java | Kotlin |
267- | ---------| ------| --------|
268- | ** Code Style** | Verbose, explicit | Concise, expressive |
269- | ** Null Safety** | Manual checks | Built-in null safety |
270- | ** Async Support** | CompletableFuture | Coroutines |
271- | ** Data Classes** | Lombok/Records | Native data classes |
272- | ** DSL Support** | Builder pattern | Native DSL support |
286+ | Feature | Java | Kotlin |
287+ | ------------------- | ------------------- | -------------- --------|
288+ | ** Code Style** | Verbose, explicit | Concise, expressive |
289+ | ** Null Safety** | Manual checks | Built-in null safety |
290+ | ** Async Support** | CompletableFuture | Coroutines |
291+ | ** Data Classes** | Lombok/Records | Native data classes |
292+ | ** DSL Support** | Builder pattern | Native DSL support |
273293
274294## 🎓 Learning Resources
275295
@@ -281,6 +301,7 @@ class StarNewsFinderTest {
281301## 🤝 Contributing
282302
283303When adding new Java examples:
304+
2843051 . Follow Java naming conventions
2853062 . Use meaningful variable and method names
2863073 . Add comprehensive JavaDoc
0 commit comments