This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to embed a browser in Java?
I am working on an application where I have to embed a web browser with application. Any ideas on how to achieve it? Also will I be able to interact JavaScript from within the code?
see this post .... Embed a web browser within a java application
check out Eclipse, it has an embedded browser which is configurable by the user (they support multiple browsers).
you can probably embed their browser even if you use an AWT based application using the AWT_SWT bridge if you are using Java swing application.
this article may help you get started.
Check out Lobo
[Lobo] is an open source web browser that is
written completely in Java. Lobo is
being actively developed with the aim
to fully support HTML 4, Javascript
and CSS2. Lobo also supports direct
JavaFX rendering.
For very simple pages, you can use the JEditorPane, which is in the Swing API: see doc
I'm afraid that embedding a real browser is your only option, if you need JavaScript capabily and AJAX etc. look at JavaXPCOM API in order to see how easily you can embed FireFox. There is also JDIC, that will allow you to embed IE in Windows environments. You will need some DLL files but the procedure is straight forward with both APIs and well documented.
Let's admit that JEditorPane is fine, but mainly for HTML you have control over. I use it only to render HTML help files into my application. Once you start visiting sites with it things start getting nasty.
Related
The program I am writing is in Java.
I am writing a little program that will download the html of webpages and save them. It works easily for basic pages that don't use JavaScript. But how can I download the page if I want it after a script has updated it? The page I am dealing with is actually updated by Ajax which might be one step harder.
I understand that this is probably a difficult problem that involves setting up a JavaScript run time environment of some kind. I am prepared for a solution of any level of difficulty, I just don't know exactly how to approach it or where to get started.
You can't do that alone with Java only. As the page that you want to download is rendered with javascript, then you must be able to execute the javascript to get the whole rendered page.
Because of this situation, you need to use a headless browser which is a web browser that can access to web pages but can’t show the output within a GUI, aims to provide the content of web pages as fully rendered to serve to the programs or scripts.
You can start with the most famous ones which are Selenium, HtmlUnit and PhantomJS
I have an HTML page that has Javascript code. It needs to be rendered first before it can be converted into an image.
I am aware of projects like wkhtmltoimage, PhantomJS, khtmltopng, webkit2png, PrinceXML and html2image. I have implemented a few of those but I am trying to find a pure Java solution that does not have to use Process to execute a command. Any help would be great, thanks!
edit: I looked into Cobra however it seems that the JS support is still in dev and it does not parse my html file properly.
Or if there are any other ways of doing this, please let me know. I am just trying to find the best solution possible.
There is no pure Java solution - no one has written a browser in Java that supports HTML 5.
I'd try either of these approaches:
Use env.js + rhino to simulate a browser in which you can run the JavaScript. That should give you a DOM which you can render using FlyingSaucer, for example.
Add SWT to your classpath (plus the binary for your platform). It contains a Browser component that uses your system's browser to render URLs or an HTML string.
You probably need SWTBot to run the browser in headless mode.
If that doesn't work and you're on Linux, then you can start an in-memory X server Xvfb to open your browser. Or you can use vncserver to start a desktop on your server.
[EDIT] The project phantomjs might do what you want:
PhantomJS (www.phantomjs.org) is a headless WebKit scriptable with JavaScript or CoffeeScript.
[...]
Use cases: Headless web testing, Site scraping, Page rendering
Multiplatform, available on major operating systems: Windows, Mac OS X, Linux, other Unices
Fast and native implementation of web standards: DOM, CSS, JavaScript, Canvas, SVG. No emulation!
Pure headless (X11) on Linux, ideal for continuous integration systems. Also runs on Amazon EC2.
The quickstart page explains how to load a web page and render it to an image.
I have found a solution using WebRenderer. WebRenderer is a paid solution and has a swing, server, and desktop edition. The swing edition is the only one that supports HTML5 as of 7/9/2012. However, the swing edition can be used on a server to convert the image by instantiating the browser and not creating a JFrame. See this question.
I have an old tool an (ex-)colleague wrote a few years back with Jaxer, that I'd like to replace/rewrite.
Jaxer is an (abandoned) server-side framework based on a headless Mozilla/Gecko-Browser allowing you to use JavaScript and the DOM server-side.
Since Jaxer is abandoned and because I have big problems installing and running Aptana Studio 1.5 with Jaxer on a new computer, I'm looking for a library/framework/something on which I can base a new version.
This tool is only run locally inside Aptana Studio (the IDE for Jaxer) and was never intended to be an actual web app. It crawls our customers websites by loading them page by page into the server-side Mozilla. In order to do that it uses jQuery and predefined CSS selectors to find the links in the menus and parse other information out of the pages. The final result is basically a glorified sitemap.
I'd like to keep this modus operandi if possible and continue using jQuery/JavaScript/the DOM to load and parse/access the pages, but it can be wrapped in a framework based on another language such as Java. I considered writing something based on Gecko myself, but that seems a bit over the top, so I'm open for an other suggestions.
As far as HTML crawling/parsing goes:
http://ccil.org/~cowan/XML/tagsoup/
or
http://jsoup.org/
IDEA: Implement a recent web browser into a java application (for saved offline, non server content).
The question is this: can I have a java application implement a webbrowser with jquery / html / css support within a java program?
So I am asking anyone who has played with JRex for advice: I want to know how complicated will it be to integrate an open source webbrowser into java. I am not all that keen on the idea of compiling Mozilla from source build. Is there a ready made compiled version?
Is there a simplified method to have latest compiled version (most current in terms of support for HTML css & javascript), and integrate that into an application?
Also: I appreciate the amount of work required to support for HTML4 nevermind 5, and CSS2 compliance. How close is JRex to that?
Application: My intention with the webbrowser is to render a webpage from offline content. It will not need to be online content, and will simply be for file based displays = e.g. file:///C:...
Does the webbrowser have to be wrapped into a server to function, e.g. to pass files to the browser to render is how complicated? I am not keen to have to implement Jetty or another server type application just for this.
If JRex is not the solution... what then? Is it possible to start a browser implementation within Java and can Java interact with the information and traverse the Dom?
Or alternatively is there .hta equivalent in recent browsers like firefox?
If you need to have the embedded browser interact with your application code, you could try the SWT Browser control, it's actually maintained as opposed to JRex. Browser uses either WebKit or Gecko or embedded IE as appropriate, or lets you choose which one you want, so it should run jQuery and familiar Javascript. And since SWT is a JNI library to begin with they probably already have guidance on how to deploy an app that uses JNI.
You can feed HTML into the control from a string (example) or a java Url - which can point to local files or resource files in your JAR, which I assume will let you split your app into different files.
To call Java code, you need to expose it as Javascript functions. example
To manipulate the HTML from Java code, you need to call Javascript functions from Java. example
To make the previous two tasks easier, you might want to look into a JSON library to simplify passing around complex data.
Does it have to be implemented within a Java program? Could you let the user use the default browser on their machine (ie does it matter what browser)?
If not would use the Java Desktop API.
if (desktop.isSupported(Desktop.Action.BROWSE)) {
txtBrowserURI.setEnabled(true);
btnLaunchBrowser.setEnabled(true);
}
If you are using Java 1.5 try http://javadesktop.org/articles/jdic/
Please forgive me if my question sounds too dumb :(... I have created a java desktop application, which has a single UI screen in Swing... The application creates a pdf report... I want to convert this application into a jsp based web application. So do I just have to create a simple screen where the parameters required to create the report are asked, this screen replaces the swing based UI... and the main block of java code (used to create the report with the help of input parameters) can be invoked from a jsp file? Is that it? Or is something more significant required to make this app into a web app. How do I find out what else is required?
You should have a web server. Create a web archive (.war) from your code base by reusing Java classes and new jsp file (UI). So your understanding is correct. and remember there won't be any main method now.
Yes - JavaSE code is the same Java on server side. You have to change UI. You may use plain HTML or web framework such as JSF.
This highly depends on your code. Business logic and PDF generation might be reused but the UI will definitely be replaced. You'd then also have to account for the download part, i.e. the response could be a page with the DL link or the generated PDF.
Edit: as Azodious already stated, you'd need a webserver like Tomcat (or JBoss which is a fully fledged application server and includes Tomcat) that is able to execute the Javacode, so just replacing the UI wouldn't help much.