program using reflection run as hadoop jar throws java.lang.NoClassDefFoundError - java

I have a base and sub class. The requirement is to invoke sub class' method using reflection. Both programs are below.
base.java
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
public class base {
public static void main(String[] args) {
URLClassLoader loader = null;
Class<?> cls;
try {
File file = new File("sub.jar" );
URL[] urls = { file.toURI().toURL() };
loader = new URLClassLoader(urls);
cls = loader.loadClass("sub");
base obj = (base) cls.newInstance();
obj.print();
}
catch(Exception e) {
System.out.println("Exception occured:" + e);
e.printStackTrace();
}
}
public void print() {
System.out.println("In Base class");
}
}
sub.java
public class sub extends base {
public void print() {
System.out.println("In subclass");
}
}
compile both in to jars.
javac base.java;
jar cvf base.jar base.class
javac sub.java;
jar cvf sub.jar sub.class
If I invoke the base.jar as "java -cp", it works fine
java -cp base.jar base
output: "In subclass"
But if I invoke it with "hadoop jar" command, I get
hadoop jar base.jar base
Exception in thread "main" java.lang.NoClassDefFoundError: base
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at base.main(base.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.ClassNotFoundException: base
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Any help is greatly appreciated.

I believe your issue is on line 15 where you try to case the loader as your base class. I believe the compiler doesn't know how to handle that. Additionally, I'm not sure why you're trying to run this program with Hadoop since it does not have any of the hadoop implementations or classes

The problem is solved by placing base.jar in hadoop classpath. Run "hadoop classpath" command which lists all classpath dirs. Place the jar in one of these directories.

Related

issue deserializing events in protobuf events in apache flink

I am reading events from kinesis in my flink app. the events are in protobuf format. if i use 'com.google.protobuf:protobuf-java:3.7.1' with in the flink app i've no issues. however if i change that to 'com.google.protobuf:protobuf-java:3.10.0' i get the above exception with stack trace
java.lang.IncompatibleClassChangeError: class com.google.protobuf.Descriptors$OneofDescriptor has interface com.google.protobuf.Descriptors$GenericDescriptor as super class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.privateGetPublicMethods(Class.java:2917)
at java.lang.Class.getMethods(Class.java:1615)
at org.apache.flink.api.java.typeutils.TypeExtractor.isValidPojoField(TypeExtractor.java:1786)
at org.apache.flink.api.java.typeutils.TypeExtractor.analyzePojo(TypeExtractor.java:1856)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateGetForClass(TypeExtractor.java:1746)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateGetForClass(TypeExtractor.java:1643)
at org.apache.flink.api.java.typeutils.TypeExtractor.createTypeInfoWithTypeHierarchy(TypeExtractor.java:921)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateCreateTypeInfo(TypeExtractor.java:781)
at org.apache.flink.api.java.typeutils.TypeExtractor.createTypeInfo(TypeExtractor.java:735)
at org.apache.flink.api.java.typeutils.TypeExtractor.createTypeInfo(TypeExtractor.java:731)
at org.apache.flink.api.common.typeinfo.TypeInformation.of(TypeInformation.java:211)
at org.apache.flink.api.java.typeutils.ListTypeInfo.<init>(ListTypeInfo.java:45)
at com.bagi.streaming.serialization.ProtoSchema.getProducedType(ProtoSchema.java:40)
at org.apache.flink.streaming.connectors.kinesis.serialization.KinesisDeserializationSchemaWrapper.getProducedType(KinesisDeserializationSchemaWrapper.java:57)
at org.apache.flink.streaming.connectors.kinesis.FlinkKinesisConsumer.getProducedType(FlinkKinesisConsumer.java:363)
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1456)
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1414)
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1396)
at com.bagi.streaming.StreamProcessor.getKinesisTrackingStream(StreamProcessor.java:101)
at com.bagi.streaming.StreamProcessor.getKinesisTrackingStream(StreamProcessor.java:110)
at com.bagi.streaming.StreamProcessor.consumeKinesis(StreamProcessor.java:117)
at com.bagi.streaming.StreamProcessor.main(StreamProcessor.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.flink.client.program.PackagedProgram.callMainMethod(PackagedProgram.java:529)
at org.apache.flink.client.program.PackagedProgram.invokeInteractiveModeForExecution(PackagedProgram.java:421)
at org.apache.flink.client.program.ClusterClient.run(ClusterClient.java:423)
at org.apache.flink.client.cli.CliFrontend.executeProgram(CliFrontend.java:813)
at org.apache.flink.client.cli.CliFrontend.runProgram(CliFrontend.java:287)
at org.apache.flink.client.cli.CliFrontend.run(CliFrontend.java:213)
at org.apache.flink.client.cli.CliFrontend.parseParameters(CliFrontend.java:1050)
at org.apache.flink.client.cli.CliFrontend.lambda$main$11(CliFrontend.java:1126)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1844)
at org.apache.flink.runtime.security.HadoopSecurityContext.runSecured(HadoopSecurityContext.java:41)
at org.apache.flink.client.cli.CliFrontend.main(CliFrontend.java:1126)
i am using flink#1.8.0 and 'com.twitter:chill-protobuf:0.9.3'. i am building flink app jar locally on my mac. i've tried using protoc at both 3.10.0 and 3.7.1 for protobuf-java at 3.10.0 in case that matters.
here is my deserializer
public class ProtoSchema implements DeserializationSchema<List<Event>> {
#Override
public List<Event> deserialize(byte[] message) throws IOException {
List<Event> events = new LinkedList<>();
InputStream inputStream = new ByteArrayInputStream(message);
while (true) {
Event event = Event.parseDelimitedFrom(inputStream);
if (event != null) {
events.add(event);
} else {
break;
}
}
return events;
}
#Override
public boolean isEndOfStream(List<Event> nextElement) {
return false;
}
#Override
public TypeInformation<List<Event>> getProducedType() {
return new ListTypeInfo<>(Event.class);
}
}
which i am plugging in by doing
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
Properties consumerConfig = new Properties();
consumerConfig.put(AWSConfigConstants.AWS_CREDENTIALS_PROVIDER, "AUTO");
consumerConfig.put(AWSConfigConstants.AWS_REGION, region);
consumerConfig.put(ConsumerConfigConstants.SHARD_GETRECORDS_INTERVAL_MILLIS, "300");
consumerConfig.put(ConsumerConfigConstants.SHARD_GETRECORDS_RETRIES, "10");
consumerConfig.put(ConsumerConfigConstants.SHARD_GETRECORDS_MAX, "5000");
consumerConfig.put(ConsumerConfigConstants.STREAM_INITIAL_POSITION, "LATEST");
env.addSource(new FlinkKinesisConsumer<>(name, new ProtoSchema(), consumerConfig)).name("KinesisSource");
env.getConfig().registerTypeWithKryoSerializer(Event.class, ProtobufSerializer.class);
Event.class is compiled from protobuf schema using protoc#3.10.0 and protobuf-java#3.10.0
As you said in comment from protobuf-java:3.9.0 there is binary incompatible change to lower versions (3.8-).
to class class Descriptors.OneofDescriptor added super-class Descriptors.GenericDescriptor,
which
A static field from a super-interface of a client class may hide a field (with the same name) inherited from new super-class and cause IncompatibleClassChangeError exception. More
So if you have on your classpath protobuf-java:3.9.0+ and also some lower version (3.8-) call this class you will got this error. (In my case it went from hadoop which has 2.5 protobuf-java version and my fat jar with 3.10)
Solution:
You need to shade one of the incompatible dependencies protobuf-java more how to shade depedency with gradle
Or use version 3.8 and lower as temporary shortsighted solution.

