---
title: "以太坊java库EthereumJ简介"
author: "it_node"
date: "2018-08-08T10:08:02Z"
tags: []
categories: ["企业架构"]
slug: "java-ethereumj"
draft: false
lastmod: "2018-08-08T10:07:38Z"
id: 2428297
url: "https://it-node.iteye.com/blog/2428297"
---

EthereumJ是以太坊协议的纯Java实现。有关以太坊及其目标的高级信息，请访问[ethereum.org](https://www.jianshu.com/ethereum.org)，其[白皮书](https://github.com/ethereum/wiki/wiki/White-Paper)提供了一个完整的概念的概述，和[黄皮书](http://gavwood.com/Paper.pdf)一起提供了协议的正式定义。

我们尽可能保持EthereumJ简单。对于[JSON-RPC](http://cw.hubwiz.com/card/c/ethereum-json-rpc-api/)支持和其他客户端功能，请看[Ethereum Harmony](https://github.com/ether-camp/ethereum-harmony)。

### 运行EthereumJ

作为依赖项添加到Maven项目：

```
   <dependency>
     <groupId>org.ethereum</groupId>
     <artifactId>ethereumj-core</artifactId>
     <version>1.8.1-RELEASE</version>
   </dependency>
```

或者你的Gradle项目：

```
repositories {
       mavenCentral()
       jcenter()
       maven { url "https://dl.bintray.com/ethereum/maven/" }
   }
   compile "org.ethereum:ethereumj-core:1.8.+" 
```

作为你项目的起点，任何一个项目都可以以下列方式启动：

```
git clone https://github.com/ether-camp/ethereumj.starter
./gradlew run
```

检查你的本地区块链如何同步使用：

```
 curl -w "\n" -X GET http://localhost:8080/bestBlock
```

构建可执行的JAR

```
git clone https://github.com/ethereum/ethereumj
cd ethereumj
cp ethereumj-core/src/main/resources/ethereumj.conf ethereumj-core/src/main/resources/user.conf
vim ethereumj-core/src/main/resources/user.conf # adjust user.conf to your needs
./gradlew clean fatJar
java -jar ethereumj-core/build/libs/ethereumj-core-*-all.jar
```

从命令行运行：

```
> git clone https://github.com/ethereum/ethereumj
> cd ethereumj
> ./gradlew run [-PmainClass=<sample class>]
```

可以运行任意一个测试示例：

```
./gradlew run -PmainClass=org.ethereum.samples.BasicSample
./gradlew run -PmainClass=org.ethereum.samples.FollowAccount
./gradlew run -PmainClass=org.ethereum.samples.PendingStateSample
./gradlew run -PmainClass=org.ethereum.samples.PriceFeedSample
./gradlew run -PmainClass=org.ethereum.samples.PrivateMinerSample
./gradlew run -PmainClass=org.ethereum.samples.TestNetSample
./gradlew run -PmainClass=org.ethereum.samples.TransactionBomb 
```

 快照构建

请注意，快照不稳定，目前正在开发中！如果你还想尝试一下：

- 将[https://oss.jfrog.org/libs-snapshot/](https://oss.jfrog.org/libs-snapshot/)添加为构建脚本的存储库。
- 添加依赖项`org.ethereum:ethereumj-core:${VERSION}`，`${VERSION}`位置的值是`1.9.0-SNAPSHOT`。

示例如下：

```
<repository>
    <id>jfrog-snapshots</id>
    <name>oss.jfrog.org</name>
    <url>https://oss.jfrog.org/libs-snapshot/</url>
    <snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- ... -->
<dependency>
   <groupId>org.ethereum</groupId>
   <artifactId>ethereumj-core</artifactId>
   <version>1.9.0-SNAPSHOT</version>
</dependency>
```

将项目导入IntelliJ IDEA：

```
> git clone https://github.com/ethereum/ethereumj
> cd ethereumj
> gradlew build
```

IDEA：

- File -&gt; New -&gt; Project from existing sources......
- 选择 ethereumj/build.gradle
- 对话框“Import Project from gradle”：点击“ok”
- 在构建之后运行`org.ethereum.Start`，`org.ethereum.samples.*`之一或创建自己的主程序。

### 配置EthereumJ

有关所有现有选项，其描述和默认值的参考，你可以参考默认配置`ethereumj.conf`（你可以在jar库或源代码`ethereum-core/src/main/resources`目录中找到它），要重新设置所需的选项，你可以使用以下方法之一：

- 把你的选项放到`<working dir>/config/ethereumj.conf`文件中
- 把`user.conf`放到classpath的启动目录中（作为一个资源）
- 将你的选项放在任何文件中并通过它提供`-Dethereumj.conf.file=<your config>`
- 编程通过`SystemProperties.CONFIG.override*()`使用
- 编程重写Spring `SystemPropertiesbean`来使用

请注意，不需要将所有选项都放在自定义配置中，只需要放置你需要修改的那些选项。

如果你希望尽快的使用java开始以太坊的开发，可以选择web3j类库，它封装实现了绝大部分web3的功能，十分好用：

[java以太坊教程](http://xc.hubwiz.com/course/5b2b6e82c02e6b6a59171de2?affid=88iteye)，主要是针对java和android程序员进行区块链以太坊开发的web3j详解。