I was trying to update to latest kotlin verion 1.4.30 an Apache Beam dataflow pipeline that is currently running with kotlin 1.4.21 but as soon as I upgrade build.gradle with version 1.4.30 the compilation fails with this exception:
java.lang.IllegalStateException: Could not read class: VirtualFile: /Users/stefanomassera/.gradle/caches/modules-2/files-2.1/org.apache.beam/beam-sdks-java-core/2.26.0/b57f8fa5ae66564c7ffafde34b690057f471bfa8/beam-sdks-java-core-2.26.0.jar!/org/apache/beam/sdk/options/PipelineOptions.class
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass.<init>(BinaryJavaClass.kt:120)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass.<init>(BinaryJavaClass.kt:34)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl.findClass(KotlinCliJavaFileManagerImpl.kt:115)
at org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade$CliFinder.findClass(KotlinJavaPsiFacade.java:484)
... omissis for brevity
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Wildcard mast have a bound for annotation of WILDCARD_BOUND position
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaAnnotation$Companion.computeTargetType$resolution_common_jvm(Annotations.kt:188)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.AnnotationsAndParameterCollectorMethodVisitor.visitTypeAnnotation$getTargetType(Annotations.kt:111)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.AnnotationsAndParameterCollectorMethodVisitor.visitTypeAnnotation(Annotations.kt:117)
at org.jetbrains.org.objectweb.asm.ClassReader.readMethod(ClassReader.java:1427)
at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:719)
at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:402)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass.<init>(BinaryJavaClass.kt:115)
... 101 more
I've also tried with a blank dummy project just to verify if it is a compatibility error with current kotlin and apache beam and it seems a compatibility issue, here my dummy test
main.kt
package org.example
import org.apache.beam.sdk.Pipeline
import org.apache.beam.sdk.options.PipelineOptionsFactory
class DummyPipeline {
fun main(args: Array<String>) {
val options = PipelineOptionsFactory.fromArgs(*args)
.withValidation()
.`as`(DummyPipelineOptions::class.java)
val pipeline = Pipeline.create(options)
pipeline.run()
}
}
build.gradle
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.30' //<-- do not build
//id 'org.jetbrains.kotlin.jvm' version '1.4.21' // <-- correctly build
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.apache.beam:beam-sdks-java-io-google-cloud-platform:2.26.0"
runtimeOnly "org.apache.beam:beam-runners-direct-java:2.26.0"
}
Did anyone solved this compatibility issue?
It causes by the Kotlin compiler.
Sorry for nuisance, I'm currently fixing that on the compiler's side. The fix will be available in Kotlin 1.5-M1.
Unfortunately, there are no normal workarounds here, since the problem occurs when reading class files (it's impossible to exclude the problematic logic in the mechanism for reading class files).
Related
I try to run a very simple gradle project which uses java 9 modules, but i receive the following error.
/home/vadim/IdeaProjects/test_modules/src/main/java/module-info.java:2: error: module not found: HdrHistogram
requires HdrHistogram;
^
Here is it https://github.com/vad0/test_modules.
The main class does basically nothing.
package app;
import org.HdrHistogram.Histogram;
public class RunHdr {
public static void main(String[] args) {
final Histogram histogram = new Histogram(5);
System.out.println(histogram);
}
}
It uses only one dependency: HdrHistogram. I included this magic command in build.gradle according to official gradle tutorial https://docs.gradle.org/current/samples/sample_java_modules_multi_project.html.
java {
modularity.inferModulePath = true
}
The whole build.gradle looks like this.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
java {
modularity.inferModulePath = true
}
dependencies {
compile group: 'org.hdrhistogram', name: 'HdrHistogram', version: '2.1.12'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
module.info looks like this
module test.modules.main {
requires HdrHistogram;
}
I have already read a number of tutorials on Jigsaw and a whole bunch of stackoverflow questions related to it, but still can't make this simple example work. How do i fix it?
Thank you
Unfortunately, gradle does not treat every jar as a module (in simple words). If you want to find out how exactly is gradle building the module-path (as opposed to class-path), you probably want to start from here, specifically at the isModuleJar method. It's pretty easy to understand (though it took me almost two days to set-up gradle and debug the problem out) that the dependency that you are trying to use : gradle says that it is not a module (it isn't wrong, but I am not sure it is correct either). To make it very correct, gradle will add your dependency to the CLASSPATH, but in the very next line: it will not add your dependency to the module-path, because if fails the filter in isModuleJar.
I do not know if this is a bug or not, or may be this is on purpose, but the solution is easy:
plugins.withType(JavaPlugin).configureEach {
java {
modularity.inferModulePath = true
}
tasks.withType(JavaCompile) {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files()
}
}
}
you add it to the path, on purpose. I will flag this as a defect and let's see what they have to say.
EDIT
Even better, use a plugin that is written by a gradle commiter:
plugins {
id 'java'
id 'de.jjohannes.extra-java-module-info' version "0.1"
}
And the easiest option on your case is to do :
extraJavaModuleInfo {
automaticModule("HdrHistogram-2.1.12.jar", "HdrHistogram")
}
I am trying to build some java 1.6 application with aspects after migrating the build from some older gradle version to gradle 6.3. Because of the newer gradle version, I am using java 9.
The aspect compiler throws ClassCastException (link to log below), which, if I understand correctly, is caused by compiler not finding java.lang.Object class. I am only guessing it might something to do with using java 9 to run the build, but targeting java 1.6 compatibility. If this is the case, how do I tell the compiler where to find it? I tried to pass the -1.6 argument to ajc, but nothing changed.
I also include my build.gradle contents - I apologize it is not fully in gradle 6.3, because I got stuck on this aspectj compilation problem, before migrating it fully.
Thanks in advance for every response.
build.gradle contents:
buildscript {
repositories {
maven {
url "${artifactoryURL}/libs-releases"
}
}
dependencies {
classpath "io.freefair.gradle:aspectj-plugin:5.0.0-rc6"
classpath "gradle.plugin.org.myire:quill:2.3.1"
}
}
apply plugin: 'java'
apply plugin: 'io.freefair.aspectj.post-compile-weaving'
apply plugin: 'org.myire.quill.cobertura'
apply from: 'https://localnet/public/gradle/trunk/repositories.gradle'
java {
sourceCompatibility = JavaVersion.VERSION_1_6
}
group = "cz.svt"
version = "${version}"
aspectj {
version = "1.6.8"
}
configurations.all {
transitive = false
}
dependencies {
compile('avalon:avalon:4.2.0')
compile('cglib:cglib-nodep:2.2')
compile('commons-beanutils:commons-beanutils:1.7.0')
compile('commons-codec:commons-codec:1.3')
compile('commons-collections:commons-collections:3.1')
compile('org.apache.commons:commons-compress:1.4.1')
compile('commons-digester:commons-digester:1.6')
compile('commons-fileupload:commons-fileupload:1.1')
compile('commons-io:commons-io:1.3.1')
compile('commons-lang:commons-lang:2.0')
compile('commons-logging:commons-logging:1.0.4')
compile('commons-net:commons-net:2.0')
compile('commons-pool:commons-pool:1.2')
compile('commons-validator:commons-validator:1.1.3')
compile('com.ibm.db2:jdbc3-driver:9.5')
compile('dom4j:dom4j:1.6.1')
compile('org.codehaus.groovy:groovy-all:1.5.7')
compile('org.apache.xmlgraphics:fop:0.94')
compile('com.google.guava:guava:13.0.1')
compile('org.codehaus.groovy:groovy-all:1.5.7')
compile('cz.svt:hibernate-svt:3.6.8.Final')
compile('com.itextpdf:itextpdf:5.0.2')
compile('joda-time:joda-time:2.1')
compile('javax.servlet:jstl:1.0.6')
compile('junit:junit:3.8.1')
compile('log4j:log4j:1.2.15')
compile('mail:mail:1.0.3')
compile('org.springframework:spring-core:1.2.9')
compile('org.springframework:spring-beans:1.2.9')
compile('org.springframework:spring-mock:1.2.9')
compile('taglibs:standard:1.0.6')
compile('struts:struts:1.2.4')
compile('struts:struts-el:1.2.4')
compile('strutstest:strutstest:2.1.3-1.2-2.3')
compile('velocity:velocity:1.4')
compile('org.apache.ant:ant:1.7.1')
compile('org.apache.tomcat:jsp-api:6.0.18')
compile('org.apache.tomcat:servlet-api:6.0.18')
compile('org.apache.tomcat:catalina:6.0.18')
compile('org.apache.tomcat:dbcp:6.0.18')
compile('org.easymock:easymock:3.0')
compile('cz.svt:easymock-propeq:1.3')
compile('org.testng:testng:5.11:jdk15')
testCompile('org.apache.ftpserver:ftplet-api:1.0.0')
testCompile('org.apache.ftpserver:ftpserver-core:1.0.0')
testCompile('org.apache.mina:mina-core:2.0.0-M4')
testRuntime('org.slf4j:slf4j-api:1.6.1')
testRuntime('org.slf4j:slf4j-log4j12:1.6.1')
testRuntime('org.uncommons:reportng:1.0')
testRuntime('asm:asm:3.0')
testRuntime('asm:asm-tree:3.0')
testRuntime('org.hibernate:hibernate-commons-annotations:3.2.0.Final')
testRuntime('org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final')
testRuntime('javax.transaction:jta:1.1')
testRuntime('antlr:antlr:2.7.6')
testRuntime("org.objenesis:objenesis:1.2")
testRuntime("oro:oro:2.0.7")
}
test {
useTestNG() {
suites 'src/test/resources/testng.xml'
listeners << 'org.uncommons.reportng.HTMLReporter'
listeners << 'org.testng.reporters.XMLReporter'
}
}
task reports {
dependsOn coberturaTestReport, javadoc
}
here is link to aspectj compiler output 1
I've been asked to implement some gRPC classes for a college course, and have run into some problems when generating the java classes from one source proto file.
Some background first: it's a fairly basic service, with a simple method that receives an id and returns a phone and an email. This is the proto file (BuscarData means FetchData, sorry for the leftover non translation!):
syntax = 'proto3';
option java_multiple_files=true;
option java_generic_services= true;
package uy.edu.um.sd20;
message DataRequest {
int32 id = 1;
}
message DataResponse {
string phone = 1;
string email = 2;
}
service DataRepo {
rpc BuscarData (DataRequest) returns (DataResponse);
}
The idea I had was to generate the classes with gradle plugins. My build.gradle:
plugins {
id 'java'
id "com.google.protobuf" version '0.8.8'
}
apply plugin: 'java'
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.11.4'
implementation 'io.grpc:grpc-netty-shaded:1.29.0'
implementation 'io.grpc:grpc-protobuf:1.29.0'
implementation 'io.grpc:grpc-stub:1.29.0'
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.11.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.29.0'
}
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
all().each { task ->
// Here you can configure the task
}
ofSourceSet('main')
}
}
From what I understood, everything's there: the grpc and protoc dependencies, and the plugin which enables protoc to compile grpc (protoc-gen-grpc), and where to deposit the generated files.
However, there are two problems:
the generated-sources are not marked as source or anything like that, meaning they cannot be imported from other classes
if I'm not mistaken, the generated-sources should generate a skeleton of DataRepoImpl so that I can add the code needed for BuscarData. However, it didn't. Or maybe I should create it, extending from DataRepo.java, but I couldn't test it, due to problem n°1.
I've added a screenshot of the project file structure:
img
As you can see, quite a lot (if not all) of the gradle settings are copy-pasted and scavenged from many different web-sites. I hope I was careful enough not to repeat any imports. There are similar questions, and I tried the solutions there, but no luck there. One example, with which I knew I had to include the gen grpc plugin: another SO question
Any tip regarding anything else is welcome! I'm new to stackoverflow question-asking, so I may have made mistakes regarding the question specificity or aim of the question.
Thanks!
Franri.
For 1), the plugin should put the generated files are the input for java compile tasks even if you do not explicitly add 'generated-sources/main/java' in the sourceSets configuration. Version 0.8.8 has been a while, you can try with newer versions. There might have been some minor fixes for things you may hit.
For 2), you did not add grpc plugin to each generateProto task. It should be
generateProtoTasks {
all().each { task ->
task.plugins { grpc{} }
}
ofSourceSet('main')
}
I'm working with lenskit and I receive this error
Exception in thread "main" java.lang.NoClassDefFoundError: javax/annotation/PreDestroy
at org.grouplens.grapht.LifecycleManager.registerComponent(LifecycleManager.java:52)
at org.grouplens.grapht.reflect.internal.ClassInstantiator.instantiate(ClassInstantiator.java:140)
at org.grouplens.grapht.Instantiators$ProviderInstantiator.instantiate(Instantiators.java:148)
at org.grouplens.grapht.Instantiators$MemoizingInstantiator.instantiate(Instantiators.java:184)
at org.lenskit.inject.NodeInstantiator$DefaultImpl.instantiate(NodeInstantiator.java:91)
at org.lenskit.inject.NodeInstantiator.apply(NodeInstantiator.java:70)
at org.lenskit.inject.NodeInstantiator$DefaultImpl.apply(NodeInstantiator.java:82)
at org.lenskit.inject.InstantiatingNodeProcessor.processNode(InstantiatingNodeProcessor.java:54)
at org.lenskit.inject.NodeProcessors.processNodes(NodeProcessors.java:92)
at org.lenskit.inject.RecommenderInstantiator.replaceShareableNodes(RecommenderInstantiator.java:113)
at org.lenskit.inject.RecommenderInstantiator.instantiate(RecommenderInstantiator.java:82)
at org.lenskit.LenskitRecommenderEngineBuilder.build(LenskitRecommenderEngineBuilder.java:144)
at org.lenskit.LenskitRecommenderEngineBuilder.build(LenskitRecommenderEngineBuilder.java:112)
at org.lenskit.cli.util.RecommenderLoader.loadEngine(RecommenderLoader.java:87)
at org.lenskit.cli.commands.GlobalRecommend.execute(GlobalRecommend.java:72)
at org.lenskit.cli.Main.main(Main.java:73)
Caused by: java.lang.ClassNotFoundException: javax.annotation.PreDestroy
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 16 more
It says that the annotation.PreDestroy class wasn't found. But in my external library there is a java.xml.ws.annotation library which includes the javax.annotation file.
The place that it was called is here.
/**
* Register a component with the lifecycle manager. The component will be torn down when the lifecycle manager
* is closed, using whatever teardown the lifecycle manager institutes.
*
* #param instance The component to register.
*/
public void registerComponent(Object instance) {
if (instance == null) {
return;
}
if (instance instanceof AutoCloseable) {
actions.add(new CloseAction((AutoCloseable) instance));
}
for (Method m: MethodUtils.getMethodsListWithAnnotation(instance.getClass(), PreDestroy.class)) {
actions.add(new PreDestroyAction(instance, m));
}
}
I'm currently using intellij, and I've tried importing the javax.annotation jar file but it didnt work. I also tried putting this code
dependencies {
compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.0'
compile group: 'org.jetbrains', name: 'annotations', version: '15.0'
}
but it still didn't work.
Looking at lenskit source on GitHub, it declares sourceCompatibility = '1.7' in build,gradle.
Downgrading JDK to 8 or 7 should do the trick and resolve the issue. since javax.annotations.PreDestroy seem to have been removed/updated in JDK 9 and later.
Beyond that, there may be other compatibility issues with a later JDK version due to the project's source compatibility mentioned above.
What I'm trying to accomplish is to group some common tasks and functions under a common.gradle file and reuse those tasks and methods in different projects by simply calling in the main build.gradle:
apply from: 'common.gradle'
I have this following method in the common.gradle file:
def readPackageNameFromManifest() {
def manifestParser = new com.android.builder.DefaultManifestParser()
return manifestParser.getPackage(android.sourceSets.main.manifest.srcFile)
}
But it keeps telling me :
> Could not compile script 'common.gradle'
unable to resolve class com.android.builder.DefaultManifestParser
# line 77, column 26.
def manifestParser = new com.android.builder.DefaultManifestParser()
It's not causing any problem when I move the method under build.gradle.
I might be missing a point. I would appreciate your thoughts. Thanks!
You must instruct gradle that your build script in common.gradle depends on android-gradle-plugin.
Add this to common.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}