Getting ClassCastException for xerces class when importing Ant build script - java

(I just asked this in the Gradle forum, but I concluded that SO just works better.)
I'm looking at modifying an existing Gradle build script. It currently tries to execute "ant" in order to run a target from a specific Ant build script. This appears to be platform-specific, and it fails on my Windows box. I'm attempting to convert it to the more portable (?) process of importing the build script and executing the desired converted task.
The first thing I did was just add the "ant.importBuild" statement just before the existing target, and ran that. This fails with the following root cause:
Caused by: java.lang.ClassCastException: org.apache.xerces.parsers.XIncludeAwareParserConfiguration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration
at org.apache.xerces.parsers.SAXParser.<init>(Unknown Source)
at org.apache.xerces.parsers.SAXParser.<init>(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.<init>(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl.<init>(Unknown Source)
at org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParser(Unknown Source)
at org.apache.tools.ant.util.JAXPUtils.newSAXParser(JAXPUtils.java:215)
at org.apache.tools.ant.util.JAXPUtils.getNamespaceXMLReader(JAXPUtils.java:172)
at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:244)
at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:177)
at org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:93)
at org.apache.tools.ant.ProjectHelper$configureProject.call(Unknown Source)
at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:76)
From the related occurrences of this on the web, it's clear there's a classpath conflict (that didn't tell me anything that wasn't obvious), but I don't know how to resolve this.
The build scripts in question are from the Mockito code base (https://github.com/mockito/mockito), so you can see them there. I only added "ant.importBuild 'build-ant.xml'" just before the place where the Ant target is executed.

I got similar problem with xerces before which caused by a newer version xerces was used automatically due to gradle use newer one by default.
I will suggest you use gradle's dependencies task to check

Related

Why is my API not getting loaded for my minecraft-Plugin?

I made myself a fancy TikZ-Api which allows to make a visualisation of Data (Plotting, diagram). The way it works is, you make an instance of the API like
TikZAPI api = new TikZAPI(); and then you add your Data for the plot (simple plot with dots) like api.addPoint(xCoord, yCoord). After I collected some data, I can print the entire code for the plot with api.print();. Then the code will appear in the console and I can copy and paste it manually into a LaTeX-Document where I can view the plot. Now the actual problem has nothing to do with the API, the API works perfectly fine. I want to collect some data of my minecraft server for example the maximum player count per hour and add this data to a plot to let it print into the console. BUT when I use my API as a library (IntelliJ: Project Structure -> Libraries -> + -> Library, which works fine, I can access all methods of my API in my plugin) and reload the server I get this error:
org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin TestAPI v1.0.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[Spi
got.jar:git-Spigot-ad703da-e2403a3]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
9) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at org.bukkit.craftbukkit.v1_16_R1.CraftServer.dispatchCommand(CraftServ
er.java:755) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.PlayerConnection.handleCommand(PlayerCo
nnection.java:1703) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.PlayerConnection.a(PlayerConnection.jav
a:1546) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.PacketPlayInChat.a(PacketPlayInChat.jav
a:47) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.PacketPlayInChat.a(PacketPlayInChat.jav
a:1) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.PlayerConnectionUtils.lambda$0(PlayerCo
nnectionUtils.java:19) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.TickTask.run(SourceFile:18) ~[Spigot.ja
r:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.IAsyncTaskHandler.executeTask(SourceFil
e:144) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.IAsyncTaskHandlerReentrant.executeTask(
SourceFile:23) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.IAsyncTaskHandler.executeNext(SourceFil
e:118) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.MinecraftServer.aZ(MinecraftServer.java
:943) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.MinecraftServer.executeNext(MinecraftSe
rver.java:936) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.IAsyncTaskHandler.awaitTasks(SourceFile
:127) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.MinecraftServer.sleepForTick(MinecraftS
erver.java:920) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:
852) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$0(MinecraftServe
r.java:164) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_291]
Caused by: java.lang.NoClassDefFoundError: tikzapi/TikZAPI
at test.Commands.onCommand(Commands.java:39) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[Spi
got.jar:git-Spigot-ad703da-e2403a3]
... 18 more
Caused by: java.lang.ClassNotFoundException: tikzapi.TikZAPI
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_291]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
java:167) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
java:96) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_291]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_291]
at test.Commands.onCommand(Commands.java:39) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[Spi
got.jar:git-Spigot-ad703da-e2403a3]
... 18 more
Well, I think the error is that the API is not getting loaded. But HOW can I do this? I already put the API into the plugins folder of my server, does not work. I made a subfolder in the plugins folder called "lib", did not work either. It does not matter if I use Eclipse or IntelliJ, both IDEs do not work. I have read that it has something to do with the manifest file but how do I make such an manifest file and does it really fix my problem? I cant use maven, because its my own API I made for myself and I want to use it like a normal library. Any ideas?
I assume your TikZ-Api is already compiled .jar file.
I will try to give you solution for IntelliJ IDEA, but general idea is same for all IDEs. When you add .jar into the libraries and then compile your plugin, IDE assumes that all these sources will be provided by VM instance during run and IDE does not have to pack the library sources with your plugin JAR file. This is true ONLY for bukkit.jar since bukkit starts first and then it loads plugins. It does not know that there is another API JAR somewhere which should be also loaded...
First simple solution is to copy raw (non-compiled) API .java sources into your plugin in the IDE and not add library at all. (Useful only if your API has few .java files and you still have them available)
Second solution is to add .jar API into libraries (as you mentioned) and tell the IDE that some libraries should be packed with your plugin during compilation and final .jar file will contain them. In IDEA, you have to create artifact configuration before compiling into .jar.
Then choose extract.
Then remove your bukkit entry from there (because we do not want to pack whole bukkit sources into our plugin) and you should see your API .jar file there. Then click Apply / OK.
Now when you build (compile) your plugin with Build artifacts, your API .jar will be compiled as well and packed into your plugin JAR file which will generate in out\artifacts
The best way here is to make the API as a plugin and load the dependency in the plugin.yml. That way, it is very easy to access it via any other plugin and there is no update compatibility issues if you use the API in different plugins. It is the norme with API in MC Server to create a plugin that acts as an API.

