copyInputStreamToFile method does not exist - java

I'm trying to use the copyInput method in my code, but seems like Intellij told me that the method does not exist
FileUtils.copyInputStreamToFile(response.getEntity().getContent(), downloadedFile);
I'm using the code from here
http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/
But it seems like the method exist here
https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html
I'm using maven, I tried with 2.0, 2.1, 2.4 without luck
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
Java 1.8
Here is the error message
Error:(201, 22) java: cannot find symbol
symbol: method copyInputStreamToFile(java.io.InputStream,java.io.File)
location: class org.apache.commons.io.FileUtils

You probably have another (older) org.apache.commons.io.FileUtils on your classpath, which would explain the issues you're having. Most likely one of your dependencies has an old org.apache.commons:commons-io artifact on the classpath. Due to the groupId difference, Maven considers them different artifacts and puts both on the classpath.
If that is what's really happening, you can explicitly forbid maven from including this transitive dependency by adding an exclusion to the dependency that references the old commons-io (even if it depends on it transitively):
<dependency>
<groupId>offending.artifact.groupId</groupId>
<artifactId>offending.artifact.artifactId</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>

I was able to see the wrong version using the menu options "To find the offending class in IntelliJ IDEA: Menu > Navigate > Class > type FileUtils", there were like 4 FileUtils,
I imported the correct version on the jar file and it fix the problem, Thanks #Anton.

FileUtils.copyInputStreamToFile(inputStream,file);
This function may be used in versions larger than two (2). You need to add this line to the gradle:
implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'

I got the same problem, but it seems that I had old version of version of commons-io. I fixed it by changing version to 2.6 in pom.xml file.

Related

ECLIPSE ERROR: The package javax.xml is accessible from more than one module: , java.xml [duplicate]

