Eclipse/Grails - Tests don't run - ClassNotFoundException [duplicate] - eclipse

While running junit test in eclipse I am getting this Exception:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
I've added junit.jar library file.
I've tried different versions of junit.jar: 4.4, 4.8, etc.
How do I fix this Exception?

Add hamcrest-all-X.X.jar to your classpath.
Latest version as of Feb 2015 is 1.3:
http://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-all-1.3.jar&can=2&q=

According to the JUnit GitHub team website (https://github.com/junit-team/junit/wiki/Download-and-Install), junit.jar and hamcrest-core.jar are both needed in the classpath when using JUnit 4.11.
Here is the Maven dependency block for including junit and hamcrest.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
<!-- Needed by junit -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

A few steps you have to follow:
Right click on the project.
Choose Build Path Then from its menu choose Add Libraries.
Choose JUnit then click Next.
Choose JUnit4 then Finish.

Works for me: IntelliJ IDEA 13.1.1, JUnit4, Java 6
I changed the file in project path: [PROJECT_NAME].iml
Replaced:
<library>
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
By:
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-library-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
So the final .iml file is:
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-library-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
P.S.: save the file and don't let to IntelliJ Idea reload it. Just once.

You need junit-dep.jar because the junit.jar has a copy of old Hamcrest classes.

Just in case there's anyone here using netbeans and has the same problem, all you have to do is
Right click on TestLibraries
Click on Add Library
Select JUnit and click add library
Repeat the process but this time click on Hamcrest and the click add library
This should solve the problem

This problem is because of your classpath miss hamcrest-core-1.3.jar. To resolve this add hamcrest-core-1.3.jar as you add junit-4.XX.jar into your classpath.
At first, I encounter this problem too, but after I refer to the official site and add hamcrest-core-1.3.jar into classpath with command line, it works properly finally.
javac -d ../../../../bin/ -cp ~/libs/junit-4.12.jar:/home/limxtop/projects/algorithms/bin MaxHeapTest.java
java -cp ../../../../bin/:/home/limxtop/libs/junit-4.12.jar:/home/limxtop/libs/hamcrest-core-1.3.jar org.junit.runner.JUnitCore com.limxtop.heap.MaxHeapTest

You need to add the hamcrest-core JAR to the classpath as described here: https://github.com/junit-team/junit4/wiki/Download-and-Install

As a general rule, always make sure hamcrest is before any other testing libraries on the classpath, as many such libraries include hamcrest classes and may therefore conflict with the hamcrest version you're using. This will resolve most problems of the type you're describing.

the simplest way of solving the problem to begin with is copying latest version of hamcrest-code.jar into your CLASSPATH that is the file you store other .jar files needed for compilation and running of your application.
that could be e.g.: C:/ant/lib

It sounds like a classpath issue, so there are a few different ways to go about it. Where does org/hamcret/SelfDescribing come from? Is that your class or in a different jar?
Try going to your project Build Path and on the Libraries tab, add a Library. You should be able to choose JUnit to your project. This is a little bit different than just having the JUnit jar file In your project.
In your Run Configuration for the JUnit test, check the Classpath. You could probably fix this by adding making sure your Classpath can see that SelfDescribing class there. The Run option in Eclipse has a different set of options for the JUnit options.

If this problem arise in a RCP project it can be because JUnit has been explicitly imported.
Check the editor for your plugin.xml under Dependencies tab, remove the org.junit from the Imported Packages and add org.junit to the Required Plug-ins.

The problem is when you set up eclipse to point to JRE instead of JDK. JRE has junit4.jar in the lib/ext folder, but not hamcrest.jar :) So the solution is to check installed JREs in Eclipse, remove the existing one and create a new one pointing to your JDK.

This happens when you run Ant via command line. The implicit user dependencies are added in the classpath at the end and take precedence over the project-added classpath. Run Ant with -nouserlib flag. The implicit dependencies would be excluded from the classpath.

There is a better answer to solve this problem.
add dependency
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

The hamcrest-core-1.3.jar available on maven repository is deprecated.
Download working hamcrest-core-1.3.jar from official Junit4 github link .
If you want to download from maven repository, use latest hamcrest-XX.jar.
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>

I had the same problem, the solution is to add in build path/plugin the jar org.hamcrest.core_1xx, you can find it in eclipse/plugins.

A few steps you have to follow:
Right click on the project.
Choose Build Path & then from its menu choose Add Libraries.
Choose JUnit then click Next.
Choose JUnit4 then Finish.
This works for me...

"java.lang.SecurityException: class" org.hamcrest.Matchers "'s signer information does not match signer information of other classes in the same package"
Do it:
Right-click on your package
click on Build Path -> Configure Build Path
Click on the Libraries tab
Remove JUnit
Apply and close
Ready.

Try adding the jar files manually or try with force update with the latest hamcrest.jar

Related

Hibernate JPA model class generation in Maven project