Step does not have a matching glue code error in cucumber eclipse

I am trying to run a feature file which mentioned follows.
Feature: Prove the concept of my script
Scenario: My first Test
Given This is my first step
When This is my second step
Then This is my third step
and when I run this feature file, it give an error that says the follows and the steps have been highlighted with the statement of "Steps does not have a glue code."
The error is shown as below. I want to run the above feature file and get to know the missing files of the feature.
Exception in thread "main" java.lang.NoClassDefFoundError: gherkin/lexer/Encoding
at cucumber.runtime.FeatureBuilder.read(FeatureBuilder.java:154)
at cucumber.runtime.FeatureBuilder.parse(FeatureBuilder.java:115)
at cucumber.runtime.model.CucumberFeature.loadFromFeaturePath(CucumberFeature.java:104)
at cucumber.runtime.model.CucumberFeature.load(CucumberFeature.java:54)
at cucumber.runtime.model.CucumberFeature.load(CucumberFeature.java:34)
at cucumber.runtime.RuntimeOptions.cucumberFeatures(RuntimeOptions.java:235)
at cucumber.runtime.Runtime.run(Runtime.java:110)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.ClassNotFoundException: gherkin.lexer.Encoding
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 9 more
Please, I am a starter to cucumber and I want to resolve this problem. Please help me if you can.
Your exception tells me that you are missing a dependency.
I would clone https://github.com/cucumber/cucumber-java-skeleton or download it as a zip and get it up and running. This would hopefully tell what's wrong with your setup.
This problem is happening because of missing of the encoding inside of gherkin in your eclipse set up.
You have to install the latest version of gherkin.
The latest available gherkin can be find from Marven repository.
After downloading the latest version, import that gherkin jar file to the cucumber project.
Please add cucumber tags which starts with # symbol Like below
#featureTest
Feature: Prove the concept of my script
#Scenario1
Scenario: My first Test
Given This is my first step
When This is my second step
Then This is my third step
Make sure the words in feature file matches "As is" in Step definition java file for Given, When and Then

ClassDefNotFoundError while class in classpath

