java.lang.reflect.InaccessibleObjectException: Unable to make protected native java.lang.Object java.lang.Object.clone() - maven

After migrating to Java 17 from Java 8 and while running the test cases , the test case is failed with
java.lang.reflect.InaccessibleObjectException: Unable to make protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException accessible: module java.base does not "opens java.lang" to unnamed module .
this fails while using Whitebox.invokeMethod in the test cases.
maven-version - 3.8.1
junit-jupiter-engine version - 5.8.2
Anything with respect to version change mismatch Java 17 will cause this kind of issue?

Related

Error when trying to run test in springboot

I try to run some tests but somehow spring gets confused and I get the error below that appears to be some clash between some beans that Spring tries to instantiate. Anyone any ideas?
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.resourceHandlerMapping(WebMvcAutoConfiguration.java:408)
The following method did not exist:
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.resourceHandlerMapping(Lorg/springframework/web/util/UrlPathHelper;Lorg/springframework/util/PathMatcher;Lorg/springframework/web/accept/ContentNegotiationManager;Lorg/springframework/format/support/FormattingConversionService;Lorg/springframework/web/servlet/resource/ResourceUrlProvider;)Lorg/springframework/web/servlet/HandlerMapping;
The method's class, org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration, is available from the following locations:
jar:file:/home/test/stuff/.m2/repository/org/springframework/spring-webmvc/5.3.13/spring-webmvc-5.3.13.jar!/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class
The class hierarchy was loaded from the following locations:
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: file:/home/test/stuff/.m2/repository/org/springframework/spring-webmvc/5.3.13/spring-webmvc-5.3.13.jar
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport: file:/home/test/stuff/.m2/repository/org/springframework/spring-webmvc/5.3.13/spring-webmvc-5.3.13.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
2021-12-16 19:46:21.564 ERROR [cc-task-allocation-backend,,,] 5484 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener#5ef5c734] to prepare test instance [eu.europa.ec.cc.taskallocation.services.RuleServiceUniformDistTest#61d4171d]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
Dependency management issue as the org.junit.jupiter dependencies were overwritten by a starter.

Modularity issues with javaagent in Java 9