When referencing simple .jar files, Eclipse shows an error stating:
The package java.awt is accessible from more than one module: <unnamed>, java.desktop
This happens for instance when javax.awt or javax.swing is included in the .jar files.
The simplest example would be the following:
package test;
import javax.swing.JDialog;
public class Test {
public static void main(String[] args) {
new JDialog();
}
}
Adding a .jar file to the classpath with only the folder structure javax/swing (no files needed) will cause the error to appear. I'm using JDK 10/12 (neither works). Setting the compiler compliance to 1.8 makes the whole thing work again.
On another machine with Eclipse 2018-09 this works with compiler compliance set to 10.
I'm on Eclipse 2019-03, on a (for testing purposes) freshly installed Eclipse 2018-09 it works fine. Why?
Edit June/2020 (Solution)
As the answers correctly stated, this is a restriction built into Java ages ago and only recently was forced upon us. I came into contact with it while migrating a big project with dozens of dependencies to Maven. There were libraries from around the year 2000! There were 'meta libraries' which consisted of several libraries packaged together.
So there was no other way than to identify what was still needed (into the trash with the rest!), update libraries which violate the rules or find replacements for them. This took me many, many hours.
In the end it worked out and we've got a nice Maven project to work with.
This is caused by
a JAR on the Classpath that contains the package java.awt that also exists in the system library but the
JRE System Library is on the Modulepath
In the Java Platform Module System (JPMS) it is not allowed to use the same package in more than one module. If the Modulepath and the Classpath is used, everything on the Classpath is handled as the <unnamed> module (in your case the package java.awt exists in the system module java.desktop and also via the JAR on the Classpath in the module <unnamed>).
Since the JRE System Library cannot be moved from the Modulepath to the Classpath (see this answer by Stephan Herrmann for details), you only have the following options:
Set the compiler compliance to 1.8 (as you already mentioned)
Rebuilt the JAR to avoid Java system library package names inside the JAR (if reflection is used, additional code changes may be necessary):
If you have the source code, change the package names (e.g. change the package and subpackae java to java_util and javax to javax_util) and recreate the JAR
If you have only the .class files you have to decompile the .class files first
Since I'll bet lots of people will be running into this problem with modular Java, I'll help and give the real answer.
This error happens when
you have a dependency in your project
that contains code
using packages
that are also in the modules
being referenced by your project
If your project has set the source compatibility to something like Java 12, it will start enforcing the rule, that has been there all along in Java:
"Don't use packages that belong to the JDK in your own code."
Unfortunately, lots of developers and vendors have done that over the years. Can't do that anymore.
If you set your project to Java 12 source compatibility, Eclipse adds the JDK modules which include everything "java.*" and "javax.*" and even "jdk.*", "org.w3c.*". These packages may be in use by your dependencies or their transitive dependencies.
How to fix
You need to:
look at which package its complaining about
and expand the "Projects and External Dependencies" node in the Package Explorer.
Find out which dependency is using that package.
Then you can simply exclude that dependency from your project.
Or you could get the source of that dependency, if available, and rebuild the jar with changed packages. Otherwise you have to remove that dependency and find a replacement for that technology. Pain huh?
If its a transitive dependency you can often just exclude it. Here is an example of that for Gradle based projects.
GradleConfig.xml:
configurations {
all*.exclude group: 'xml-apis'
}
In my case, it was because I included a dependency (Apache Tika) in the POM.xml file.
I had to force the exclusion of the module that contained the classes with errors while imported at that dependency:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.24.1</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
It worked for me that way.
I found a simple solution to troubleshoot this in Eclipse. Hit Ctrl + Shift + T in Eclipse to open the Open Type prompt. Then type the name of the package that is causing the issue. For me, it was org.w3c.dom. Now the search results will show all the locations from where this package is being loaded. Remove every JAR from the classpath that comes in the result other than the JDK 11 library.
My project being a legacy one, I had to remove a lot of JARs from the build path. Also, my project does not use Maven. So removing the JARs was a fairly straightforward step. The steps might vary for other build tools like ANT, Maven, Gradle, etc. I have just explained the troubleshooting steps above.
See also: The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml where I answered:
Disappointingly I don't see any compiler flags to show what jar the problem is with
Even -Xlint:module doesn't seem to show up anything useful and eclipse doesn't shed any light on the issue
Instead to find where java.awt comes from I've been using this script:
mvn dependency:copy-dependencies -DincludeScope=test -DoutputDirectory=deps
for i in deps/*.jar; do if unzip -l $i| grep -q java.awt; then echo $i; fi ; done
Strictly you don't have to specify the scope test as that's the default but I've included it as you might want to use compile instead
I think my flavour of the problem might be useful.
I got this error for classes under javax.xml.stream, by old Maven projects that depend on artifacts like xml-apis, stax-api, or geronimo-stax-api.
Technically, the problem is what others have already said: those artifacts expose the javax.xml.* package without any awareness of Java modules (they were invented later), so the package is automatically assigned to the unnamed module, which conflicts with the same package being included in the JDK's most recent versions, where the package has its own module name, and therefore the same package results in two different modules, which is forbidden.
That said, the practical solution is essentially to work with Maven exclusions to remove those dependencies from your project and let it use the JDK version instead (or, of course, remove them as direct dependencies, if that's your case). Use the equivalent if you're working with another build system.
In theory, the more recent flavours of these packages offered by the JDK might be non backward-compatible, in practice, I doubt such JSR specifications changed much over the years and so far, I haven't seen any issue with their replacement.
Since this is ranks quite high on Google I'm going to drop this here in case it's helpful for someone.
I've found some interesting behaviour with Java 11 and the xmlbeans library. The xmlbeans library is a transitive dependency of Apache POI, a very popular library for working with Microsoft Office documents, it is used to handle the internal XML structures of the newer Office formats. I've tested it with Apache POI 3.9 and it works perfectly fine, despite the error shown by Eclipse. So, I guess in Java 11 this rule it's not fully enforced.
For Apache POI version 5.0.0 using Maven, in the pom.xml:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
Fixed: "The package javax.xml.parsers is accessible from more than one module"
I met a similar issue with the eclipse IDE while upgrading JDK from 1.8 to 11, "The package org.w3c.dom is accessible from more than one module: , java.xml". upgrading eclipse from 2019 to 2021 and setting system JDK home to 11 does not solve it. I don't think it's caused by "org.w3c.dom" existing in different jars of the classpath,dut to "Order and Export" should ordered the search sequence.
After several hours of searching and investigating, this issue is fixed by setting the Java Compiler - Compiler compliance level to 1.8(default is 11).
You can do what other people suggest which is to exclude xml-apis which worked fine with me, but if your are using and an old jaxb-api replace them with jakarta.xml.bind-api:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
and of course upgrade your jaxb-impl to match the same api:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.3</version>
</dependency>
Steps below helped me,
Right click Eclipse project > Properties > Java Build Path
In Libraries tab, remove all the external jar files under Modulepath and add them under Classpath (you can just select all the jars and drag them under Classpath)
Click Apply and Close
Hope it help you too.
Wow, this was a tough one!
Some helpful tips (a summary of my journey through this maze):
This started when I migrated from Java 1.8 to 11.0.11
Right out of the gate this caused problems. I needed to change the syntax for how to specify the Java version for the maven build plug in as shown below:
Before
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
After
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
After that, everything compiled at a command prompt. However, when opening the project in Eclipse, I got a bunch of errors as shown in the next section.
Getting "<class> cannot be resolved to a type" errors
When I got errors in Eclipse and not at the command line I immediately started looking at configuration settings in Eclipse to no avail. After some googling I came to this post. The post at https://www.eclipse.org/forums/index.php/t/1110036/ was also very helpful.
Finding what resources were the problem
The answer to this post by #Anu suggesting using <shift><ctrl>T to bring up the type search window was very helpful using this I was able to see what resources were causing the problem (i.e. any resource that is also included in the JDK, in my case it was the ErrorHandler in the xml-apis jar). Type the name of one of the resources giving a "cannot be resolved to type" to see the name of the jar file, in my case the jar is xml-apis-1.4.01.jar.
All of this is shown in the screen shot below (click on the image to get a cleaner view).
Finding the Dependency that contains the offending jar
We can now use the information from https://www.eclipse.org/forums/index.php/t/1110036/ to find the dependency in our pom.xml file that contains the offending resource.
Open the pom.xml file in Eclipse and select the "Dependency Hierarchy". In the filter type the name of the jar file we found in the previous step (in my case it was xml-apis).
This will pull up the dependencies creating the problem. A screen shot of the Dependencies Hierarchy filtered for "xml-apis". From this we can see the offending dependencies in our pom.xml file are xercesImpl and xlsx-streamer.
Solving the problem
We can now add exclusions to these dependencies that will resolve the problem as shown below.
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>2.1.0</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.0</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
And this fixes the current problem.
For anyone who suffer with this problem when refering to the org.w3c or javax.xml imports, be careful with this one module dependency: xml-apis it conflicts with java default xml classes.
One big project who references it is called DynamicJasper
Here's the image of my app
It seems to me the real problem here is that the JDK (in my case, JDK-19) has org.w3c.dom embedded in it, so everything else that was doing the correct thing by referencing libraries from external-lib, rather than embedding is now broken. I removed xml-apis and xerces, but it still complains about org.w3c.dom. the only place it is referenced in my project is the JDK.
Removing jar files, (xml-apis.jar and xerces_2_5_0.jar) Does not fix the issue. I cannot remove batik, project depends upon it.
I have a standard Java build, no maven. There is no decompiling/removing a valid reference that can be done with 3rd party software like batik.
Any suggestions on how to remove the embedded org.w3c.dom from JDK-19 so I can build?
I had this problem with the Stanford Core NLP package. As mentioned above adding an exclusion in the pom.xml solved the problem:
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>4.5.2</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
First of all, make sure your pom is actually correct by running
mvn install
if that works, and eclipse still complains look at build path and find the library it is holding onto that is not in your pom.
Sometimes I just
rm .classpath
rm -rf .settings
rm -rf project
mvn eclipse:clean eclipse:eclipse

How to solve my dependencies problem using Maven and slf4j with OWLAPI on Eclipse please

First of all, I'm a newbie with Maven, so I apologize if I misunderstood something.
In a recent question I ask about the import of OWLApi in my Java project and people told me to use Maven to import it, instead of importing JAR file.
I tried a new Maven Project and imported to test OWLApi and it worked well.
Today, for my profesionnal project, I managed to work with Maven. I converted my project to Maven, added OWLApi in my dependencies... and when I build with Maven, I got the error :
Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError
Moreover, my classes are not compiled anymore (impossible to run my classes, because no .class are compiled).
If it helps, this is my pom, where I tried to excluse slf4j, as I found in another question. But it didn't help me :
<dependencies>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-distribution</artifactId>
<version>5.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Thank you in advance.
What's happening here is that the classpath contains two adapters, one that passes logging calls done with Log4J to a SLF4J logger, and one that does exactly the reverse. With both in the classpath, any logging call would go first to one logging system, then be passed to the other, and this would create an infinite loop. This would result in a stack overflow, which is what the error message is reporting.
OWLAPI doesn't import either jar, it only imports the SLF4J API, so the conflicting jars must be dependencies of other libraries.
You have tagged Eclipse here, so I assume you're using Eclipse. When you open a pom file in Eclipse, you get a tabbed editor, with one tab being Dependency Hierarchy. You can use this tab to look for the jars you wish to exclude, the tab will show which of your dependencies are bringing in the jars, so you can put the exclusions in the right place. It should be sufficient to exclude one of the jars (it depends on which logging system you mean to actually use).

Problem with javax.xml in OpenJDK 11 and Eclipse [duplicate]

When referencing simple .jar files, Eclipse shows an error stating:
The package java.awt is accessible from more than one module: <unnamed>, java.desktop
This happens for instance when javax.awt or javax.swing is included in the .jar files.
The simplest example would be the following:
package test;
import javax.swing.JDialog;
public class Test {
public static void main(String[] args) {
new JDialog();
}
}
Adding a .jar file to the classpath with only the folder structure javax/swing (no files needed) will cause the error to appear. I'm using JDK 10/12 (neither works). Setting the compiler compliance to 1.8 makes the whole thing work again.
On another machine with Eclipse 2018-09 this works with compiler compliance set to 10.
I'm on Eclipse 2019-03, on a (for testing purposes) freshly installed Eclipse 2018-09 it works fine. Why?
Edit June/2020 (Solution)
As the answers correctly stated, this is a restriction built into Java ages ago and only recently was forced upon us. I came into contact with it while migrating a big project with dozens of dependencies to Maven. There were libraries from around the year 2000! There were 'meta libraries' which consisted of several libraries packaged together.
So there was no other way than to identify what was still needed (into the trash with the rest!), update libraries which violate the rules or find replacements for them. This took me many, many hours.
In the end it worked out and we've got a nice Maven project to work with.
This is caused by
a JAR on the Classpath that contains the package java.awt that also exists in the system library but the
JRE System Library is on the Modulepath
In the Java Platform Module System (JPMS) it is not allowed to use the same package in more than one module. If the Modulepath and the Classpath is used, everything on the Classpath is handled as the <unnamed> module (in your case the package java.awt exists in the system module java.desktop and also via the JAR on the Classpath in the module <unnamed>).
Since the JRE System Library cannot be moved from the Modulepath to the Classpath (see this answer by Stephan Herrmann for details), you only have the following options:
Set the compiler compliance to 1.8 (as you already mentioned)
Rebuilt the JAR to avoid Java system library package names inside the JAR (if reflection is used, additional code changes may be necessary):
If you have the source code, change the package names (e.g. change the package and subpackae java to java_util and javax to javax_util) and recreate the JAR
If you have only the .class files you have to decompile the .class files first
Since I'll bet lots of people will be running into this problem with modular Java, I'll help and give the real answer.
This error happens when
you have a dependency in your project
that contains code
using packages
that are also in the modules
being referenced by your project
If your project has set the source compatibility to something like Java 12, it will start enforcing the rule, that has been there all along in Java:
"Don't use packages that belong to the JDK in your own code."
Unfortunately, lots of developers and vendors have done that over the years. Can't do that anymore.
If you set your project to Java 12 source compatibility, Eclipse adds the JDK modules which include everything "java.*" and "javax.*" and even "jdk.*", "org.w3c.*". These packages may be in use by your dependencies or their transitive dependencies.
How to fix
You need to:
look at which package its complaining about
and expand the "Projects and External Dependencies" node in the Package Explorer.
Find out which dependency is using that package.
Then you can simply exclude that dependency from your project.
Or you could get the source of that dependency, if available, and rebuild the jar with changed packages. Otherwise you have to remove that dependency and find a replacement for that technology. Pain huh?
If its a transitive dependency you can often just exclude it. Here is an example of that for Gradle based projects.
GradleConfig.xml:
configurations {
all*.exclude group: 'xml-apis'
}
In my case, it was because I included a dependency (Apache Tika) in the POM.xml file.
I had to force the exclusion of the module that contained the classes with errors while imported at that dependency:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.24.1</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
It worked for me that way.
I found a simple solution to troubleshoot this in Eclipse. Hit Ctrl + Shift + T in Eclipse to open the Open Type prompt. Then type the name of the package that is causing the issue. For me, it was org.w3c.dom. Now the search results will show all the locations from where this package is being loaded. Remove every JAR from the classpath that comes in the result other than the JDK 11 library.
My project being a legacy one, I had to remove a lot of JARs from the build path. Also, my project does not use Maven. So removing the JARs was a fairly straightforward step. The steps might vary for other build tools like ANT, Maven, Gradle, etc. I have just explained the troubleshooting steps above.
See also: The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml where I answered:
Disappointingly I don't see any compiler flags to show what jar the problem is with
Even -Xlint:module doesn't seem to show up anything useful and eclipse doesn't shed any light on the issue
Instead to find where java.awt comes from I've been using this script:
mvn dependency:copy-dependencies -DincludeScope=test -DoutputDirectory=deps
for i in deps/*.jar; do if unzip -l $i| grep -q java.awt; then echo $i; fi ; done
Strictly you don't have to specify the scope test as that's the default but I've included it as you might want to use compile instead
I think my flavour of the problem might be useful.
I got this error for classes under javax.xml.stream, by old Maven projects that depend on artifacts like xml-apis, stax-api, or geronimo-stax-api.
Technically, the problem is what others have already said: those artifacts expose the javax.xml.* package without any awareness of Java modules (they were invented later), so the package is automatically assigned to the unnamed module, which conflicts with the same package being included in the JDK's most recent versions, where the package has its own module name, and therefore the same package results in two different modules, which is forbidden.
That said, the practical solution is essentially to work with Maven exclusions to remove those dependencies from your project and let it use the JDK version instead (or, of course, remove them as direct dependencies, if that's your case). Use the equivalent if you're working with another build system.
In theory, the more recent flavours of these packages offered by the JDK might be non backward-compatible, in practice, I doubt such JSR specifications changed much over the years and so far, I haven't seen any issue with their replacement.
Since this is ranks quite high on Google I'm going to drop this here in case it's helpful for someone.
I've found some interesting behaviour with Java 11 and the xmlbeans library. The xmlbeans library is a transitive dependency of Apache POI, a very popular library for working with Microsoft Office documents, it is used to handle the internal XML structures of the newer Office formats. I've tested it with Apache POI 3.9 and it works perfectly fine, despite the error shown by Eclipse. So, I guess in Java 11 this rule it's not fully enforced.
For Apache POI version 5.0.0 using Maven, in the pom.xml:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
Fixed: "The package javax.xml.parsers is accessible from more than one module"
I met a similar issue with the eclipse IDE while upgrading JDK from 1.8 to 11, "The package org.w3c.dom is accessible from more than one module: , java.xml". upgrading eclipse from 2019 to 2021 and setting system JDK home to 11 does not solve it. I don't think it's caused by "org.w3c.dom" existing in different jars of the classpath,dut to "Order and Export" should ordered the search sequence.
After several hours of searching and investigating, this issue is fixed by setting the Java Compiler - Compiler compliance level to 1.8(default is 11).
You can do what other people suggest which is to exclude xml-apis which worked fine with me, but if your are using and an old jaxb-api replace them with jakarta.xml.bind-api:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
and of course upgrade your jaxb-impl to match the same api:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.3</version>
</dependency>
Steps below helped me,
Right click Eclipse project > Properties > Java Build Path
In Libraries tab, remove all the external jar files under Modulepath and add them under Classpath (you can just select all the jars and drag them under Classpath)
Click Apply and Close
Hope it help you too.
Wow, this was a tough one!
Some helpful tips (a summary of my journey through this maze):
This started when I migrated from Java 1.8 to 11.0.11
Right out of the gate this caused problems. I needed to change the syntax for how to specify the Java version for the maven build plug in as shown below:
Before
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
After
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
After that, everything compiled at a command prompt. However, when opening the project in Eclipse, I got a bunch of errors as shown in the next section.
Getting "<class> cannot be resolved to a type" errors
When I got errors in Eclipse and not at the command line I immediately started looking at configuration settings in Eclipse to no avail. After some googling I came to this post. The post at https://www.eclipse.org/forums/index.php/t/1110036/ was also very helpful.
Finding what resources were the problem
The answer to this post by #Anu suggesting using <shift><ctrl>T to bring up the type search window was very helpful using this I was able to see what resources were causing the problem (i.e. any resource that is also included in the JDK, in my case it was the ErrorHandler in the xml-apis jar). Type the name of one of the resources giving a "cannot be resolved to type" to see the name of the jar file, in my case the jar is xml-apis-1.4.01.jar.
All of this is shown in the screen shot below (click on the image to get a cleaner view).
Finding the Dependency that contains the offending jar
We can now use the information from https://www.eclipse.org/forums/index.php/t/1110036/ to find the dependency in our pom.xml file that contains the offending resource.
Open the pom.xml file in Eclipse and select the "Dependency Hierarchy". In the filter type the name of the jar file we found in the previous step (in my case it was xml-apis).
This will pull up the dependencies creating the problem. A screen shot of the Dependencies Hierarchy filtered for "xml-apis". From this we can see the offending dependencies in our pom.xml file are xercesImpl and xlsx-streamer.
Solving the problem
We can now add exclusions to these dependencies that will resolve the problem as shown below.
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>2.1.0</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.0</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
And this fixes the current problem.
For anyone who suffer with this problem when refering to the org.w3c or javax.xml imports, be careful with this one module dependency: xml-apis it conflicts with java default xml classes.
One big project who references it is called DynamicJasper
Here's the image of my app
It seems to me the real problem here is that the JDK (in my case, JDK-19) has org.w3c.dom embedded in it, so everything else that was doing the correct thing by referencing libraries from external-lib, rather than embedding is now broken. I removed xml-apis and xerces, but it still complains about org.w3c.dom. the only place it is referenced in my project is the JDK.
Removing jar files, (xml-apis.jar and xerces_2_5_0.jar) Does not fix the issue. I cannot remove batik, project depends upon it.
I have a standard Java build, no maven. There is no decompiling/removing a valid reference that can be done with 3rd party software like batik.
Any suggestions on how to remove the embedded org.w3c.dom from JDK-19 so I can build?
I had this problem with the Stanford Core NLP package. As mentioned above adding an exclusion in the pom.xml solved the problem:
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>4.5.2</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
First of all, make sure your pom is actually correct by running
mvn install
if that works, and eclipse still complains look at build path and find the library it is holding onto that is not in your pom.
Sometimes I just
rm .classpath
rm -rf .settings
rm -rf project
mvn eclipse:clean eclipse:eclipse

java.lang.NoClassDefFoundError:javax/xml/bind/JAXBContext

I'm using JDK 1.5 in my web project and using maven for compile.
I was facing this:
WebServiceException ("javax.xml.ws.WebServiceException: Provider com.sun.xml.ws.spi.ProviderImpl not found")
in my project. To fix that I used two approaches:
First, I add jaxws-rt-2.1.4 in my class path.(Also made entry in POM.xml)
Second, Added jar in jre/lib folder
But after this I am facing this:
java.lang.NoClassDefFoundError:javax/xml/bind/JAXBContext
Does anyone have any idea that could help me?
Adding a dependency on jaxb-impl should be sufficient, as this transitively depends on jaxb-api, which is the JAR containing the class it's complaining about. In Java 6, javax.xml.bind is part of the JRE by default so it would work out of the box there.
add this dependency to pom
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1</version>
</dependency>

How do I get depend on all artifacts from a group of a version from maven?

I'm new to Maven and I'm trying to build a project for the first time. I want to write some code that depends on apache lucene. Here's a list of artifacts in maven that I'm trying to get.
Is there any way instead of explicitly listing each artifact, I could simply depend on all artifacts of a given version? I tried this:
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>*</artifactId>
<version>3.6.1</version>
</dependency>
which gave me the error
'dependencies.dependency.artifactId' for org.apache.lucene::jar with value '' does not match a valid id pattern. # line 19, column 19
I can verify that I can download dependencies when I explicitly state them. IE this works fine:
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.1</version>
</dependency>
I realize depending on everything in lucene is probably sub-optimal, but for doing something quick-and-dirty I'd hate to have to manually populate all these little lucene libraries. What is the typical practice for getting a large set of related dependencies in maven?
Short answer: you can't. Remember you just do this once and later you can simply copy-paste dependencies (not very DRY though). Also consider creating an archetype that will quickly create a skeleton with all required dependencies (for quick and dirty projects).
Longer answer: well, you can work around that. Create a separate pom.xml with:
<packaging>pom</packaging>
and declare all Lucene dependencies there manually, one after another. Once and for all. Later you can simply add a dependency to your pom.xml (that is to groupId/artifactId/version defined there) which will transitively include all dependencies of that pom.xml.
Talking about transitivity: if you depend on a JAR in maven and that JAR has other dependencies, you get that transitive dependencies implicitly. Examine Lucene poms, maybe it's enough to import few of them and rely on transitive dependencies?
Inside a single dependency for a groupId add different artifactId's
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<artifactId>spring-context</artifactId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

Categories