Eclipse IvyDE managed libraries not exported into runnable JAR file - eclipse

The problem
So far, Eclipse has exported IvyDE managed libraries into a runnable JAR file, but I'm not sure why it has stopped working recently.
What's wrong? How can I fix the issue?
ivy.xml
<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation= "http://ant.apache.org/ivy/schemas/ivy.xsd">
<info organisation="com.example" module="eclipse-ivyde-export-issue" />
<dependencies>
<dependency org="org.json" name="json" rev="20220320"/>
</dependencies>
</ivy-module>
Steps for reproducing the issue
Clone the project:
git clone git#github.com:haba713/eclipse-ivyde-export-issue.git
Import the cloned project into Eclipse workspace.
Right-click the class src/com.example/Main.java in the project tree and
choose Run as → Java Application. See the output: {"key":"value"}.
Right-click the project and choose Export → Java → Runnable JAR file.
Choose the launch configuration for the class com.example.Main and
Library handling: [x] Extract required libraries into generated JAR. Click
Finish.
Try to run the generated JAR file:
$ java -jar eclipse-ivyde-export-issue.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
at com.example.Main.main(Main.java:8)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 1 more
Environment
Eclipse 2022-06 M2 (4.24.0 M2)
IvyDE 2.2.0.final-201311091524-RELEASE
OpenJDK 17.0.3+7-Debian-1deb11u1
Debian GNU/Linux 11 (Bullseye)
Eclipse 2020-06 with OpenJDK 8 works fine
I tested the same project with Eclipse 2020-06 and OpenJDK 8. JAR export gives a warning "This operation repacks referenced libraries" and IvyDE managed classes are included into the generated JAR file.
(The question was posted also to Eclipse Community Forums.)

Workaround
I think that this is a JDT or IvyDE bug. Workaround is to use JRE System Library 1.8 instead of 17. See the note at the end of README for more information.
The chosen Launch configuration must have JDK 1.8 as a runtime environment.

Related

application.xml modified in Eclipse causes deployment issue

When importing a Maven project to Eclipse (sometimes just refreshing the workspace), The file application.xml is modified and causes a problem deploying the EAR to Jboss 5:
org.jboss.deployers.spi.DeploymentException: Exception determining structure: AbstractVFSDeployment
Caused by: org.jboss.xb.binding.JBossXBRuntimeException: {http://java.sun.com/xml/ns/j2ee}library-directory cannot appear in this position. Expected content of {http://java.sun.com/xml/ns/j2ee}application is unordered_sequence: {http://java.sun.com/xml/ns/j2ee}module* {http://java.sun.com/xml/ns/j2ee}security-role* {all {http://java.sun.com/xml/ns/j2ee}descriptionGroup}?
IDE Has replaced:
<application version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
By:
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:application="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" version="1.4">
And has added:
<library-directory>lib/</library-directory>
Has happened with Eclipse Mars and Eclipse Luna,
Is this because of some configuration in the IDE?
It is due to Eclipse configuration. Try Window - Preferences, then Maven -> Java EE Integration and uncheck the 'Enable Java EE Integration' as shown below. You'll also want to make sure that preference is not set individually for any particular project (right click on the project and choose 'Properties' then the same Maven -> Java EE Integration').

Can maven treat WEB-INF\lib the way eclipse (and m2e) does?