Security exception with sphinx4 jars

I created a project with sphinx & cloudgarden.But when i run the rpoject it gives an error from the sphinx part as below
Exception in thread "Thread-20" java.lang.SecurityException: class "edu.cmu.sphinx.util.props.ConfigurationManagerUtils"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:898)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:668)
at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at edu.cmu.sphinx.api.Context.<init>(Context.java:59)
at edu.cmu.sphinx.api.Context.<init>(Context.java:45)
at edu.cmu.sphinx.api.AbstractSpeechRecognizer.<init>(AbstractSpeechRecognizer.java:44)
at edu.cmu.sphinx.api.LiveSpeechRecognizer.<init>(LiveSpeechRecognizer.java:34)
at hbsn.HBSN.Sphinx(HBSN.java:275)
at hbsn.HBSN$2.run(HBSN.java:205)
and this is the code for sphinx
public static Configuration configuration;
public static LiveSpeechRecognizer recognize;
public static SpeechResult result;
public static String Sphinx_Result;
public static void Sphinx() throws InterruptedException{
try {
configuration = new edu.cmu.sphinx.api.Configuration();
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("file:C:/HBSN/Dictioanary-1000.dic");
configuration.setLanguageModelPath("file:C:/HBSN/Language Model-1000.lm");
configuration.setUseGrammar(false);
recognize = new LiveSpeechRecognizer(configuration);
recognize.startRecognition(true);
while ((result = recognize.getResult()) != null) {
Sphinx_Result = result.getHypothesis();
}
recognize.stopRecognition();
} catch (IOException e) {
main.Error("Error In Sphinx", " sphinx speech recognition unit not working");
}
}
used APIs are
sphinx4-core-1.0.0.jar
sphinx4-data-1.0.0.jar
How can i resolve this issue.
You are trying to use compromised jars. You can get latest jars from official website. They should have version 5prealpha, not 1.0.0.

