Exception in thread "main" java.lang.NoClassDefFoundError: org/reactivestreams/Publisher? - java

While creating a hello world program I got this exception. Here is the code:
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* Created by veneet on 30/04/17.
*/
public class MainApp {
public static void main(String[] args) {
// This is where the exception occurs.
Observable<String> observable = Observable.create(e -> {
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onComplete();
});
Observer<String> observer = new Observer<String>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(String s) {
System.out.println(s);
}
#Override
public void onError(Throwable e) {
System.err.println(e.getMessage());
}
#Override
public void onComplete() {
}
};
observable.subscribeOn(Schedulers.io());
observable.subscribe(observer);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The build.gradle dependencies are like this:
dependencies {
compile "io.reactivex.rxjava2:rxjava:2.1.0"
// https://mvnrepository.com/artifact/org.reactivestreams/reactive-streams
compile group: 'org.reactivestreams', name: 'reactive-streams', version: '1.0.0.final'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
The complete stacktrace is like this(I think the first line is totally unrelated, but putting it to ensure):
objc[3423]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x10b6dc4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10d0194e0). One of the two will be used. Which one is undefined.
Exception in thread "main" java.lang.NoClassDefFoundError: org/reactivestreams/Publisher
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at MainApp.main(MainApp.java:11)
Caused by: java.lang.ClassNotFoundException: org.reactivestreams.Publisher
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)
... 13 more

From the comments:
you don't have to include a dependency on Reactive-Streams because RxJava has a compile-time dependency on it already. Otherwise, the right version is:
compile 'org.reactivestreams:reactive-streams:1.0.0'
and for the Test Compatibility Kit:
testCompile 'org.reactivestreams:reactive-streams-tck:1.0.0'
The .final was a release mistake I guess.

Related

How to gradle matrix-toolkits-java (MTJ) without error?

I would like to use the MTJ library. But why do I get a ClassNotFoundException? How can I implement my build and code the right way?
First the error message, then the code example:
Error (gradle build; java -jar build/libs/tmp.jar):
Exception in thread "main" java.lang.NoClassDefFoundError: no/uib /cipr/matrix/DenseMatrix
at de.piphi.Main.readMatrix(Main.java:7)
at de.piphi.Main.main(Main.java:12)
Caused by: java.lang.ClassNotFoundException: no.uib.cipr.matrix.DenseMatrix
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
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)
gradle.build:
apply plugin: 'java'
repositories{
jcenter()
mavenCentral()
}
dependencies {
compile group: 'com.googlecode.matrix-toolkits-java', name: 'mtj', version: '1.0.2'
}
jar {
manifest {
attributes 'Main-Class': 'de.piphi.Main'
}
}
src/main/java/de/piphi/Main.cpp:
package de.piphi;
import no.uib.cipr.matrix.*;
public class Main {
static void readMatrix(){
Matrix m = new DenseMatrix(2,2);
System.out.println(m);
}
public static void main(String[] args) {
readMatrix();
}
}
I know Is there any example how to use Matrix Toolkit Java (MTJ)?, but the wiki page mentioned does not exist anymore.
I had to add the plugin applications to build.gradle. In Main.cpp the import of import no.uib.cipr.matrix.*; must be
import no.uib.cipr.matrix.DenseMatrix;
I changed 'Matrix m = new DenseMatrix(2,2);' to
DenseMatrix m = new DenseMatrix(2,2);

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.

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

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.

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.

ClassNotFoundException when accessing Processing from Eclipse

I'm trying to run Processing from Eclipse but whenever I run the application as a Java Application I get java.lang.ClassNotFoundException.
Here's my full code:
import processing.core.PApplet;
public class App extends PApplet {
public void setup() {
size(200,200);
background(0);
}
public void draw() {
stroke(255);
if (mousePressed) {
line(mouseX,mouseY,pmouseX,pmouseY);
}
}
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "App" });
}
}
Exception:
java.lang.ClassNotFoundException: App
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at processing.core.PApplet.main(PApplet.java:6522)
at com.WordClouds.App.main(App.java:17)
I had the same problem with the class name correctly set and also the package. This solved my issue:
PApplet.main(new String[] { "--present", MyProcessingSketch.class.getName() });
You are probably doing this tuturial : http://www.learningprocessing.com/tutorials/processing-in-eclipse/
You probably overlooked this :
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "MyProcessingSketch" });
}
Note that the String “MyProcessingSketch” must match the name of your class (and if it is in a package, should include the package, i.e. packagename.MyProcessingSketch).

Categories

Resources