In my pom.xml, I added this dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
</dependency>
<!-- Annotation processor that raising compilation errors whenever constraint
annotations are incorrectly used. -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<scope>provided</scope>
</dependency>
When I compile and install the project by executing "mvn clean install" at terminal, the model classes are generated in this directory:
target/generated-sources/annotations/com/myproject/ne/model/
Then if I import this Maven project from Eclipse, it works all fine without complaining the model classes automatically generated.
However, if I don't execute "mvn clean install" at a terminal to generate the model classes and directly import the clean project, Eclipse doesn't generate the model classes, and therefore generates compilation errors.
What's needed to use the hibernate-jpamodelgen to automatically when a clean maven project is imported and compiled?
You can achieve it by enabling it with annotation processing.
In eclipse right click on project --> properties --> Java Compiler --> Annotation Processing --> Factory Path enable it.
Now populate the .factorypath file at the root of your project with the following contents:
<factorypath>
<factorypathentry kind="PLUGIN" id="org.eclipse.jst.ws.annotations.core" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hibernate/hibernate-jpamodelgen/1.0.0.Final/hibernate-jpamodelgen-1.0.0.Final.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.0.Final/hibernate-jpa-2.0-api-1.0.0.Final.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
Refresh the project in Eclipse
Follow #Tanvi B answer, and 1 more step:
project --> properties --> Java Compiler --> Annotation Processing:
Enable project specific settings (check all). And at Generated source directory, you have to define target/generated-sources

BIRT 4.5 and Tomcat 8 compatibility issue [duplicate]

This is what I got on the browser screen when I try to run the JSP file.
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
And the jasper exception is thrown on the Tomcat window when I ran it.
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
That method was introduced in JSP 2.1. There are 3 causes for this problem:
You're using an too old version of the JSP container (you need for example at least Tomcat 6.0).
You've declared the wrong Servlet version in web.xml. JSP 2.1 goes hand in hand with Servlet 2.5, so your web.xml should at least be declared as per the Servlet 2.5 spec (you still need a Servlet 2.5 / JSP 2.1 capable servletcontainer for that).
You've duplicated older versioned appserver-specific libraries into webapp's /WEB-INF/lib, like servlet-api.jar, jsp-api.jar and so on. You should never do that. It will only result in classloading collisions. Get rid of them in your webproject and leave/untouch them there in the appserver.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
also fixes the issues
The method getJspApplicationContext(ServletContext) is undefined for
the type JspFactory
This can also happen when your project requires a reference to a server runtime:
Right click on your project in Eclipse's "Project Explorer"
Choose "Build Path | Configure Build Path"
Click on the "Libraries" tab
Click "Add Library"
Select "Server Runtime" and click "Next"
Choose "Apache Tomcat 7", or whatever your server runtime version should be.
Note: This error can also happen for ANT builds, and for that case, you need to reference a target runtime in your ANT build.xml file... The following references a local tomcat installation, and uses its "lib" folders when doing the compile:
<project name="tomcat-demo" default="compile" basedir=".">
<property name="tomcat-home" value="/path/to/your/tomcat/apache-tomcat-7" />
<path id="project-classpath">
<fileset dir="WebContent/WEB-INF/lib" includes="*.jar" />
<fileset dir="${tomcat-home}/bin" includes="*.jar" />
<fileset dir="${tomcat-home}/common/lib" includes="*.jar" />
<fileset dir="${tomcat-home}/server/lib" includes="*.jar" />
</path>
...
</project>
Snagged from this url:
For Maven, I also had to add the jsp-api dependency as a provided jar like this: (Some other dependency was pulling it in, I could never figure out which one, but this fixed it up)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

Prevent IvyDE from resolving specific build time dependencies

Its maybe that I'm trying to misuse Ivy, but if I am then I definitely don't understand 'configurations'
I have a dependency I want to access only when running my build script under Jenkins. In my eclipse workspace I have no need for the dependency. Is it possible to achieve this?
For example if I wanted to pull ant-contrib in at build time I have tried setting up my configuration file as follows:
<configurations defaultconf="compile">
<conf name="compile" visibility="public" />
<conf name="build" visibility="public" extends="compile" />
</configurations>
<dependencies>
<dependency org="junit" name="junit" rev="4.8.1" conf="compile->default" />
<dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" conf="build->*" />
</dependencies>
With this example I always end up with ant contrib and ant on my build path in eclipse which isn't what I wanted. The resolve ant task allows me to resolve a specific configuration so I assumed that IvyDE would only resolve the default one. What am I missing?
Thanks,
Dan.
When adding the ivy file using IvyDE it is possible to select the configurations that should be resolved. This way only compile can be selected and only junit would be resolved.
It seems that to change this you need to remove and re-add the ivy dependency, I can't find any settings to change the configurations without doing that.

Building with Lombok's #Slf4j and Eclipse: Cannot find symbol log