I'm trying to run ParSeMiS. According to the documentation, it requires ant, prefuse and antlr jars to be available in its lib directory. I've put all the required jars in it. However, when I try to run it, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: antlr/TokenStreamException
at de.parsemis.miner.environment.Settings.parseFileName(Settings.java:198)
at de.parsemis.miner.environment.Settings.parseOption(Settings.java:312)
at de.parsemis.miner.environment.Settings.parse(Settings.java:170)
at de.parsemis.miner.environment.Settings.parse(Settings.java:122)
at de.parsemis.Miner.run(Miner.java:358)
at de.parsemis.Miner.main(Miner.java:61)
Caused by: java.lang.ClassNotFoundException: antlr.TokenStreamException
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 6 more
Now, I've verified that antlr/TokenStreamException.class is present in the antlr jar. I have tried adding the jar manually to the classpath by both exporting the CLASSPATH variable and setting it via the -cp switch. However, none of that works, and I still get this exception. Can anybody help me figure out what's wrong? Thanks.
The problem is that you're using -jar which ignores your CLASSPATH environment variable. You should list your dependencies in the manifest, as shown here, e.g.
Class-Path: lib/ant.jar lib/antlr-3.4-complete.jar lib/prefuse.jar
(It should have still worked with an explicit -cp option, however. My guess is that you got something wrong when specifying that, and assumed it was the same underlying cause as the failure when using the environment variable.)
I realize this question is very old, but I just had exactly the same problem and found this thread. For posterity, I'm posting how I did eventually get it to run:
As mentioned above, when running with -jar, java apparently ignores the class path. So don't run it with -jar. instead include the jars in the path and run the class directly. Poking around, the following should work (paths are on my Ubuntu 12.10 system):
java -cp /usr/share/java/antlr.jar:/full/path/to/parsemis.jar de.parsemis.Miner
You can then pass in options to the above. Maye sure you use full paths, and no shortcuts like ~/foo, as they apparently don't expand.
Of course, if you're using a Dot-formatted graph like I am, it dies very early on complaining of "unexpected char 0xA", but at least it gets further.

getting javax.mail.MessagingException

I am using javax.mail .jar file to read the mail messages. But when i m running the code i am getting the following exception.
I added mail.jar in classpath.
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingE
xception
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: MasterProcess. Program will exit.
any suggetionsn to solve this issue please ...
You're not getting a MessagingException, the VM is complaining that it can't find MessagingException (although it's probably that it's looking for it because it wants to throw it, but those are issues for later).
Check if your mail.jar actually contains this class, and check if your mail.jar really is on the classpath.
The last thing that could happen is that the class is incompatible with your version of Java. Classes compiled for 1.5 won't run on 1.4, for example.
Working in eclipse and not in command line clearly say that there is multiple versions of mail jar present in the project.
Ex:
You have 3 Jars , Jar X, Jar Y and Jar Z.
With out you knowing JAR X might be already be bundled inside JAR Z ( in this case mail jar but some different version.
So what is the problem in having multiple version of same jar ?
You have two different versions and you will not know which will be referenced in your project. (so if you are looking for some class from mail-2.jar in your project you will get class not found exception if the reference is made to mail-1.jar by class loader)
So how come its working properly on Eclipse ?
In eclipse you can see there is an option for ORDER the library, these will be reference in the same order, but while running in command prompt we used to load the as lib/* which loads all to gather and we will not know which lib will get loaded first.
How to identify the culprit and fix the issue ?
Option 1 :
See docs page / user guide of the libs you are using to see what they have in them.
Option 2 :
In eclipse move the JAR in question (mail.jar here) to TOP in the class path order. (your program should work now)
Now bring down the order one by one to see where you get the error. (if you wish to find the jar which is causing this issue , duplicate reference)
Extract the jar which has the duplicate reference remove the duplicate inside the JAR and repackage it. (if required)
Also be sure you're having also the java activation.jar jar.
Are you by any chance working with an App Server (eg. Jboss), if so, just check classloading model (in jboss with single classloading model you may be loading a not so up-to-date version of the jars)
I've also faced the same error. It happens to me that while compiling i used the jars correctly. While running it on command line i noticed the mail.jar (javamail API) was not included.

Class in buildpath-jar still not found

I'm developing a eclipse plugin rcp and I'm running into a NoClassDefFoundError
Exception in thread "Thread-7" java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
at org.geonames.WebService.search(WebService.java:783)
at geo.GeocoderGeonames$SearchThread.run(GeocoderGeonames.java:119)
Caused by: java.lang.ClassNotFoundException: org.jdom.input.SAXBuilder
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClassInternal(BundleLoader.java:483)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:399)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:387)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:87)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 2 more
The class that supposedly cannot be found is in a jar that I have added to the buildpath. I don't get any compile error etc only this exception when the running application enters the code where this class is needed...
Is there some other place I need to add the jar
After reading this added the jar to the MANIFEST.MF, which solved the problem.
As I understand it, eclipse starts several classloaders which only sees what MANIFEST.MF tells them to see and ingnores the build path...
How are you running your plug-in? You may need to add the JAR to the class path in the VM arguments.
In our experience a NoClassDefFoundError can sometimes mean that more than one version of a Class are found, as there is also a ClassNotFoundException that's normally thrown if a class cannot be found.
Another reason in your case (XML parser) might be something with endorsed classes. Are you directly importing the jdom classes or something like org.w3c...? If so, have a look at the "endorsed classes" system of Java, something that I just recently came across.

Categories

Resources