I have a servlet/jsp web project which runs fine on eclipse and is exported as war fine (once I clean it that is). I mavenized the project deleting all of the dependencies from the WEB-INF\lib folder except a homebrew jar (the output of another project in the workspace). When I run the package maven goal I get messages for missing classes from this jar:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
#..... NOTICE THIS COMES FROM A CUSTOM JAR
[ERROR] /C:/path/DataServlet.java:[3,30] package xxx.java.helpers does not exist
Now this has been asked before and the most rigorous solution appears to be to create a local repo: Can I add jars to maven 2 build classpath without installing them? (NB: I am at maven 3).
I would like to avoid this - so is there any way maven will just stuff this jar to WEB-INF\lib in the war ?
Solutions that use some maven plugin to cp the contents of the WEB-INF\lib in the war are welcome - although I just have this feeling that there should be a solution that takes into account the "special" nature of this folder.
Observations:
Alt+F5 removes this line:
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
which corresponds to the "Web App libraries" in the Java Build Path. So not only maven refuses to take into account the WEB-INF\lib - it also breaks the build path of eclipse completely.
Related:
Maven: How to include jars in Eclipse, which are not available in repository?
Uses the maven eclipse plugin : update my classpath with an Eclipse User Library via the maven eclipse plugin - not compatible with m2e
How does the m2e eclipse plugin interact with eclipse? - apparently m2e checks the pom then calls the eclipse builders (hence the .classpath is read)
Eclipse maven-enabled web app references workspace projects, but those are not deployed when running Tomcat server
Deploying a Maven project with dependencies to Tomcat or Jboss running within Eclipse
Did you add this jar from WEB-INF\lib as a dependency like this:
<dependency>
<groupId>someGroupId</groupId>
<artifactId>someArtifactId</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/homebrew jar</systemPath>
</dependency>

How to exclude Eclipse plugin library jars from Tomcat classpath

I have a Spring MVC project that uses Maven imported into Eclipse Kepler SR1 JEE and deployed to Tomcat 7 within Eclipse. Everything was fine until I recently added elasticsearch as a dependency to the Maven pom file and now when I start Tomcat I get an exception
java.lang.ClassCastException: org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet
Launching the server in verbose mode, I see that its because the servlet API is now being loaded from 2 places:
the Tomcat installation folder where it normally comes from
Loaded javax.servlet.Servlet from file:/C:/ProgramFiles/apache-tomcat-7.0.33/lib/servlet-api.jar
but also from one of the Eclipse plugins (Groovy) I have installed
Loaded javax.servlet.Servlet from file:/C:/ProgramFiles/eclipse/plugins/org.codehaus.groovy_1.8.6.xx-20130703-1600-e43-RELEASE/lib/servlet-api-2.4.jar
I can't figure out why this is happening. If I rename the servlet jar file in the plugin's lib folder (i.e. change the extension from .jar to something else so that its not recognized), Tomcat starts up without an exception but that's a hack and so I'd like to know what the cause is and if I'm missing some configuration to exclude plugin library jars from the Tomcat classpath.
Thanks.

The import org.apache.commons cannot be resolved in eclipse juno

I am having a problem while compiling my project in Eclipse. It is showing the error The import org.apache.commons cannot be resolved.
Please tell me what does this error means and how to solve it.
The mentioned package/classes are not present in the compiletime classpath. Basically, Java has no idea what you're talking about when you say to import this and that. It can't find them in the classpath.
It's part of Apache Commons FileUpload. Just download the JAR and drop it in /WEB-INF/lib folder of the webapp project and this error should disappear. Don't forget to do the same for Apache Commons IO, that's where FileUpload depends on, otherwise you will get the same problem during runtime.
Unrelated to the concrete problem, I see that you're using Tomcat 7, which is a Servlet 3.0 compatible container. Do you know that you can just use the new request.getPart() method to obtain the uploaded file without the need for the whole Commons FileUpload stuff? Just add #MultipartConfig annotation to the servlet class so that you can use it. See also How to upload files to server using JSP/Servlet?
If you got a Apache Maven project, it's easy to use this package in your project. Just specify it in your pom.xml:
<project>
...
<properties>
<version.commons-io>2.4</version.commons-io>
</properties>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${version.commons-io}</version>
</dependency>
</dependencies>
...
</project>
expand "Java Resources" and then 'Libraries' (in eclipse project).
make sure that "Apache Tomcat" present.
if not follow-
right click on project -> "Build Path" -> "Java Build Path" -> "Add Library" -> select "Server Runtime" -> next -> select "Apache Tomcat -> click finish
You could just add one needed external jar file to the project.
Go to your project-->java build path-->libraries, add external JARS.Then add your downloaded file from the formal website. My default name is commons-codec-1.10.jar
Look for "poi-3.17.jar"!!!
Download from "https://poi.apache.org/download.html".
Click the one Binary Distribution -> poi-bin-3.17-20170915.tar.gz
Unzip the file download and look for this "poi-3.17.jar".
Problem solved and errors disappeared.
You could also add the external jar file to the project.
Go to your project-->properties-->java build path-->libraries, add external JARS. Then add your downloaded jar file.
In my little experience, I have solved the issue about org.apache.commons.cli, on my Eclipse Version: 2019-12 (4.14.0):
In Provar(2.8.0), Issue got resolved after adding the jar file(commons-io-2.11.0.jar) to the project.
Steps:
1.Download the latest JAR file from https://commons.apache.org/proper/commons-io/download_io.cgi
Added jar file under lib folder in project.
2.Project--> Properties --> Java build path --> Libraries--Add Jars from lib folder.