I have the lombok plugin in Eclipse and enabled annotation processing in Eclipse under java compiler, but still it is unable to recognize the log statements when I use #Slf4j annotation.
Do we have to make any other settings?
You also have to install Lombok into Eclipse.
See also this answer on how to do that or check if Lombok is installed correctly.
Full Disclosure: I am one of the Project Lombok developers.
I also faced the similar issue on log and #Slf4j on my STS environment. To resolve this, here is what I did on spring tool suite (sts-4.4.0.RELEASE) and lombok-1.18.10.jar (current latest version available in mavenrepository).
If having maven project, ensure lombok dependency added to it. Else you need manually add the jar to your project classpath.
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
Clean build the maven application. This will download lombok jar in your .m2 location by default from maven repository.
The path would be org\projectlombok\lombok\1.18.10\
Now open command prompt and navigate to the lombok path and execute command java -jar lombok-1.18.10.jar
C:\xxx\xxx\org\projectlombok\lombok\1.18.10>java -jar lombok-1.18.10.jar
Opens up lombok dialog box. If see message Can't find IDE
Click Specify location...
Provide the path to your STS root location
My case it is
C:\apps\sts-4.4.0.RELEASE\SpringToolSuite.exe
Install/Update
Install successful
Click Quit Installer
Now in explorer navigate to your STS root path.
C:\apps\sts-4.4.0.RELEASE\
We see lombok.jar placed in the sts root path
Now edit in notepad SpringToolSuite4.ini file
We see following appended at the end
-javaagent:C:\apps\sts-4.4.0.RELEASE\lombok.jar
Start STS using SpringToolSuite4.exe
Clean, rebuild your project.
So like others, i also faced this issue. Below is what I did.
Installed lombok.jar like explained here.
Tried restarting eclipse. (Did not work)
Tried refreshing gradle project. (Did not work)
tried what Hervian suggested in his answer here. (Did not work)
Closed the projects, deleted from workspace and then re-imported. Bam!! Worked.
I got the same error even after Lombok was installed. For me the solution was to add another lombok annotation (i used #Data) to my class after which the eclipse errors went away. Perhaps this force refreshed some cache.
Of course, I simply deleted the #Data annotation afterwards.
this got the fix to me by adding the slf4j dependency, Lombok can identify the slf4j but does not get the download, this is true for java project if you are using spring boot then slf4j comes by default.
here are my dependencies
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>

TestNG test cases are not compiling when i do project -> clean

I am using Spring, TestNG, Eclipse, Maven, Mac OS
Scenario is
I am doing some changes in my test cases ( TestNG test cases ),
Then i do Project -> Clean in eclipse
Now i am running the test file, but the changes not updated.
I suspects that the test files are not compiled.
I can run the server and i can run my webservices
But if i run maven test ( mvn test ), the entire code is compiling including test cases.
So to run the test cases i am running mvn test command only.
Not able to run through eclipse.
Eclipse config:
Auto build on
In Java build path the test package is included
If you need any more info i'll provide.
org.testng.TestNGException:
Cannot find class in classpath: com.***.***.model.***.case.CaseModelImplTest
at org.testng.xml.XmlClass.loadClass(XmlClass.java:76)
at org.testng.xml.XmlClass.init(XmlClass.java:68)
at org.testng.xml.XmlClass.<init>(XmlClass.java:54)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:512)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:179)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:788)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1343)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2755)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:198)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
at org.testng.xml.Parser.parse(Parser.java:170)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:304)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:86)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:199)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:170)
It seems , you are not included your Test case class into the build path. Right click on your Project folder, go to the project properties->src
include your Testcase src folder by checking it.
Now it should run.
Open the Problems view. In Eclipse go to Window->Show View->Problems. It will list out all the failures while compiling the code.
Are the test cases being compiled, but to the wrong directory? They need to be compiled to test-classes not classes. In your build path, you need to set your Output folder to be
PROJECT/target/test-classes
This is true for all test resources (including src/test/java & src/test/resources).
Edit your project properties in Eclipse, select Java Build Path, and then in the Source tab, you'll see all of your source directories. Each one has an output folder. This output folder needs to be as above.
Go to Project --> Clean
That worked for me with similar problem.
Please check your testNG.xml file for correct test because I got the same error and it was because the wrong test name was entered.
is your test folder added as a source folder in Eclipse? If it isn't it won't be included in the build.
I solved this by:
Changing the build path / deleting some of the wrong ones
Cleaning the project
I think some time we get this error if your test class is in default package and in Testng.xml, you specify as below -
<suite name="Suite" parallel="none">
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
<test name="Test">
<classes>
<class name=".Test1" />
</classes>
</test> <!-- Test -->
Here in above, I moved my testclass from default package to some package and also updated testng.xml as below - and everything started working.
<suite name="Suite" parallel="none">
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
<test name="Test">
<classes>
<class name="com.sigma.rest.api.Test1" />
</classes>
</test> <!-- Test -->
I hope this helps you... if issue still exists, try above options.
thanks!
Check if your project artifacts like excel, property files, xml files etc are open in text editors etc. ( if yes please close them and do rebuild )
When you clean and rebuild make sure all project dependent files are closed and you do build.