Class not found when using JAX-RS with Eclipse and Glassfish - eclipse

I am using Eclipse Luna (versions 4.4.2) and Glassfish 4 to build a REST web-app using JAX-RS.
The following piece of code, which is just a very simple post to the REST api that was previously working just fine, has started throw a very strange error:
#POST
public Response addToWatchlist(WatchlistEntry watchlistentry)
{
return Response.ok(watchlist_service.addToWatchlist(watchlistentry).toString()).build();
}
The error is below:
Warning: StandardWrapperValve[Jersey Web Application]: Servlet.service() for servlet Jersey Web Application threw exception
java.lang.ClassNotFoundException: javax.xml.parsers.ParserConfigurationException not found by org.eclipse.persistence.moxy
All I have been able to find out about this is this webpage:
https://java.net/jira/browse/JERSEY-2888
One of the comments says this has been fixed in EclipseLink 2.6.1 and Jersey 2.19.
I am pretty new to using MAVEN and eclipse - assuming the above is correct, how do I get my Maven Project in Eclipse to update the Jersey version? Also how do I update the EclipseLink version? Their site only seems to have version 2.6.0 available: http://www.eclipse.org/eclipselink/#download
I assume all this can be done through eclipse itself?
All help appreciated!
EDIT 1: It seems like eclipselink 2.6.1 has been released on Oct 15th 2015, as you can see here: http://www.eclipse.org/eclipselink/releases/
However, as you can see here, it doesn't seem to have been incorporated into eclipse for "help -> update software": http://download.eclipse.org/rt/eclipselink/updates/
Which is highly annoying.
I am building my REST website, and no PUT or POST will work because of this error.
Does anybody know how to get 2.6.1 working? I am using a Maven jersey-quickstart-webapp Project.
This bug is a royal pain in the face.
EDIT 2: I've got a hack working today. When I check programmtically what version of eclipselink is being used at runtime like below, it tells me it is version 2.6.1, even though the bug remains:
Class<?> myClass = Class.forName("org.eclipse.persistence.Version");
Method myMethod = myClass.getMethod("getVersion");
String version = myMethod.invoke(null).toString();
System.out.println("version = " + version);
In a related question (How to find what version of EclipseLink my Eclipse Project is using?) I found out how to find the actual jar files glassfish is using for eclipse. They are in the glassfish/modules directory. Inside the org.eclipse.persistence.core.jar file has will be a readme.html which tells you the version of eclipselink that glassfish is using. For me it was 2.6.
I manually updated the org.eclipse.persistence.core.jar and the org.eclipse.persistence.moxy.jar file with the latest versions from the maven site. While this is quite hacky, as I don't know what else is effected by doing this, it does get over this problem. If I find out the correct way to do this, I will write an answer below, for all and sundry.

Going through the Jersey JIRA and eclipse link website it seems like Eclipse link 2.6.1 is still under development i.e. not yet released. That's why its not available for download.
To use an updated version of Jersey in maven you need to give depending to the newer version 2.19 in your pom.xml.
But, since eclipse link 2.6.1 is yet to be released, even if you use newer version of Jersey it might be of little use. But still you can give it a try.

To update version of jersey, find the following dependency and update the value of version tag in your pom.xml file
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.1</version>
</dependency>

Which server are you deploying your project into? I've had A LOT of problems with Glassfish 4.1.1 (incluiding that error). I downgraded to 4.1 and everything runs fine. It may be due to a problem with 4.1.1 newest modules (jars incluided in the server). I hope it helps. Its not the best solution but for me was a temporary workaround 'till I find which modules are failing.

The problem was solved by updating the version of eclipselink that glassfish was using. Eclipse updated it's version, but glassfish needs it's own update. The list of jar files that need to be placed in the glassfish/modules directory is given in this answer: How to change EclipseLink in GlashFish 4.0?

Related

Missing Jars when switching from Glassfish to Apache Tomcat Webserver on Glassfish using Eclipse