WTP - m2e not deploying transitive dependencies

I have a web application which is structured in this way:
A.jar -> B.war -> C.war
I'm using Eclipse Juno and the WTP version is 1.1. The A.jar is a workspace utility project which is being included by B.war. B.war is a war project that is included by C.war as an overlay. That's the way I'm doing that:
<dependency>
<groupId>com.projects</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
After that, I deploy the C project to the Tomcat server. That works like a charm if I manually deploy the Maven generated war to the Tomcat, because A.jar is included in WEB-INF/lib. However my problem comes when I let m2e-wtp do the deploy, because it's doing the overlay properly but not including the A transitive dependency. I tried including it as a pom, as I read somewhere around here, but I have the same result.
<dependency>
<groupId>com.projects</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.projects</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
I'm using the newest versions of m2e (1.2) and m2e-wtp (0.16) and have my projects updated with the Maven configuration.
Is it an m2e-wtp issue or simply do I have to organize my project in other way?
EDITED
I noticed m2e-wtp configuration is stored into Eclipse's project./settings/org.eclipse.wst.common.component file. That's how it looks for my war:
<dependent-module deploy-path="/"
handle="module:/overlay/prj/B?includes=**/**&excludes=META-INF/MANIFEST.MF">
<dependency-type>consumes</dependency-type>
</dependent-module>
<dependent-module deploy-path="/"
handle="module:/overlay/slf/?includes=**/**&excludes=META-INF/MANIFEST.MF">
<dependency-type>consumes</dependency-type>
</dependent-module>
As I can see the war dependency is set for consume while the jar dependencies are set for use.
Released m2e-wtp version 0.17 doesn't seem to fix it.
EDITED (2013-08-30)
Today I was back to the same problem. Even I have Eclipse kepler installed with the latest stable release of WTP out of the box, this problem seems to persist. I thought it was solved, but I apparently mischeck it...
I think it is not a problem of your project organization. Your issue is very similar to this m2e-wtp bug report.
It seems to be a Eclipse Juno and WTP Plugin problem.
I had the same problem and i solved in this way:
Backup your eclipse workspace and your project code
remove your project from eclipse (without remove the contents)
open a command terminal (cmd)
run mvn eclipse:clean
run mvn eclipse:eclipse -Dwtpversion=2.0
edit your eclipse classpath file with a text editor: %PROJECT_PATH%\.classpath
remove all lines with attribute kind="var" from your .classpath file. For example:
< classpathentry kind="var" path="M2_REPO/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar" sourcepath="M2_REPO/javax/servlet/servlet-api/2.5/servlet-api-2.5-sources.jar"/>
save file & close editor.
in eclipse, click on: file->Import...->Maven->import existing maven project, and import your project again
Maven -> Update Project (if you want)
You could see on deployment assembly in eclipse project properties that problem is solved and the maven dependencies are there.
Hope this helps.
I also have this problem. I have an ugly workaround:
Put all the original war's dependencies in a separate project (type jar) and make both original war and overlay war depend on that. So for the example:
A.jar -> B.war -> C.war
becomes
A.jar -> B-dependencies.jar (new module called B-dependencies created)
B-dependencies.jar -> B.war
B-dependencies.jar -> C.war
Note that it's not specific to Tomcat; I'm using JBoss.