java.lang.NoClassDefFoundError: com/google/common/base/Joiner - java

I'm hitting this error when run my package jar:
java.lang.NoClassDefFoundError: com/google/common/base/Joiner
I simply call: java -jar xxx.jar
I already added the dependency in my pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<scope>compile</scope>
</dependency>
I'm using IntelliJ editor. I have a unit test for the function which uses the Joiner class. It runs successfully from within IntelliJ.
I put my cursor on the Joiner and use "command + B" to search for the declaration of the Joiner class. It opens the Decompiled source code page, and on the heading it shows the path as: guava-18.0.jar/com/goog/common/base/Joiner
So everything looks correct.
Can anyone help me figure out why do I hit this error?

You could shade guava into your jar but it would result in a bigger jar file. But that way it would be included in your programm 100%. Read more about shading maven depencies into your jar file in the official maven-documentation: http://maven.apache.org/plugins/maven-shade-plugin/

Related

Method not found error in Castor-XML jar file

I have a simple maven project to do some SIF calls with MDM hub, and adding castor dependencies for this.
Maven dependency added:
org.codehaus.castor
castor-xml
1.4.1
This downloaded the castor-xml-1.4.1.jar file.
Right at the line calling sipClient.process(req) below exception is thrown
Exception in thread "main" java.lang.NoSuchMethodError: org.exolab.castor.xml.Marshaller.getResolver()Lorg/exolab/castor/xml/ClassDescriptorResolver;
at com.siperian.sif.message.CastorUtil.setMappingLoader(CastorUtil.java:470)
at com.siperian.sif.message.CastorUtil.beanToXmlString(CastorUtil.java:358)
at com.siperian.sif.message.CastorUtil.beanToXmlString(CastorUtil.java:323)
at com.siperian.sif.message.CastorUtil.beanToXmlString(CastorUtil.java:309)
at com.siperian.sif.message.CastorUtil.beanToXmlString(CastorUtil.java:295)
at com.siperian.sif.client.HttpSiperianClient._process(HttpSiperianClient.java:117)
at com.siperian.sif.client.SiperianClient.process(SiperianClient.java:179)
I can see the getResolver method and classDescriptorResolver in the jar file in Java Decompiler, Images
classResolverDescriptor
getResolver method
Same exception even for 1.3.2 dependency.
Should I download any extra dependencies.
Thanks
This specific error for 2 reasons:
1- You are missing the jar file that has this method (This might not be the issue you have, as you stated you can see it when looking at the decompiled jar)
2- You have 2 or more jars in your dependencies, and it is actually looking at the jar which does not have the method you need.
How you should approach this is as follows:
Go to your ide, and open your pom.xml file
Open the Dependency Heirarchy view and search for org.codehaus.castor or castor-xml and see how many different versions you have.
If you have more than 1, and some are included as part of another jar, you can use in your pom.xml to remove the versions which you dont want.
If you like command line you can probably do the above, using mvn dependency:tree
Hope this helps you in some way.
-- Edited --
Your code is using 1.3.2 dependency. How? You can download the castor-xml.1.3.2.jar and extract it and look into the Marshaller. You will see the method getResolver() does not take any parameters, and therefore you get NoMethodFound.
´´´
/**
* Returns the ClassDescriptorResolver for use during marshalling
*
* #return the ClassDescriptorResolver
* #see #setResolver
*/
public XMLClassDescriptorResolver getResolver() {
}
´´´
Therefore you need to find out in your dependency hierarchy, will one includes this 1.3.2 jar and exclude this jar from it.
An example of how to do exclude is in pom.xml:
<dependency>
<groupId>sample.group.which.has.castor.in.it</groupId>
<artifactId>artifactor.which.has.castor.in.it</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>org.codehaus.castor</groupId>
<artifactId>castor-xml</artifactId>
<version>1.3.2</version>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- add proper dependency also, as it is needed -->
<groupId>org.codehaus.castor</groupId>
<artifactId>castor-xml</artifactId>
<version>1.4.1</version>
</dependency>

Mockito cannot find defined class while running a test

I am having trouble with mockito dependencies.I wrote a sample test and when I ran it I got this exception
java.lang.NoClassDefFoundError: javassist/NotFoundException
at org.powermock.core.transformers.TestClassTransformerBuilder$RemovesTestMethodAnnotation.fromMethods(TestClassTransformerBuilder.java:62)
at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.createDefaultMockLoader(AbstractCommonTestSuiteChunkerImpl.java:126)
....
the full Exception can be viewed at : https://pastebin.com/xWqUX0Wc
and the
test code - https://pastebin.com/pbWLc27B
My dependencies are the following:
mockito-all-1.9.5.jar
powermock-api-mockito-1.6.3.jar
powermock-api-support-1.4.9.jar
powermock-core-2.0.4.jar
powermock-module-junit-1.7.4.jar
powermock-module-junit-common-1.7.4.jar
powermock-reflect-2.0.4.jar
powermock-test-utils-1.5.3.jar
Where can be the problem?I guess something is wrong with the version of the jars.What version of the jars would you suggest to use ?
Your are missing JavaAssist jar download below jars and add them into your projects or add them in maven pom.xml file.
<javaassist.version>3.20.0-GA</javaassist.version>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javaassist.version}</version>
<scope>compile</scope>
</dependency>

ClassNotFoundException in com.amazonaws.demos.polly.PollyDemo

