NoClassDefFoundError Htmlunit - java

I'm new to HtmlUnit. I tried a simple code for loading a web page using htmlunit and use the xpath to get to a specific div tag.
The xpath is correct. I have verified using the console.
The code that so far I tried is
import java.util.List;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class Test {
public static void main(String s[]) throws Exception
{
WebClient client = new WebClient();
HtmlPage page = client.getPage("http://jpu1.pv.sv.nextag.com/serv/iinedan/buyer/outPdir.jsp?search=camera");
List<?> items = page.getByXPath("//div[contains(#class,'search-result-cards')]//div[contains(#class,'listContainer')]");
for(HtmlElement item : (List<HtmlElement>)items)
{
List<?> imageContainer;
imageContainer = item.getByXPath("//div[contains(#class,'imageContainer')]/a");
System.out.println("Image container data length : "+imageContainer.size());
System.out.println("Image "+imageContainer.toString());
}
}
}
But when I execute this code, I'm facing the following exceptions.
Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Test.main(Test.java:11)
Caused by: java.lang.ClassNotFoundException:
org.w3c.dom.ElementTraversal
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 37 more
It seems to like its a configuration isssue. I downloaded the jar from the site and added it to my refrenced library. IDE that I'm using is eclipse. HtmlUnit version is htmlunit-2.13.jar

The org/w3c/dom/ElementTraversal present in library xml-apis-1.4.01.jar on which htmlunit depends on. So, as a solution you need to put this jar in your project's classpath.
However, htmlunit depends on number of libraries. And it could happen that, after you resolved the current error, you will get a noClassDefFoundError for some other class.
In order to resolve it easily, start using Maven.
Create a maven project.
Add an entry for htmlunit dependency in pom.xml as follows :
<dependencies>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
Now create a new class and add your code there.
Maven will ensure, all transitive dependencies of any library to be available and keep those in project classpath automatically (if in IDE).

Many jar package includes org.w3c.dom.ElementTraversal.
Such as xml-apis, batik-ext, jython.
I think jar package conflict maybe cause your problem.
Can you give more jar package version detail?

Related

StackOverflowError when trying to connect to MySQL

I am trying to establish a connection between a Java-based project and a MySQL database. From what I can tell, the database seems to be fine (the expected information is stored in its columns). However, when I use the Java program to open a connection I obtain a StackOverflow error, which I am not sure why is happening:
Exception in thread "Thread-2" java.lang.StackOverflowError
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
I have taken several actions to solve this issue but none of them have worked. Among them, I have successfully run the example that is described in MySQLJavaTutorials, which makes me think that this issue is not because of either the JDBC connector, the operating system, the database itself or the MySQL server version.
However, I have noticed that if I completely delete the database (by executing DROP DATABASE mydatabase) I get the exact same exception, as if the database had never existed in the first place. This also happens if I alter the name of the database to one that does not exist on purpose. My machine uses:
Operating System: Ubuntu 64 bits 14.04.5 LTS
MySQL server version: 5.5.58
Eclipse Oxygen 2
Java 1.8.0_151
Do you have any idea of why this is happening?
While the project I am working on is rather complex, the location of the call where I invoke the function is:
#Override
public void openDBConnection() throws Exception {
//this method establish a database connection
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName(driver);
// Setup the connection with the DB
//////////////////////////////////////////////////////////////////////////////
String connection = "jdbc:mysql://" + host + "/" + dbname + "?"
+ "user=" + user + "&password=" + passwd + "&useSSL=false";
System.out.println(connection);
connect = DriverManager.getConnection(connection);
//////////////////////////////////////////////////////////////////////////////
} catch (ClassNotFoundException | SQLException e) {
throw e;
} finally {
}
}
The exception is thrown at the connect = DriverManager.getConnection(connection); line. The connection string that I obtain is fine. Actually, I am quite sure that this is not related to the JDBC connection because if I miswrite the driverĀ“s name then I get a reasonable error instead of it.

how to compile Java for Spark?

