bindableService issue with grpc-java

我是研究僧i 提交于 2021-02-05 11:25:26

问题


I am trying to use grpc-java v1.1.2 (build.gradle section below) but when I try to run the fat jar for the sample application, its throwing the exception given below. I do not see any issues when compiling the application.

build.gradle parts:

    apply plugin: 'com.google.protobuf'

    buildscript {
        repositories {
            mavenCentral()
            mavenLocal()
        }
        dependencies {
            // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
            // gradle versions
            classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
        }
    }


    def grpcVersion = '1.1.2'   //''1.2.0-SNAPSHOT' // CURRENT_GRPC_VERSION

    dependencies {
        compile "io.grpc:grpc-netty:${grpcVersion}"
        compile "io.grpc:grpc-protobuf:${grpcVersion}"
        compile "io.grpc:grpc-stub:${grpcVersion}"
        compile 'me.grapebaba:hyperledger-java-client:0.1.3'
}


    protobuf {
        protoc {
            artifact = 'com.google.protobuf:protoc:3.2.0'
        }
        plugins {
            grpc {
                artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
            }
        }
        generateProtoTasks {
            all()*.plugins {
                grpc {
                    // To generate deprecated interfaces and static bindService method,
                    // turn the enable_deprecated option to true below:
                    option 'enable_deprecated=false'
                }
            }
        }
    }

    idea {
        module {
            // Not using generatedSourceDirs because of
            // https://discuss.gradle.org/t/support-for-intellij-2016/15294/8
            sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
            sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
        }
    }


    jar {
        manifest {
               attributes(
                    'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                    'Main-Class': 'com.test.io.grpc.HelloWorldServer'
            )
        }
    }



    task helloWorldServer(type: CreateStartScripts) {
        mainClassName = 'io.grpc.mgcs.HelloWorldServer'
        applicationName = 'hello-world-server'
        outputDir = new File(project.buildDir, 'tmp')
        classpath = jar.outputs.files + project.configurations.runtime
    }

    task helloWorldClient(type: CreateStartScripts) {
        mainClassName = 'io.grpc.mgcs.HelloWorldClient'
            applicationName = 'hello-world-client'
            outputDir = new File(project.buildDir, 'tmp')
            classpath = jar.outputs.files + project.configurations.runtime
    }

    applicationDistribution.into('bin') {
        from(helloWorldServer)
        from(helloWorldClient)
        fileMode = 0755
    }

exception

Caused by: java.lang.ClassNotFoundException: io.grpc.BindableService
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

回答1:


I fixed the issue by using the shadow plugin for creating fat jars.

    buildscript {
        repositories {
            mavenCentral()
            mavenLocal()
        }
        dependencies {
            classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
            classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
            classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
        }
    }

    plugins {
        id 'java'
        id 'application'
        id 'idea'
        id 'com.github.johnrengelman.shadow' version '1.2.4'
        id "net.ltgt.errorprone" version '0.0.9'
    }


    shadowJar {
        baseName = 'shadow'
        classifier = null
        version = null
    }

jar {
    manifest {
        attributes(
                'Class-Path': configurations.runtime.files.collect {"$it.name"}.join(' '),
                'Main-Class': 'com.abc.test'
        )
    }
}

Run the following:

gradle shadowJar

the shadowJar file gets created in the build/libs directory which can be run as:

java -jar shadowJar



回答2:


The issue for me was, jar did not contain the dependent lib classes. I had to make this change to resolve it :

jar {
    ...
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Which is just another way to create fat jar :)



来源:https://stackoverflow.com/questions/42540531/bindableservice-issue-with-grpc-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!