Unable to instrument apache httpclient using javaagent for spring boot uber jar application

I'm trying to write a javaagent with Bytebuddy to intercept apache httpclient requests and I want to use this agent for spring boot application. The agent works fine when I start my test spring boot application from Idea (run the main method directly). However, when I package the application into a spring boot uber jar and run it using java -javaagent:myagent.jar -jar myapplication.jar,
it throws the following exception.
onError:org.apache.http.impl.client.AbstractHttpClient
java.lang.NoClassDefFoundError: org/apache/http/HttpHost
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at net.bytebuddy.description.method.MethodList$ForLoadedType.<init>(MethodList.java:106)
at net.bytebuddy.description.type.TypeDescription$ForLoadedType.getDeclaredMethods(TypeDescription.java:985)
at net.bytebuddy.implementation.MethodDelegation$MethodContainer$ForExplicitMethods.ofStatic(MethodDelegation.java:1037)
at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:247)
at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:226)
at com.yiji.dtrace.agent.httpclient4.interceptor.HttpClient4Interceptors$1.transform(HttpClient4Interceptors.java:48)
at net.bytebuddy.agent.builder.AgentBuilder$Transformer$Compound.transform(AgentBuilder.java:457)
at net.bytebuddy.agent.builder.AgentBuilder$Default$Transformation$Simple$Resolution.apply(AgentBuilder.java:2791)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:3081)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:170)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:170)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:170)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.yjf.common.net.HttpUtil.<init>(HttpUtil.java:118)
at com.yjf.common.net.HttpUtil.<init>(HttpUtil.java:81)
at com.yjf.common.net.HttpUtil.<clinit>(HttpUtil.java:78)
at com.daidai.dtrace.agent.test.Main.main(Main.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpHost
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)
... 60 more
and here is my agent related code.
public class DTraceAgent {
public static TypeDescription abstractHttpClientDescription() {
return new TypeDescription.Latent("org.apache.http.impl.client.AbstractHttpClient",
Modifier.PUBLIC|Modifier.ABSTRACT,
TypeDescription.OBJECT,
Arrays.asList(httpClientDescription()));
}
public static TypeDescription httpHostDescription() {
return new TypeDescription.Latent("org.apache.http.HttpHost",
Modifier.PUBLIC|Modifier.FINAL,
TypeDescription.OBJECT,
Arrays.asList(new TypeDescription.ForLoadedType(Cloneable.class),
new TypeDescription.ForLoadedType(Serializable.class)));
}
public static TypeDescription httpContextDescription() {
return new TypeDescription.Latent("org.apache.http.protocol.HttpContext",
getInterfaceModifiers(),
TypeDescription.OBJECT,
null);
}
public static TypeDescription httpRequestDescription() {
return new TypeDescription.Latent("org.apache.http.HttpRequest",
getInterfaceModifiers(),
httpMessageDescription(),
null);
}
public static void premain(String arguments, Instrumentation instrumentation) {
new AgentBuilder.Default()
//.withBinaryLocator(binaryLocatorFor(instrumentation))
.withListener(DebugListener.getListener())
.type(is(abstractHttpClientDescription()))
.transform(new AgentBuilder.Transformer() {
public DynamicType.Builder transform(DynamicType.Builder builder,
TypeDescription typeDescription) {
return builder.method(named("execute")
.and(takesArguments(httpHostDescription(), httpRequestDescription(), httpContextDescription()))
.and(returns(named("org.apache.http.HttpResponse"))))
.intercept(MethodDelegation.to(HttpClientInterceptor4dot3Plus.class));
}
}).installOn(instrumentation);
}
}
public class HttpClientInterceptor4dot3Plus {
public static CloseableHttpResponse doExecute(
#SuperCall Callable<CloseableHttpResponse> client, #Argument(1)HttpRequest request
) throws Exception {
StringBuilder builder = new StringBuilder(1024);
if (request != null && request.getRequestLine() != null) {
RequestLine requestLine = request.getRequestLine();
builder.append(requestLine.getMethod()).append(" ").append(requestLine.getUri());
}
try (TraceScope scope = Trace.startSpanForEntry(builder.toString())) {
Trace.spanType(Span.SPAN_TYPE_HTTP);
try {
return client.call();
} catch (Exception e) {
Trace.exception(e);
throw e;
}
}
}
}
public class DebugListener {
public static AgentBuilder.Listener getListener() {
return new AgentBuilder.Listener() {
#Override
public void onTransformation(TypeDescription typeDescription, DynamicType dynamicType) {
System.err.println("onTransformation:" + typeDescription.getCanonicalName());
try {
dynamicType.saveIn(new File("generated_classes"));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onIgnored(TypeDescription typeDescription) {
//System.err.println("onIgored:" + typeDescription.getCanonicalName());
}
#Override
public void onError(String typeName, Throwable throwable) {
System.err.println("onError:" + typeName);
throwable.printStackTrace();
}
#Override
public void onComplete(String typeName) {
//System.err.println("onComplete:" + typeName);
}
};
}
}
I think this problem is caused by the way spring boot uber jar bootstraps an application. Spring boot provides a dedicated class loader named LaunchedURLClassLoader to load application related classes from the uber jar, while javaagent jar is loaded by default system classloader (if my understanding is correct). So the apache httpclient lib (included in the uber jar) is not visible to the system classloader.
I tried to provider a BinaryLocator to the AgentBuilder, but it didn't work. Maybe the BinaryLocator was not correctly constructed. Anyway, a proper BinaryLocator maybe a possible solution.
Thanks a lot for any solutions or suggestions.
Other infomation may be helpful:
spring-boot version 1.3.1.RELEASE
byte-buddy 0.7.7, packaged into the agent using maven-assembly-plugin's jar-with-dependencies descriptorRef
apache httpclient 4.3.2
I solved this problem through two steps:
Use Spring Boot's dedicated ClassLoader to deligate to an unloaded type:
public static void premain(String arguments, Instrumentation instrumentation) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ClassFileLocator.Compound compound = new ClassFileLocator.Compound(ClassFileLocator.ForClassLoader.of(classLoader), ClassFileLocator.ForClassLoader.ofClassPath());
TypeDescription delegator = TypePool.Default.of(compound).describe(delegatorClass).resolve();
new AgentBuilder.Default()
//.withBinaryLocator(binaryLocatorFor(instrumentation))
.withListener(DebugListener.getListener())
.type(is(abstractHttpClientDescription()))
.transform(new AgentBuilder.Transformer() {
#Override
public DynamicType.Builder transform(DynamicType.Builder builder,
TypeDescription typeDescription) {
return builder.method(named("execute")
.and(takesArguments(httpHostDescription(), httpRequestDescription(), httpContextDescription()))
.and(returns(named("org.apache.http.HttpResponse"))))
.intercept(MethodDelegation.to(delegator));
}
}).installOn(instrumentation);
}
Package the javaagent jar into Spring Boot's uber-jar such that the delegator class can reference classes related to the intecepted classes.
This issue is discussed in greater detail in Byte Buddy's issue tracker.

Custom Classloader And Java Agent

I want to modifly the class in app which is running. So I use java agent to do this. My java code like this:
/**
*
* #param args
* #param inst
*/
public static void agentmain(String args, Instrumentation inst) {
try {
inst.addTransformer(new GameClassFileTransformer(), true);
parseFiles();
List<Class<?>> retransformClasses = new ArrayList<>();
for (JarClassInfo jarClassInfo : GameAgentMain.bugFiles.values()) {
retransformClasses.add(Class.forName(jarClassInfo.getClassName()));
}
Class<?>[] classes = new Class[retransformClasses.size()];
retransformClasses.toArray(classes);
inst.retransformClasses(classes);
} catch (Exception e) {
LogUtil.error(e);
}
}
The JarClassInfo is a class about the class I want to reload.There hava two fields in JarClassInfo ,first is className,second is bytecode(java type is byte[]),The classname is which class want to reload.But the class is loaded by
custom classLoad
.When the app run at :
retransformClasses.add(Class.forName(jarClassInfo.getClassName()));
That is a exception be thrown:
Exception in thread "Attach Listener" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:382)
at sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain(InstrumentationImpl.java:407)
Caused by: java.lang.ClassFormatError: Incompatible magic value 2329931675 in class file com/jingqi/game/module/chat/command/SendChatMsgCMD
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$1(URLClassLoader.java:409)
at java.net.URLClassLoader$2.run(URLClassLoader.java:361)
at java.net.URLClassLoader$2.run(URLClassLoader.java:1)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.jingqi.game.agent.GameAgentMain.agentmain(GameAgentMain.java:60)
... 6 more
My custom classloader(EncrypClassLoader) is used to decode my class.
So the methd call stack indicate sun.misc.Launcher$AppClassLoader to load SendChatMsgCMD .The SendChatMsgCMD is not a normal class file(because I encrypt this class file).
So ,what method can be solved 。
Your custom classloader isn't doing what it's supposed to do: decrypt classes.
That's why you're​ getting​ the java.lang.ClassFormatError about the invalid magic value.
EDIT: you need to also realise that Class.forName() will load the class referred by the classname argument using the current class's classloader.