I am developing an Angularjs frontend which talks to a REST backend. I am developing using Eclipse and was using a Glassfish webserver.
I had the need to create htaccess redirect for Angular routes (see here: htaccess redirect for Angular routes) but to do this I need to change my webserver from Glassfish to Apache Tomcat since the htaccess stuff doesn't exist for the Glasshfish webserver.
When I created the new apache tomcat server v8.0, upon starting the log gave multiple errors for missing JAR files. These JARs were:
avalon-framework-4.1.3.jar
batik-css-1.6-1.jar
batik-ext-1.6-1.jar
batik-gui-util-1.6-1.jar
batik-util-1.6-1.jar
commons-configuration-1.5.jar
logkit-1.0.1.jar
nekohtml-1.9.11.jar
xercesImpl-2.8.1.jar
xml-apis-1.3.03.jar
Additionally, it wants the following JAR which doesn't not seem to be around anymore:
antisamy-1.3-SNAPSHOT.jar
A lot of the JARS mentioned above are old versions of the JARS. What can I do about the missing JAR that is not around anymore?
Also, how do I know if these JARs are the most current versions I should be using? (I added all of them I could find manually in the tomcat /lib directory)
Thanks in advance - I'm quite new to all this stuff so go easy on me:)
EDIT 1: I've included the non-snapshot version of the JAR as per unwichtich's answer. However this doesn't fix the error. Why would the Apache Tomcat v8.0 require such old JAR files to work, especially a snapshot jar? Where would one look to see where these JAR's are configured? Is it possible to know which newer versions of the JARs are compatible with my setup?
antisamy-1.3-SNAPSHOT.jar
First, you should NOT depend on SNAPSHOT dependencies, they can change the programs behaviour in the same version....
Second, SNAPSHOT versions might be seen as beta steps to the final release, so you can not expect that these SNAPSHOT versions are still available online when the final version was released.
A quick googling didn't suggest any old link to antisamy-1.3-SNAPSHOT.jar, but you may try this one: https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/owaspantisamy/antisamy-bin.1.3.jar. This should be the final of 1.3 and if you are lucky it contains the same API as the SNAPSHOT version you used before. If not, you may have to update your code so that it works with the final.
There seem to be newer versions of the lib, you also may want to try them.

The import javax.persistence cannot be resolved

