Reputation: 263
I'm trying to use htmlunit in a web service. I created a dynamic web project and implemented the function homepage(). Then I used the web service wizard to generate the web server and the client. When I tried to use it, this exception is thrown.
Exception: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException; nested exception is: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException Message: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException; nested exception is: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException
I added the htmunit jars into the buildpath. There is no reason for it to be unable to locate the classes. The server class is below:
package test.eko;
import java.io.IOException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class TestEko {
public String homePage(String place, String checkinDate, String checkoutDate){
final WebClient webClient = new WebClient();
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setPrintContentOnFailingStatusCode(false);
String URL = "http://hotels.travelcomparing.com/SearchResults.aspx?languageCode=EN¤cyCode=EUR&destination=place:" + place + "&radius=0km&checkin=" + checkinDate +"&checkout=" + checkoutDate + "&Rooms=1&adults_1=1&pageSize=15&pageIndex=0&sort=MinRate-asc&showSoldOut=false&view=hc_sr_summary&scroll=0&mapstate=contracted";
HtmlPage page = null;
try {
page = webClient.getPage(URL);
} catch (FailingHttpStatusCodeException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
webClient.waitForBackgroundJavaScript(10000);
String htmlContent = page.asXml();
String hotelName = "";
String priceFrom = "";
String result = "";
for (int i = 0; i < 5; i++){
int index1 = htmlContent.indexOf ( "<div fn=");
int index2 = htmlContent.indexOf("<div fn=", index1 + 8);
String row = (String) htmlContent.subSequence(index1 , index2);
htmlContent = htmlContent.substring(index2);
//System.out.println(row);
int index3 = row.indexOf("class=\"hc_i_hotel\" p=\"2\">");
int index4 = row.indexOf("</a>", index3);
hotelName = (String) row.subSequence(index3 + 61, index4 -34);
System.out.println(hotelName);
result = result + hotelName + " ";
int index5 = row.indexOf("<dd class=\"hc_i_price\">");
int index6 = row.indexOf("</dd>", index5);
String priceRow = (String) row.subSequence(index5, index6 + 5);
int index7 = priceRow.indexOf("<span class=\"hc_pr_syb\">");
int index8 = priceRow.indexOf("<span class=\"hc_pr_cur\">");
priceFrom = (String) priceRow.subSequence(index7 + 148, index8 - 38);
System.out.println(priceFrom);
result = result + priceFrom + " ";
}
return result;
}
}
This code should take 3 strings as parameters and access a website with those parameters and then save the page to a string and parse it for some information. Then it should return those information concatenated in a single string. The code worked normally in a normal project (not a dynamic web project). I'm very new to web services and I'm not sure what I did wrong. Can someone please point me in the right direction?
Upvotes: 0
Views: 1277
Reputation: 85779
I added the htmunit jars into the buildpath (...) The code worked normally in a normal project (not a dynamic web project)
When working on Dynamic Web Projects, all your third party libraries must be in the WEB-INF/lib folder in order to be recognized and loaded by the web application server. Make sure to do this, then re-build your application and try to deploy it, run your tests, etc.
Upvotes: 1