0

I am trying to run a simple app on GWT, where Jsoup fetches a Wiki page, parses it to find some text, and put that text in a button. I have put the Jsoup jar file in the required folder (as you can see here:)

enter image description here

The imports are working fine, for the use of those Jsoup type variables in the later part of the code shows no errors. But still I am getting the error as seen here:

enter image description here

Can anyone tell me where I am erring?

SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • It may just be my old eyes, but I'm unable to read the error from the screenshot. Would it be possible for you to transcribe the error and include it in your question? – Henrik Aasted Sørensen Jan 04 '13 at 08:01
  • Well, I understand the problem, but the error message is pretty large! You can just view the image in another tab, it will show very clearly.. – SexyBeast Jan 04 '13 at 08:04
  • You should take a look at this article: [GWT - Using external jars / Java Projects](http://www.vogella.com/articles/GWTModules/article.html). You can't use a JAR-file directly. GWT needs the Java source to perform the compilation. – Henrik Aasted Sørensen Jan 04 '13 at 08:22
  • I didn't understand that. Can you please elaborate? – SexyBeast Jan 04 '13 at 09:00

4 Answers4

3

Jsoup is a server side processing jar. It has no GWT port!!!

Option 1 -

You need to fork and port it to GWT Compatible code ( not possible if it uses api not supported by GWT ) to use it on Client side.

Option 2 -

Instead you should try using it only on server side. Process the wiki page and send the text string to Client side.

Option 3 -

Similar third party libs in GWT - http://code.google.com/p/gwt-html/

Note: They are not always maintained. So use them only if you understand.

appbootup
  • 9,537
  • 3
  • 33
  • 65
  • Hi SSR, thanks for the info. Can you tell me how to use the jSoup jar file on the server side? That is, where to embed what, so that I can just import jSoup on my server side code and use it to scrape Web? – SexyBeast Jan 09 '13 at 06:52
  • i.e Option 2 - request and process the URL on server side. Then from Client side request the processed output via RPC or JSON. – appbootup Jan 09 '13 at 07:07
  • Please explain how to import the jSOup jar file, (i.e. where to place it, how to import it). I know the rest of the process, that is scraping and sending back info to the client.. – SexyBeast Jan 09 '13 at 07:25
1

The easiest way is to use gwtquery (aka gquery). In the best case, your code could look like this:

$(".myButton").load("file.html");

Gwtquery is a port of the jquery api for gwt, and jsoup is the same but for the jvm. Gquery is well maintained.

This is an example of porting the code in your snapshot to gquery:

// The target url should be in the same domain unless you configure CORS
GQuery.get("http://stats...", null, new Function() {
  public void f() { 
    $("<div>" + getDataObject() + "</div>") // wrap the server response in a div
      .find("p").each(new Function() {
        public void f() {
          System.out.println($(this).text());
        }
       });
  }
});
Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
1

Jsoup is a server side library and you can not use it in your gwt codes (java classes that located in com.google.gwt.sample.stockwatcher.client).

I see some unused import in your StockWatcher class (import org.jsoup.helper.Validate), you have to remove them (press Ctrl + Shift + O)

Saeed Zarinfam
  • 9,818
  • 7
  • 59
  • 72
  • I have already understood that. What i want to know now is how to use it on the server side code. As per the instructions of SSR on another question, I have placed the jSoup jar file in WEB-INF/lib. Now how do I import it into the server side code (`GreetingServiceImpl.java`)? – SexyBeast Jan 09 '13 at 07:50
  • You have to use it in your rpc classes `GreetingServiceImpl.java` and remove all usage and import in your client side class. – Saeed Zarinfam Jan 09 '13 at 07:53
  • After writing code in your rpc you should return your desire information as a java object to client side and use this information in your gwt codes. – Saeed Zarinfam Jan 09 '13 at 07:56
  • I have removed all imports from client side. Please tell me how to import that on the server side code in the file `GreetingServiceImpl.java`. I mean, what is the path-name? – SexyBeast Jan 09 '13 at 07:57
  • Add jsoup jar file in your eclipse java build path library as a external jar, see this http://stackoverflow.com/questions/13988653/gwt-using-external-jar/13988769#13988769 – Saeed Zarinfam Jan 09 '13 at 08:00
  • Okay, done that. I have kept that jar file in my desktop, I just pointed the to that. now what do i give as the import address in my server-side code? – SexyBeast Jan 09 '13 at 08:04
  • Now you have to start coding in `GreetingServiceImpl.java` in eclipse, i can understand your mean. – Saeed Zarinfam Jan 09 '13 at 08:09
  • Yeah I am doing that. Just tell me what to import, man! I am nearing my deadline! – SexyBeast Jan 09 '13 at 08:10
  • You use jsoup library for example if you use `Document` class in your server side code you have to import `org.jsoup.nodes.Document` in your code, see jsoup using documentations. http://jsoup.org/cookbook/ – Saeed Zarinfam Jan 09 '13 at 08:14
  • Your welcome, if i could help you, do not forget to give me the bounty. – Saeed Zarinfam Jan 09 '13 at 09:12
  • Of course I won't. I will be able to award it only after 24 hours! – SexyBeast Jan 09 '13 at 10:09
1

While Jsoup is designed to be a server-side library, it is not impossible to modify it to run with GWT on the client side. Some features must be cut in order to do this, and anything that uses File or InputStream must be deleted. My IDE of choice is IntelliJ Idea, and the "Find Usages" feature was very helpful in determining which functions can be safely deleted, and which other ones must be emulated. A word of advice, it's better to comment out a function that you think you don't need than to delete it entirely, because upon further inspection you may find that you actually need to modify or emulate that function.

Here are the classes or functions that I emulated:

  • StringBuilder.appendCodePoint(int codePoint)
  • Charset
  • CharsetEncoder
  • String.format(final String format, final Object... args);

Replacements:

  • java.util.Pattern becomes com.google.gwt.regexp.shared.RegExp.
  • Map loadEntities(String filename) can be hardcoded.

If you would like to take my work, you can download it here: http://www.mediafire.com/download/b3le77rrvc33vpa/Jsoup.zip
Disclaimer: You can use this file as a reference for the steps I've outlined above, but I can't guarantee that somewhere in the process of removing GWT-incompatible code that I haven't introduced the possibility of XSS attack. If you find such an error, please let me know.

dmiller309
  • 385
  • 5
  • 8