128

java 调用 ethereum 的智能合约

lizhipingsss · 于 发布 · 最后由 halepa回复 · 1482 次阅读

有人使用过java调用过ethereum的智能合约吗,如果用web3j的话,如何与以太坊进行通讯?

  • 128
    ajian1984

    您看看这篇文章里的东西有没有用 使用 Infura 和 web3.js 呼叫合约
    或者这篇 web3.js编译Solidity,发布,调用全部流程
    如果没有用的话,及时在这里提供反馈,我再帮您找。哪篇比较有用也可以告诉我

  • 128
    lizhipingsss

    @ajian1984 我是用的web3j这个工具与ethereum进行通讯,因为我的项目是一个B/S架构的,用的java spring boot你架构,我在项目里创建了与ethereum接口类:
    package com.example.mortgage.ethereum;

    import org.web3j.protocol.Web3j;
    import org.web3j.protocol.http.HttpService;

    public class Web3JClient {
    private static String URL = "http://10.10.10.4:61911/";

    public Web3JClient(){}

    public volatile static Web3j web3j;

    public static Web3j getClient(){
    if(web3j==null){
    synchronized (Web3JClient.class){
    if(web3j==null){
    web3j = Web3j.build(new HttpService(URL));
    }
    }
    }
    return web3j;
    }

    }
    然后在我的控制器里调用:
    public class PlatformController {

    @Autowired
    PlatformService platformService;

    @RequestMapping("/index")
    public String index(Model model){

    try{
    Web3j web3j;
    Web3JClient web3jClient=new Web3JClient();
    web3j=Web3JClient.getClient();

    }catch (Exception e) {

    e.printStackTrace();

    }

    return "index";
    }
    }
    在Web3j.build(new HttpService(URL))这里出错,我这个端口我用telnet 测试,是没问题的,以下是出错信息:

    2018-05-10 12:34:48.768 ERROR 9988 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: rx/schedulers/Schedulers] with root cause

    java.lang.ClassNotFoundException: rx.schedulers.Schedulers
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_161]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_161]
    at org.web3j.protocol.rx.JsonRpc2_0Rx.(JsonRpc2_0Rx.java:39) ~[core-2.2.1.jar:na]
    at org.web3j.protocol.core.JsonRpc2_0Web3j.(JsonRpc2_0Web3j.java:90) ~[core-2.2.1.jar:na]
    at org.web3j.protocol.core.JsonRpc2_0Web3j.(JsonRpc2_0Web3j.java:83) ~[core-2.2.1.jar:na]
    at org.web3j.protocol.Web3j.build(Web3j.java:14) ~[core-2.2.1.jar:na]
    at com.example.mortgage.ethereum.Web3JClient.getClient(Web3JClient.java:17) ~[classes/:na]
    at com.example.mortgage.controller.PlatformController.index(PlatformController.java:50) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAd

  • 128
    lizhipingsss

    问题已解决,这个问题配置不当引起的

  • 128
    xjtufjj

    Java 做DAPP,应该使用web3j 哈~

    jinjun

  • 8433 large
    halepa

    我注意到智能合约现在变得非常流行。 对于普通客户来说,这确实很方便并很安全。