Execute commands, operate files, and run code in cloud environments
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>agentbay-sdk</artifactId>
<version>0.15.0</version>
</dependency>implementation 'com.aliyun:agentbay-sdk:0.15.0'Before using the SDK, you need to:
- Register an Alibaba Cloud account: https://aliyun.com
- Get API credentials: AgentBay Console
- Set environment variable:
export AGENTBAY_API_KEY=your_api_key
import com.aliyun.agentbay.*;
// Create session
AgentBay agentBay = new AgentBay();
CreateSessionParams params = new CreateSessionParams();
SessionResult result = agentBay.create(params);
if (result.isSuccess()) {
Session session = result.getSession();
// Execute command
CommandResult cmdResult = session.getCommand().executeCommand("ls -la");
System.out.println(cmdResult.getOutput());
// File operations
session.getFileSystem().writeFile("/tmp/test.txt", "Hello World");
FileContentResult content = session.getFileSystem().readFile("/tmp/test.txt");
System.out.println(content.getContent()); // Hello World
// Clean up
session.delete();
}The SDK automatically reads configuration from environment variables:
export AGENTBAY_API_KEY=your_api_key
export AGENTBAY_ENDPOINT=wuyingai.cn-shanghai.aliyuncs.com # Optional
export AGENTBAY_REGION_ID=cn-shanghai # Optional
export AGENTBAY_TIMEOUT_MS=60000 # OptionalAgentBay agentBay = new AgentBay();You can also provide configuration explicitly:
String apiKey = "your_api_key";
Config config = new Config("cn-shanghai", "wuyingai.cn-shanghai.aliyuncs.com", 60000);
AgentBay agentBay = new AgentBay(apiKey, config);Or just override the API key:
AgentBay agentBay = new AgentBay("your_api_key");The SDK uses the following precedence order (highest to lowest):
- Explicitly passed configuration in code
- Environment variables
- Default configuration
- 📚 Quick Start Tutorial - Get started in 5 minutes
- 🎯 Core Concepts - Understand cloud environments and sessions
Choose Your Cloud Environment:
- 🌐 Browser Use - Web scraping, browser testing, form automation
- 🖥️ Computer Use - Windows desktop automation, UI testing
- 📱 Mobile Use - Android UI testing, mobile app automation
- 💻 CodeSpace - Code execution, development environments
Additional Resources:
- 📖 Feature Guides - Complete feature introduction
- 🔧 Java API Reference - Detailed API documentation
- 💻 Java Examples - Complete example code
- 📋 Logging Configuration - Configure logging levels and output
// Create session
SessionResult result = agentBay.create(new CreateSessionParams());
Session session = result.getSession();
// List sessions by labels
Map<String, String> labels = new HashMap<>();
labels.put("environment", "production");
SessionListResult listResult = agentBay.list(labels, 10);
// Delete session
session.delete();// Read and write files
session.getFileSystem().writeFile("/path/file.txt", "content");
FileContentResult content = session.getFileSystem().readFile("/path/file.txt");
System.out.println(content.getContent());
// List directory
DirectoryListResult files = session.getFileSystem().listDirectory("/path");// Execute command
CommandResult result = session.getCommand().executeCommand("java MyClass.java");
System.out.println(result.getOutput());// Create context
Context context = agentBay.getContext().get("my-project", true).getContext();
// Create session with context
ContextSync contextSync = ContextSync.create(
context.getId(),
"/tmp/data",
SyncPolicy.defaultPolicy()
);
CreateSessionParams params = new CreateSessionParams()
.setContextSyncs(Arrays.asList(contextSync));
Session session = agentBay.create(params).getSession();// Run code in isolated environment
CreateSessionParams params = new CreateSessionParams().setImageId("code_latest");
Session session = agentBay.create(params).getSession();
CodeExecutionResult result = session.getCode().runCode("print('Hello World')", "python");
if (result.isSuccess()) {
System.out.println(result.getResult()); // Hello World
}This project is licensed under the Apache License 2.0 - see the LICENSE file for details.