Commit 80d3d1c3 authored by xiezhi's avatar xiezhi

java

parent 9c24090b
FROM hb.eazytec-cloud.com/eazytec/node:ubuntu
\ No newline at end of file
FROM hb.eazytec-cloud.com/eazytec/eazydevelop-base:ubuntu
\ No newline at end of file
{
"name": "Java Development Environment",
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"build": {
"dockerfile": "Dockerfile"
},
"features": {
"ghcr.io/devcontainers/features/java:1.6.3": {
"version": "17",
"installMaven": true,
"installGradle": true
},
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": true,
"configureZshAsDefaultShell": true,
"installOhMyZsh": true,
"upgradePackages": true
}
},
"customizations": {
"vscode": {
"extensions": [
"vscjava.vscode-java-pack",
"vscjava.vscode-maven",
"vscjava.vscode-gradle",
"redhat.java",
"vscjava.vscode-java-debug",
"vscjava.vscode-java-test"
],
"settings": {
"java.home": "/usr/local/openjdk-17",
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic"
}
}
},
"postCreateCommand": "java -version && mvn -version && gradle -version",
"remoteUser": "vscode"
}
\ No newline at end of file
# Java DevContainer Demo
这是一个使用 DevContainer Features 的 Java 开发环境演示项目。
## 特性
- ✅ Java 17 运行时
- ✅ Maven 构建工具
- ✅ Gradle 构建工具
- ✅ JUnit 测试框架
- ✅ VS Code Java 扩展
- ✅ Oh My Zsh 终端
## 快速开始
1. 在 VS Code 中打开项目
2.`Ctrl+Shift+P` (Windows/Linux) 或 `Cmd+Shift+P` (Mac)
3. 选择 "Dev Containers: Reopen in Container"
4. 等待容器构建完成
## 运行项目
### 使用 Maven
```bash
# 编译
mvn compile
# 运行测试
mvn test
# 运行主类
mvn exec:java -Dexec.mainClass="com.example.App"
# 打包
mvn package
```
### 使用 Gradle
```bash
# 编译
gradle compileJava
# 运行测试
gradle test
# 运行主类
gradle run
# 打包
gradle build
```
## 项目结构
<?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>java-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Java Demo</name>
<description>A simple Java demo project</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.example;
/**
* 主应用程序类
* @author Demo User
* @version 1.0
*/
public class App {
/**
* 主方法
* @param args 命令行参数
*/
public static void main(String[] args) {
System.out.println("🎉 欢迎使用 Java DevContainer!");
System.out.println("Java 版本: " + System.getProperty("java.version"));
System.out.println("Java 供应商: " + System.getProperty("java.vendor"));
// 演示一些 Java 17 特性
demoJava17Features();
// 演示计算功能
Calculator calculator = new Calculator();
int result = calculator.add(10, 20);
System.out.println("10 + 20 = " + result);
}
/**
* 演示 Java 17 新特性
*/
private static void demoJava17Features() {
System.out.println("\n�� Java 17 新特性演示:");
// Text blocks (Java 15+)
String json = """
{
"name": "Java Demo",
"version": "17",
"features": ["Text Blocks", "Pattern Matching", "Records"]
}
""";
System.out.println("Text Blocks 示例:");
System.out.println(json);
// Pattern matching (Java 16+)
Object obj = "Hello Java 17";
if (obj instanceof String str) {
System.out.println("字符串长度: " + str.length());
}
}
}
\ No newline at end of file
package com.example;
/**
* 简单计算器类
* @author Demo User
* @version 1.0
*/
public class Calculator {
/**
* 加法运算
* @param a 第一个数
* @param b 第二个数
* @return 两数之和
*/
public int add(int a, int b) {
return a + b;
}
/**
* 减法运算
* @param a 被减数
* @param b 减数
* @return 两数之差
*/
public int subtract(int a, int b) {
return a - b;
}
/**
* 乘法运算
* @param a 第一个数
* @param b 第二个数
* @return 两数之积
*/
public int multiply(int a, int b) {
return a * b;
}
/**
* 除法运算
* @param a 被除数
* @param b 除数
* @return 两数之商
* @throws ArithmeticException 当除数为0时抛出
*/
public double divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("除数不能为0");
}
return (double) a / b;
}
}
\ No newline at end of file
package com.example;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* 计算器测试类
* @author Demo User
* @version 1.0
*/
public class CalculatorTest {
private Calculator calculator = new Calculator();
@Test
public void testAdd() {
assertEquals("加法测试失败", 15, calculator.add(10, 5));
assertEquals("加法测试失败", 0, calculator.add(-5, 5));
assertEquals("加法测试失败", -10, calculator.add(-5, -5));
}
@Test
public void testSubtract() {
assertEquals("减法测试失败", 5, calculator.subtract(10, 5));
assertEquals("减法测试失败", -10, calculator.subtract(-5, 5));
assertEquals("减法测试失败", 0, calculator.subtract(-5, -5));
}
@Test
public void testMultiply() {
assertEquals("乘法测试失败", 50, calculator.multiply(10, 5));
assertEquals("乘法测试失败", -25, calculator.multiply(-5, 5));
assertEquals("乘法测试失败", 25, calculator.multiply(-5, -5));
}
@Test
public void testDivide() {
assertEquals("除法测试失败", 2.0, calculator.divide(10, 5), 0.001);
assertEquals("除法测试失败", -1.0, calculator.divide(-5, 5), 0.001);
assertEquals("除法测试失败", 1.0, calculator.divide(-5, -5), 0.001);
}
@Test(expected = ArithmeticException.class)
public void testDivideByZero() {
calculator.divide(10, 0);
}
}
\ No newline at end of file
/Users/xiezhi/work/code/ide/cursor/devcontainer/eazy-code-server-devcontainer/src/main/java/com/example/Calculator.java
/Users/xiezhi/work/code/ide/cursor/devcontainer/eazy-code-server-devcontainer/src/main/java/com/example/App.java
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment