我正在尝试使用 grpc-java v1.1.2(下面的 build.gradle 部分),但是当我尝试为示例应用程序运行 fat jar 时,它会抛出下面给出的异常。编译应用程序时我没有看到任何问题。
build.gradle 部分:
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
}
例外
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)