I'm currently working on a project that requires EntityManager EntityManagerFacotry and Persistence each from the javax.persistence package. It seems to be for the database service, but the current code is not very well documented. By searching google it seems that there should be an xml file that comes along with this, but there isn't one of those either. I guess my question is simply how do I make these unresolved imports go away? Do I have to add another jar to the build path? It seems that I shouldn't have to since it's been around since 1.5.
Any help is greatly appreciated.
I ran into this same issue and realized that, since I am using spring boot, all I needed to do to resolve the issue was to add the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Yes, you will likely need to add another jar or dependency
javax.persistence.* is part of the Java Persistence API (JPA). It is only an API, you can think of it as similar to an interface. There are many implementations of JPA and this answer gives a very good elaboration of each, as well as which to use.
If your javax.persistence.* import cannot be resolved, you will need to provide the jar that implements JPA. You can do that either by manually downloading it (and adding it to your project) or by adding a declaration to a dependency management tool (for eg, Ivy/Maven/Gradle). See here for the EclipseLink implementation (the reference implementation) on Maven repo.
After doing that, your imports should be resolved.
Also see here for what is JPA about. The xml you are referring to could be persistence.xml, which is explained on page 3 of the link.
That being said, you might just be pointing to the wrong target runtime
If i recall correctly, you don't need to provide a JPA implementation if you are deploying it into a JavaEE app server like JBoss. See here "Note that you typically don't need it when you deploy your application in a Java EE 6 application server (like JBoss AS 6 for example).". Try changing your project's target runtime.
If your local project was setup to point to Tomcat while your remote repo assumes a JavaEE server, this could be the case. See here for the difference between Tomcat and JBoss.
Edit: I changed my project to point to GlassFish instead of Tomcat and javax.persistence.* resolved fine without any explicit JPA dependency.
If anyone is using Maven, you'll need to add the dependency in the POM.XML file. The latest version as of this post is below:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
hibernate-distribution-3.6.10.Final\lib\jpa : Add this jar to solve the issue. It is present in lib folder inside that you have a folder called jpa ---> inside that you have hibernate-jpa-2.0-1.0.1.Final jar
When I ran into this problem, I tracked down enough to fix my problem and move on.
The short version is:
At some point in time Oracle open-sourced J2EE code and the Eclipse foundation took it over.
The transition took a while so information came out during the transition which was transitory in nature. As a result, you might find articles that were only useful during the transition.
The javax.persistence package was moved to a newly named dependency (jakarta.persistence. The persistence package is part of the larger JPA (Java Persistence API). See Intro to JPA.
The Java Persistence API was first released as a subset of the Enterprise JavaBeans 3.0 specification (JSR 220) in Java EE 5. It has since evolved as its own spec, starting with the release of JPA 2.0 in Java EE 6 (JSR 317). JPA was adopted as an independent project of Jakarta EE in 2019. The current release as of this writing is JPA 3.1.
There were issues with SpringBoot pulling in multiple javax.persistence dependencies, Spring-Boot Issue 21220.
Spring and SpringBoot updated their dependencies to use the new location. From Infoq.com, Nov 24, 2022
VMware released the long-anticipated Spring Framework 6 and Spring Boot 3. After five years of Spring Framework 5, these releases start a new generation for the Spring ecosystem. Spring Framework 6 requires Java 17 and Jakarta EE 9 and is compatible with the recently released Jakarta EE 10
If you are on this page looking for answers, most likely it's because your code doesn't compile because it can't find javax.persistence. If this is the case, then you'll either need to:
add the dependency to jakarta.persistence.
Or use older versions of Java and JPA dependencies define classes in the javax.persistence package.
In the future or if you choose to you can rename references from javax.persistence to jakarta.persistence. The same class files in javax.persistence also exist in the jakarta.persistence package.
To fix my problem I added the following dependency:
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
I was using SpringBoot 2.2.2.RELEASE af the time which picked up version 2.2.3 of the jar file (jakarta.persistence-api-2.2.3.jar).
This jar file contained (at least) the following packages:
javax.persistence
javax.persistence.criteria
javax.persistence.metamodel
javax.persistence.spi
based on what my IDE is telling me.
The following articles were helpful for me to get to the solution I needed:
The SO question about this and the answer - https://stackoverflow.com/a/60024749/3281336 which pointed to links I've also included below. Thanks to #Krisz for that.
Explanation of why javax.persistence package was moved to jakarta dependency - https://blogs.oracle.com/javamagazine/post/transition-from-java-ee-to-jakarta-ee This article is good because it gives old dependencies along with the newer dependencies that are needed
SpringBoot 3 & Spring Framework 6 use Jakarta EE 9 - https://www.infoq.com/news/2022/11/spring-6-spring-boot-3-launch/
My solution was to select the maven profiles I had defined in my pom.xml in which I had declared the hibernate dependencies.
CTRL + ALT + P in eclipse.
In my project I was experiencing this problem and many others because in my pom I have different profiles for supporting Glassfish 3, Glassfish 4 and also WildFly so I have differet versions of Hibernate per container as well as different Java compilation targets and so on. Selecting the active maven profiles resolved my issue.
I solved the problem by adding the following dependency
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>2.2</version>
</dependency>
Together with
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
If you are using Gradle with spring boot and spring JPA then add the below dependency in the build.gradle file
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.1.3.RELEASE'
}
In newer hibernate jars, you can find the required jpa file under "hibernate-search-5.8.0.Final\dist\lib\provided\hibernate-jpa-2.1-api-1.0.0.Final". You have to add this jar file into your project java build path. This will most probably solve the issue.
Add this to your dependency if your using maven
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
If you are using Hibernate as a JPA implementation and you are not using Maven/Gradle, the easier way is to download whole bundle instead of jar file one by one.
Go http://hibernate.org/orm/downloads/ and download the latest library, extract the jar from the required folder.
Sad and ashamed to say that after spending 1 hour on same problem (unable to resolve #Entity and javax.persistence) occurring on STS/Eclipse and with all the imports (implementation 'org.springframework.boot:spring-boot-starter-data-jpa'). Turns out it was issue with STS/Eclipse IDE because exactly same code worked on IntelliJ IDE. If nothing works give another IDE a go.
If you are not using Maven/Gradle to import the dependency, simply just download this jar from maven repository and set in build path on Eclipse or your preferred IDE.
https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api/2.2

eclipse: java.lang.ClassNotFoundException: org.eclipse.emf.ecore.EObject driving me bananas?

Hell folks, i have eclipse Version: Helios Service Release 1 Build id: 20100917-0705.
Does someone know how to get rid of this nasty exception?
I've tried with a brand new indigo installation, brand new tomcat 7.0.22, to no avail, what could be wrong with my project?
I'm using geotools.
Ok, I somewhat found the cause. When I use Geotools' DataStoreFinder.getDataStore() method the exception shows up. if I use other methods everything's fine.
What puzzles me, and the main reason to ask this question here, is because one would expect other kind of exception, like a geotools exception, but, why an eclipse exception? however, if someone finds ever the reason, I'll be more than glad to listen about it.
if you want to replicate it, these are the steps to follow:
download geotools (exception 'works' with vers 2.7 and 8)
eclipse indigo or helios
apache tomcat 6 or 7
jdk 1.6
create a simple servlet and just call DataStoreFinder.getDataStore() inside the doget or dopost method.
Just a guess, but sounds like you don't have EMF installed and Geotools must depend on it. EObject is the EMF base class. If you used p2 to install Geotools though, it should have made sure that EMF was already installed. If however you just copied the plugins for it into your Eclipse installation and did not have EMF, you would see this.

NetBeans + GWT 2.2.0 + Maven compile problem

I'm trying to create a Maven Web Application project with Google Web Toolkit in NetBeans 6.9. I've followed the steps shown on this video: http://www.youtube.com/watch?v=M-iVZ5TJ21w
After creating the project and adding GWT to the frameworks my project compiles and deploys to Tomcat nicely. Now i like to change the default GWT version from 2.0.3 to 2.2.0. As i edit the gwt.version property in the POM and save the file, NetBeans fetches the new gwt-user-2.2.0.jar file and the javadoc. However if i try to compile the project i get this Maven error in the console:
Downloading: http://repo1.maven.org/maven2/com/google/gwt/gwt-dev/2.2.0/gwt-dev-2.2.0-linux.jar
Unable to find resource 'com.google.gwt:gwt-dev:jar:linux:2.2.0' in repository central (http://repo1.maven.org/maven2)
If i point my browser to the URL http://repo1.maven.org/maven2/com/google/gwt/gwt-dev/2.2.0/ i can see that there's truly no gwt-dev-2.2.0-linux.jar present. However i can see there a gwt-dev-2.2.0.jar which i think will be the correct one to use.
What should i do to fix this error?
You're probably using old version of gwt-maven-plugin. Switch to 2.2.0 or remove version declaration and it'll fetch newest version.
It's also possible that you have some old-format dependency for GWT in your pom. There used to be tag required for old GWT versions. If you have one you can remove tag and it will fetch system independent version without -linux suffix.

Unable to publish web app: SpringSource Tool Suite problem?

I have an existing, fully functional Spring web application based on Spring 2.5.6 - developed using SpringSource Tool Suite 2.1.0.SR1.
Because I'd like to use REST I decided to upgrade to Spring 3.0.0.M4. After editing the dependencies in pom.xml and changing my code to reflect the API changes in Spring 3.0 I tried to publish my web app to a local server (SpringSource tc - a Tomcat derivate).
The result is an almost empty web app folder and therefore a non-functional app. The app's folder only contains WEB-INF/lib with all libraries required by the Maven dependencies.
After realising that something's broken, I created a new Spring MVC project (based on the default 2.5.6) and published it to the same server. No problems. I tried to adapt my project's files (.settings/*, .project, .classpath, .springBeans), but this didn't change anything.
I'm pretty lost right now. My guess is that STS doesn't handle 3.0 apps correctly. Any suggestions?
PS: I don't want to revert to 2.5 if it's not absolutely necessary. I don't need STS and tc so I don't have a problem using other tools, but it worked fine so far.
I run into this all the time using Eclipse Galileo and m2eclipse 0.9.8 and Tomcat with WTP. I think it is m2eclipse that is the culprit. The problem seems worse after switching from Ganymede. The work around is to run mvn to create the war and then copy the war contents from "target" to WTP's "wtpwebapps" directory. You can conveniently find this horribly long path by double clicking the server in the Servers view, and choosing "Open Launch Configuration" from there click on Arguments(?) tab I think and copy the catalina.home java property that is defined as an argument there.
The problem vanished with newer versions of STS. Additionally my development environment changed a bit since I posted this question, so I can't really tell what caused the problem.
For me, it looked like a weird hiccup inside STS.
Spring Tool Suite 2.1.0 claims partial support for Spring 3.0, though not for the REST features. According to the release, future releases will add full support. From the release statement:
Features
Support for milestones of Spring 3.0 including XML editing and validation, support for #Configuration and #Bean annotations
Future
Complete Spring 3.0 support including tools for developing RESTful web applications
Try deploying your app to embedded jetty. 'mvn jetty:run' with help you confirm if that there's nothing wrong with your build (that all the right manifests and deps are in place)