I would like to compile Java file to jar. I want it to be runnable by Spark. I did try to compile normally but it have an error like this.
java.lang.NoClassDefFoundError: JavaWordCount (wrong name: org/apache/spark/examples/JavaWordCount)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.spark.util.Utils$.classForName(Utils.scala:229)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:700)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:187)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:212)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:126)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
This is how I compile Java.
javac -classpath spark-sql_2.11-2.1.1.jar:spark-core_2.11-2.1.1.jar:scala-compiler-2.11.8.jar:scala-library-2.11.8.jar JavaWordCount.java
And this is how I make jar file
jar cvf JavaWordCount.jar JavaWordCount*.class
However doing like this came up with an error above when I tried to spark-submit.
spark-submit --class JavaWordCount JavaWordCount.jar README.md
I also did try to change class into org.apache.spark.examples.JavaWordCount but it's still give me the same error.
Where did I go wrong? Any suggestion?
PS I use an example JavaWordCount in Spark folder.
I solved the problem only need to javac with all spark jar files(only the one that needed) and when use jar cvf with spark jar files again which I didn't do it when I posted the question.

API works with Java application but not JavaFX

I am building a software in Java and JavaFX. It generates PDF reports (with DynamicReports) then uses SendGrid API to send to send them. I have already added the SendGrid API .jar file to the classpath.
The problem is that my JavaFX application crashes when I instanciate my subclass of SengGrid. As you can see:
Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: com/sendgrid/SendGrid$Email
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760) at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Reporting.TableReport.lambda$build$0(TableReport.java:160)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.sendgrid.SendGrid$Email
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 20 more
Now the interesting part is that the API works perfectly when I call it from a non JavaFX project. And more interestingly, it also works when I call it from a "console" main class inside the very same JavaFX project.
So far, I have tried to call that "console" main from my JavaFX code as a sort of static method that would fulfill my task (sending the PDF files). But again, I encounter the very same exception.
Can anyone suggest a solution to solve this ? (or at least go around it)
Turns out it was an issue with Netbeans. Using the exact same code with Eclipse works perfectly. I couldn't find an explanation but I'm giving this answer for anyone who might stumble upon the same issue as me.

org.apache.thrift7.TBase vs org.apache.thrift.TBase

I am not being able to find any jar which contain this kind of package. Can, anyone tell me in which libthrift Link jar i can find this class and package. I couldn't find any jar containing this package. Everytime i execute my java file it throw following expection:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/thrift7/TBase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at backtype.storm.topology.TopologyBuilder.initCommon(TopologyBuilder.java:215)
at backtype.storm.topology.TopologyBuilder.setSpout(TopologyBuilder.java:178)
at backtype.storm.topology.TopologyBuilder.setSpout(TopologyBuilder.java:164)
at com.test.newpackage.TopologyMain.main(TopologyMain.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.thrift7.TBase
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 16 more
Try searching Central when you're looking for a class. fc:"org.apache.thrift.TBase" suggests org.apache.thrift:libthrift:0.9.0.
For libthrift7, see the instructions on the Storm wiki: use the http://clojars.org/repo repository and depend directly on storm:storm. storm:libthrift7 will be brought in transitively.
Did you include org.apache.thrift in the maven dependencies.? If not please include:
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.0</version>
</dependency>
please include following:
<!-- https://mvnrepository.com/artifact/storm/libthrift7 -->
<dependency>
<groupId>storm</groupId>
<artifactId>libthrift7</artifactId>
<version>0.7.0-2</version>
</dependency>

Absent Code attribute in method that is not native or abstract in class file javax/transaction/SystemException

I am building an application using JBoss AS 7.1 and I am writing a test
#Test
public void testGetLoginUser() throws Exception {
final MarketCrudService crudService = new MarketCrudService(jpaRule.getEntityManager());
crudService.create(new Login("user1", "password"));
}
This test depends on #Rule created which instantiates a in-memory derby db for writing test data
When I run this test, I see following error
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/transaction/SystemException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2248)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:141)
... snipped
This happens at line
emFactory = Persistence.createEntityManagerFactory(persistenceUnitName, persistenceProperties);
I googled and found out the this is because jta.jar is missing, so in my pom.xml I added
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
<version>1.0.1.Final</version>
</dependency>
Running again did not solved the problem, how can I fix it?
Adding this resolved the issue
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
</dependency>

Categories