We have a javaagent written in Java 8 that instruments application code using javassist. There is one simple ClassFileTransformer that instruments java.lang.Error class constructor to record an instantiated error. I am retransforming Error class because it is loaded much before my agent is loaded. The instrumetation code used links java.lang.Error class to classes in agent jar. As java.lang.Error is loaded by bootstrap class loader and that Error is linked to classes in agent jar, I am using -Xbootclasspath/a option to load agent classes as well using bootstrap class loader. This is working fine in Java 8.
Now to Java 9+: Now I am trying to run the same Java 8 agent in Java 9 JVM and running into modularity issues. I am still using -Xbootclasspath/a option because I still have to load the agent classes using bootstrap class loader. As with Java 9 modularity, my agent classes are loaded into unnamed module of bootstrap class loader. Though java.lang.Error class is also loaded by bootstrap class loader but it is part of java.base named module in Java 9 and I am running into below error because unnamed module is not accessible to named java.base module.
Java command line options used are -Xbootclasspath/a:"path-to-agent.jar" and -javaagent:path-to-agent.jar
javassist.CannotCompileException: [source error] no such class: com.sg.agent.ErrorRecorder
at javassist.CtBehavior.insertAfter(CtBehavior.java:877)
at javassist.CtBehavior.insertAfter(CtBehavior.java:792)
at com.sg.agent.ErrorClassTransformer.transform(ErrorClassTransformer.java:60)
at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:550)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:157)
at com.sg.agent.SGAgent.retransformError(SGAgent.java:362)
at com.sg.agent.SGAgent.premain(SGAgent.java:346)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:500)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:512)
Caused by: compile error: no such class: com.sg.agent.ErrorRecorder
at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:470)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:414)
at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:321)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:683)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
at javassist.compiler.Javac.compileStmnt(Javac.java:567)
at javassist.CtBehavior.insertAfterAdvice(CtBehavior.java:892)
at javassist.CtBehavior.insertAfter(CtBehavior.java:851)
... 15 more
So after some googling I found there are java command line options to overcome these issues. Accordingly, now I am using these below command line options
1) -Xbootclasspath/a:"path-to-agent.jar"
2) -javaagent:path-to-agent.jar
3) --module-path path-to-agent-jar-folder
4) --add-modules sg.agent
5) --add-modules java.base
6) --add-reads java.base=sg.agent
and agent jar file has below contents in it's manifest file.
Manifest-Version: 1.0
Premain-Class: com.sg.agent.SGAgent
Can-Retransform-Classes: true
Created-By: Srinivas
Automatic-Module-Name: sg.agent
Add-Exports: module/package
Add-Opens: module/package
Boot-Class-Path: C:/Test/SGAgent.jar
and now JVM is throwing below error. From the below stacktrace it can be seen that JVM has read sg.agent as a named module but still complaining no such class: com.sg.agent.ErrorRecorder though --add-reads option gives java.base access to sg.agent. And additionally, my jetty server is not starting now.
javassist.CannotCompileException: [source error] no such class: com.sg.agent.ErrorRecorder
at sg.agent/javassist.CtBehavior.insertAfter(CtBehavior.java:877)
at sg.agent/javassist.CtBehavior.insertAfter(CtBehavior.java:792)
at sg.agent/com.sg.agent.ErrorClassTransformer.transform(ErrorClassTransformer.java:60)
at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:550)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:157)
at sg.agent/com.sg.agent.SGAgent.retransformError(SGAgent.java:362)
at sg.agent/com.sg.agent.SGAgent.premain(SGAgent.java:346)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:500)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:512)
Caused by: compile error: no such class: com.sg.agent.ErrorRecorder
at sg.agent/javassist.compiler.MemberResolver.searchImports(MemberResolver.java:470)
at sg.agent/javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:414)
at sg.agent/javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:321)
at sg.agent/javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:683)
at sg.agent/javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)
at sg.agent/javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
at sg.agent/javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
at sg.agent/javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)
at sg.agent/javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
at sg.agent/javassist.compiler.Javac.compileStmnt(Javac.java:567)
at sg.agent/javassist.CtBehavior.insertAfterAdvice(CtBehavior.java:892)
at sg.agent/javassist.CtBehavior.insertAfter(CtBehavior.java:851)
... 15 more
Error: LinkageError occurred while loading main class org.eclipse.jetty.start.Main
java.lang.LinkageError: loader (instance of jdk/internal/loader/ClassLoaders$AppClassLoader): attempted duplicate class definition for name: "org/eclipse/jetty/start/Main"

Transitive dependency missing in development mode

In a simple REST service built with Quarkus, a ClassNotFoundException is thrown if the service is run in development mode. With the uber-jar, it works correctly.
How can I get the service working in development mode too?
The missing class belongs to a transitive dependency of a QR bill generator library, which is declared build.gradle:
dependencies {
...
implementation 'net.codecrete.qrbill:qrbill-generator:2.2.2'
...
}
The error occurs in development mode (gradle quarkusDev) only. It is triggered if a REST request would need to use the qrbill:qrbill-generator library:
2020-05-03 12:11:49,463 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-1) HTTP Request to /xxx/yyy failed, error id: 89c5b7f8-c762-4f7f-959b-67f16b2bc120-1: org.jboss.resteasy.spi.UnhandledException: java.lang.NoClassDefFoundError: io/nayuki/qrcodegen/QrCode$Ecc
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:216)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:515)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:259)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:160)
[several lines omitted]
Caused by: java.lang.NoClassDefFoundError: io/nayuki/qrcodegen/QrCode$Ecc
at net.codecrete.qrbill.generator.QRCode.draw(QRCode.java:49)
at net.codecrete.qrbill.generator.BillLayout.drawPaymentPart(BillLayout.java:131)
at net.codecrete.qrbill.generator.BillLayout.draw(BillLayout.java:94)
[several lines omitted]
Caused by: java.lang.ClassNotFoundException: io.nayuki.qrcodegen.QrCode$Ecc
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:341)
at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:294)
... 44 more
When analyzing the dependencies, I can see that io.nayuki:qrcodegen:1.6.0 is part of the runtimeClasspath but not part of the compileClasspath. This looks ok.
Could it be that Quarkus dev mode uses the wrong classpath?

Maven jars classes conflicts