How to lookup EJB remoting from a EJB application into a java application?

I'm working on a tow projects , EJB project (where i have all my EJB Remote) and the other project is a simple java application with main method. but when i try to lookup an EJB statless and remote via JNDI , it doesn't work.
Really i used many way but no good results.
this is the code and the consol error .
1) My Ejb project when i clean and install it , it gives me a jar file.
2) I use Jboss EAP6.
3) i have a jndi.properties insid of src/main/ressouces where i have this :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url = localhost:1099
PersonalFactory.java (EJB of the EJB project)
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.lazrak.common.logging.CommonLogger;
import com.lazrak.remotes.IPersonFactoryRemote;
#Remote(IPersonFactoryRemote.class)
#Stateless
public class PersonFactory implements IPersonFactoryRemote
#Override
public void findMe() {
System.out.println("I'am inside of the method findMe");
}
MyMainClass.java (where i have the main method and it is in the second java project)
All import ...
public class MyMainClass {
public static void main(String[] args) throws Exception {
System.out.println("Main start ");
invokeStatelessBean();
}
private static void invokeStatelessBean(){
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
InitialContext context = null;
try {
context = new InitialContext(props);
} catch (NamingException e) {
System.out.println("Faild to add properties to InitialContext"+e.getMessage());
}
try {
final IPersonFactoryRemote statelessRemoteCalculator = (IPersonFactoryRemote) context
.lookup("ejb:"+moduleName+"/"+PersonFactory.class.getSimpleName();+"/"+IPersonFactoryRemote.class.getName());
} catch (NamingException e) {
System.out.println("EJB Remot doesn't been fined\n"+e.getMessage());
}
}
}
when i run my main application , it gives me that :
Main start
enter 2014-09-26 15:57:26,098 DEBUG org.jboss.logging - InitialContextFactory.java:64 - Logging Provider: org.jboss.logging.Log4jLoggerProvider
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/remoting3/spi/ConnectionProviderFactory
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:99)
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:99)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at RemoteEJBClient.invokeStatelessBean(RemoteEJBClient.java:42)
at RemoteEJBClient.main(RemoteEJBClient.java:18)
Caused by: java.lang.ClassNotFoundException:
org.jboss.remoting3.spi.ConnectionProviderFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 11 more
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
Those properties are used for connecting to an older jboss version.. For eap 6, you have to use remoting instead of jnp.
Here´s a tutorial that will show you how to connect
http://www.mastertheboss.com/jboss-server/jboss-as-7/jboss-as-7-remote-ejb-client-tutorial

Categories

Resources