This commit is contained in:
黎润豪 2026-01-09 21:36:42 +08:00
parent 32580b0c14
commit cbc87213d1
4 changed files with 192 additions and 0 deletions

2
.gitignore vendored
View File

@ -11,6 +11,8 @@
# AWS User-specific
.idea/**/aws.xml
/.idea
/target
# Generated files
.idea/**/contentModel.xml

40
pom.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>cc-process-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 在这里添加项目依赖 -->
<dependency>
<groupId>net.cyclingbits</groupId>
<artifactId>claude-code-sdk-java</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,121 @@
package com.example;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class CommandRunDemo {
public static void main(String[] args) {
executeClaudeCommand(new File("D:/test"), "创建一个测试文件到这个目录");
}
/**
* 执行命令
* @param dir 执行命令的目录
* @param command 执行的命令
* @return 执行是否正常完成
*/
public static boolean executeCommand(File dir, String... command) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
if (!dir.exists()) {
if (!dir.mkdirs()) {
// throw new BusinessException("创建目录失败");
}
}
processBuilder.directory(dir);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
System.out.println(line);
}
}
int exitCode = process.waitFor();
if (exitCode == 0) {
// log.info("命令执行成功:{}", String.join(" ", command));
return true;
} else {
// log.error("命令执行失败,退出码:{},输出:{}", exitCode, output);
return false;
}
} catch (Exception e) {
// log.error("执行G命令异常{}", String.join(" ", command), e);
return false;
}
}
/**
* 调用CC
* @param dir 访问目录
* @param prompt CC执行提示词
* @return 执行是否正常完成
*/
public static boolean executeClaudeCommand(File dir, String prompt) {
try {
// log.info("使用ClaudeCode执行提示词{}", prompt);
ProcessBuilder processBuilder = new ProcessBuilder(
"claude", //需要保证claude在PATH中某些npm的特殊安装方法可能不适用如fnm安装
"-p", prompt,
"--output-format", "stream-json",
"--dangerously-skip-permissions",
"--verbose"
);
processBuilder.directory(dir);
// processBuilder.redirectErrorStream();// 将错误流重定向到标准输出流
Process process = processBuilder.start();
// claude的bug不手工关闭输入流的话会卡死
if (process.getOutputStream() != null) {
process.getOutputStream().close();
}
// 处理输入流(脚本输出内容)
new Thread(() -> handleStream(process.getInputStream(), false)).start();
// 处理错误流(脚本错误输出)
new Thread(() -> handleStream(process.getErrorStream(), true)).start();
int exitCode = process.waitFor();
// log.info("ClaudeCode执行结束结束码{}", exitCode);
return exitCode == 0;
} catch (Exception e) {
// log.error("使用ClaudeCode生成修复代码失败", e);
e.printStackTrace();
return false;
}
}
/**
* 处理流
*
* @param inputStream 输入流
* @param error 是否为错误流
*/
private static void handleStream(java.io.InputStream inputStream, boolean error) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, "GBK"))) { // 使用GBK编码适应Windows环境
String line;
while ((line = reader.readLine()) != null) {
if (error) {
System.err.println(line);
} else {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,29 @@
package com.example;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlinx.coroutines.flow.Flow;
import net.cyclingbits.claudecode.api.ClaudeCodeClient;
import net.cyclingbits.claudecode.types.*;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
*
* @author lroyia
* @since 2026/1/9 20:54
**/
public class SdkRunDemo {
public static void main(String[] args) {
ClaudeCodeClient claudeCodeClient = new ClaudeCodeClient();
ClaudeCodeOptions options = ClaudeCodeOptions.builder()
.permissionMode(PermissionMode.BYPASS_PERMISSIONS)
// .cwd()// 设置工作路径
.build();
List<Message> join = claudeCodeClient.queryAsync("在当前目录创建一个空的test.txt文件", options).join();
join.forEach(System.out::println);
}
}