I'm new to Maven and the AWS SDK. So I installed both and updated my Java SDK. Double checked all required path and classpath settings.
The AWS Polly manual (page 119 in the pdf) presents a demo code example, to test Polly.
Being in this for the very first time, I tried this example (pom.xml and PollyDemo.java). Calling Maven as written in the manual, I receive the ClassNotFoundException for PollyDemo (classpath to com.amazonaws.demos.polly package has been set).
With over 10 years Java experience I feel like a newbie.
Please help
you need to add aws-java-sdk-polly dependency into your pom.xml
file and update the project, you can find dependency below:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-polly</artifactId>
<version>1.11.77</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.soundlibs/jlayer -->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1-1</version>
</dependency>
for more you can refer below link :
http://docs.aws.amazon.com/de_de/polly/latest/dg/examples-java.html
http://docs.aws.amazon.com/polly/latest/dg/examples-java.html
The example can be run if: AWS credentials are created and setted (1), a new project is started by creating an empty directory (e.g. 'my-app'),
opening a shell in 'my-app' and running the command 'mvn archetype:generate -DgroupId=com.amazonaws.demos.polly -DartifactId=polly-java-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false' (2), and finally replace both the existing 'pom.xml' and hello world java file with the ones in the example (3).

No FileSystem for scheme:hdfs and Class org.apache.hadoop.DistributedFileSystem not found

I want to upload a file to HDFS. I compiled my code using following jars as dependencies:
hadoop-auth-2.6.1.jar,
hadoop-common-2.6.1.jar and
hadoop-hdfs-2.6.1.jar,
My code:
I compiled it with Ant. But, it gave me this error: No FileSystem for scheme:hdfs.
Then I changed the code and compiled again:
But now I got another error: Class org.apache.hdfs.DistributedFileSystem not found.
What's wrong? And what should I do?
DistributedFileSystem is part of hadoop-core.
To fix this problem, you need to include hadoop-core-1.2.1.jar also (Note: I am using Maven for building):
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
Overall, I am using following Maven dependencies:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
While getting Hadoop Filesystem object like below
FileSystem fs = FileSystem.get(hdfsUrl,configuration);
If you get following error :
"No FileSystem for scheme:hdfs"
You can resolve it by setting following 2 properties on configuration.
configuration.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
configuration.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");
Now, you may get new error like below :
java.lang.ClassNotFoundException: Class org.apache.hadoop.hdfs.DistributedFileSystem not found
Hadoop-common.jar uses Thread.currentThread.getContextClassLoader() and configuration.getClassLoader to load classes.
So, if you set your classLoader by using
Thread.currentThread.setContextClassLoader(yourClassLoader);
configuration.setClassLoader(yourClassLoader);
you will be able to load required class from other hadoop jars (e.g. hadoop-hdfs)
Let me know if you need more help. And don't forget to upvote if you find this bit useful.
I have the same problem, when I compile my Java code into a executable jar and run the compiled jar. Always some error "Not found" (e.g. in your case no FileSystem...), which means some hadoop jar is not included in the compilation.
The solution is add the correct dependencies in the Maven/Gradle or add (all) the jars.
In my case, the hdfs is from the class org.apache.hadoop.hdfs.DistributedFileSystem from the jar: hadoop-hdfs-client-3.2.1.jar.
The relevant jars which have been used can be found in the log file (if you successfully run the program and have the log file). In my example is the following:
You can simply add all the jars (from the installed hadoop folder). They should be in the folder of common/hdfs/ ... under the folder: hadoop 3.2.1/share/hadoop. There are possible other jars which are used but not shown in the log. To be safe, just include all the jars. You can run hdfs classpath in the terminal to find the location of all the jars.
Once all the jars have been added, in your java code, you may also need to set the hadoop configuration
Configuration hadoopConfiguration = new Configuration();
hadoopConfiguration.addResource(new Path(CoreSiteXMLStr));
hadoopConfiguration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");

Why do I get compilation error "org/codehaus/groovy/control/CompilationFailedException"?

I am trying to compile my JasperReports template using an Ant script and Java. I am getting this error:
jasper java.lang.NoClassDefFoundError:
org/codehaus/groovy/control/CompilationFailedException
There is nothing complex in the template, but I still can't compile.
You will have to set the language value in your template to Java. There are two ways you can do this:
If you are using iReport, select the root object in your Report Inspector (the one with the same name as your report). Then in the Properties window, select Java from the Languages drop-down.
If you are editing the raw mark-up in the JRXML file, remove language="groovy" from the file altogether.
Then try to recompile - you should be sorted. :)
If you are using Maven, you must add the groovy dependency in your pom.xml.
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.10</version>
</dependency>
In another case, you must add the library groovy in your lib folder (WEB-INF/lib)
Another solution is to copy groovy-all-{version}.jar from the groovy binary distribution into the application's.
If you are using TIBCOJaspersoftStudio:
Download latest groovy 2.4.* jar from https://groovy.apache.org/download.html
Unpack and get this file ./groovy-2.4.10/embeddable/groovy-all-2.4.10.jar
Put the jar in ./TIBCOJaspersoftStudio-6.3.1.final/plugins
Delete the old jar: ./TIBCOJaspersoftStudio-6.3.1.final/plugins/groovy-all_2.4.5.jar
Change the languge to java in JRXML (ex:- language="java") or add groovy*.jar to your project’s classpath.
Your are missing the a important library groovy in path.
case 1 : if you are using Maven add this dependency with compatible version in pom.xml
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.4</version>
</dependency>
Case 2 : Second way is add the compatible version of groovy jar in class path
Url to download groovy jar : http://groovy-lang.org/download.html

Categories