We are using maven in our project, we are facing some issue with
jars
Issue is :
We have ss_css2 jar and cssparser jar in project. Those two are being
used in different functionality.
The problem is those two jars have same class with same package
com.steadystate.css.parser.CSSOMParser
Because of that we are getting conflicts. The place where it actually
needs to get CSSOMParser class from cssparser instead it is
resolving class in ss_css2 jar because of that am getting below
exception
com.steadystate.css.parser.CSSOMParser.<init>(Lorg/w3c/css/sac/Parser;)V
java.lang.NoSuchMethodError:
com.steadystate.css.parser.CSSOMParser.<init>(Lorg/w3c/css/sac/Parser;)V
at
com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.parseCSS(CSSStyleSheet.java:882)
at
com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.<init>(CSSStyleSheet.java:193)
at
com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.loadStylesheet(CSSStyleSheet.java:341)
at
com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLinkElement.getSheet(HTMLLinkElement.java:152)
at
com.gargoylesoftware.htmlunit.javascript.host.css.StyleSheetList.item(StyleSheetList.java:167)
at
com.gargoylesoftware.htmlunit.javascript.host.Window.getComputedStyle(Window.java:1687)
at
com.gargoylesoftware.htmlunit.html.DomNode.isDisplayed(DomNode.java:749)
at
com.gargoylesoftware.htmlunit.html.HtmlElement.isDisplayed(HtmlElement.java:1319)
at
org.openqa.selenium.htmlunit.HtmlUnitWebElement.isDisplayed(HtmlUnitWebElement.java:490)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement$1.call(
If we remove that ss_css2 jar other functionality getting failed,
getting below exception while doing text to pdf convertion
ERROR core.JobRunShell:211 - Job
com.scraper.ScheduledAgentLauncher.ScheduledAgent_DMSAgentProcessor_8
threw an unhandled Exception:
org.springframework.scheduling.quartz.JobMethodInvocationFailedException:
Invocation of method 'run' on target class [class
com.highradius.dms.scheduler.DMSAgentScheduledAction] failed; nested
exception is java.lang.NoClassDefFoundError:
com/steadystate/css/parser/SACParser at
org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:273)
at
org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
Caused by: java.lang.NoClassDefFoundError:
com/steadystate/css/parser/SACParser at
org.zefer.html.doc.css.d.<init>(Unknown Source) at
org.zefer.html.doc.css.c.<init>(Unknown Source) at
org.zefer.html.doc.css.c.<init>(Unknown Source)
Please suggest what is the best approach to solve this.
you may use maven-dependency-plugin and configure the include/exclude parameters to include or exclude classes.
See Unpacking specific artifacts
hope this helps.

Squeryl fails to reflect in debug mode only

I can't debug (simple run works fine) my unit tests. Squeryl fails with the following exception:
error while reflecting on metadata for (Some(private scala.Option
com.company.play.model.db.mapping.Position.orgUnit2id),Some(public scala.Option
com.company.play.model.db.mapping.Position.orgUnit2id()),None,Set(#org.squeryl.annotations.ColumnBase(optionType=class java.lang.Object, name=, length=-1, scale=-1, value=ORG_UNIT2ID))) of class com.company.play.model.db.mapping.Position
java.lang.RuntimeException: error while reflecting on metadata for (Some(private scala.Option com.company.play.model.db.mapping.Position.orgUnit2id),Some(public scala.Option com.company.play.model.db.mapping.Position.orgUnit2id()),None,Set(#org.squeryl.annotations.ColumnBase(optionType=class java.lang.Object, name=, length=-1, scale=-1, value=ORG_UNIT2ID))) of class com.company.play.model.db.mapping.Position
at org.squeryl.internals.PosoMetaData$$anonfun$4.apply(PosoMetaData.scala:115)
at org.squeryl.internals.PosoMetaData$$anonfun$4.apply(PosoMetaData.scala:80)
at org.squeryl.internals.PosoMetaData.<init>(PosoMetaData.scala:80)
at org.squeryl.View.<init>(View.scala:66)
at org.squeryl.Table.<init>(Table.scala:29)
at org.squeryl.Schema$class.table(Schema.scala:340)
at com.******.play.model.db.mapping.DBLibrary$.table(DBLibrary.scala:5)
...
Caused by: scala.MatchError: null
at org.squeryl.internals.FieldMetaData$$anon$1.build(FieldMetaData.scala:466)
at org.squeryl.internals.PosoMetaData$$anonfun$4.apply(PosoMetaData.scala:112)
... 22 more
I debug using IntelliJ IDEA latest version (13.1.4). Running both in IDEA & in console (sbt clean compile test) works fine.
Switching from jdk 1.8 to jdk 1.7 solved the issue.

Resources