Open html in browser from Java. Bug if browser is not started? - java

I am using
java.awt.Desktop.getDesktop().browse(uri);
to show my users a generated html file. All is fine if the browser (firefox 3.5.7; linux) is started before I make the call. But if the browser start is triggered from the getDesktop().browse call then the java application will not exit until the browser closes.
How can I avoid this behaviour? Is this known under windows/macOsx too?
(If I then force the exit of the java application the browser will close too and sometime even crash!?)

The reason the browser exists is because the browser is launched as a dependent process...so when the first process shuts down, all its dependent processes are shut down with it. But in the case when you start the browser first, it already has a different process ID that isn't affected by your application. I think the only way to avoid this behavior is to use a different technique (perhaps Runtime.exec()?) to launch the browser in a way that registers it as a non-dependent process.

The project Browser Launcher (http://browserlaunch2.sourceforge.net/) can be the solution for your problem. You can launch a browser from your code like this:
String url = "http://....";
BrowserLauncher() launcher = new edu.stanford.ejalbert.BrowserLauncher();
launcher.openURLinBrowser(url);

Related

Disable message print on terminal when using Desktop.browse

I'm developing a command line tool and at some point it redirects the user to the default web browser. I use the following code for that
if(Desktop.isDesktopSupported()){
Desktop.getDesktop().browse(new URI("http://www.example.com"));
}
The browser opens without any problem but there are some messages printed on the console while this is up. stuff like
[6620:6620:0622/180058:ERROR:browser_window_gtk.cc(1082)] Not implemented reached in virtual void BrowserWindowGtk::WebContentsFocused(content::WebContents*)
or
Created new window in existing browser session.
Is there a way to stop printing these kind of messages. (as it is a command line application it does not look good). Is there any other way to open a browser?
Thanks in advance
This looks like this is printed by the browser on startup. These GTK log messages aren't uncommon.
You can start the browser directly from the console to verify this.
EDIT:
As starting the browser is handed off to an operating system specific method, there's not much control over it.
To have better control you can try to launch the browser directly:
launch the process Launch JVM process from a Java application use Runtime.exec?
for linux (more or less cross-distribution): Linux: command to open URL in default browser
for windows: Launching a website via windows commandline
You can use Desktop.browse if the other methods fail.

Session Management in different browsers

I have deployed my web application on Tomcat7. There is a form asking some info and starts deploying another application and shows a progress bar and after completion redirects you to the new url.
If in the middle of deployment I close the tab or go to another url and then comes back to my application then I got new form instead of previous page showing my progress.This is happening in chrome and IE, while in Mozilla FirFox things are going perfect.
My question is that can some body tell me what is actually going on in those browsers and what I have to do to tackle this problem.
Cheers
may the reason be in the approach those browsers work with tabs? every new tab for GC is a new system process...

call gui from JSP?

I am currently learning JSP and Java Servlets. I was wondering if there is any way to call a java GUI from a JSP, not necessarily to load into the page, but to load onto the screen for database input. I am struggling trying to find some reference on how to do this on the net, but no luck so far.
You could use Java Webstart to start up the application on the client side. Or, ask the user to install a local client differently (like a daemon service).
The "GUI application" must have some kind of RPC (=remote procedure call) service running in background.
Once the client has your gui application installed, the JSP can invoke urls on http://localhost:port (ajax would help sending messages to the GUI application). The GUI application must have some rpc system (rest, webservice) that receives the requests from your web application (your jsp app) and start the GUI requested by the URL.
The problem here is to make sure the client has the port of choice available.
Another possibility is to register some URL to your application in the client side, so that when the user clicks a url like myapp://form/123 the OS automatically invokes your application to handle that URL. This kind of thing must be done differently per operating system. It's the way it works when you click on a magnet link or a skype://link. The procedure to register urls in the operating system is different per OS, you need to do some research (I never did that, but I am sure it is possible). You can also register a file extension to your app, and make sure that the specified extension is registered to your app. When the browser downloads the file and opens it, your app is invoked with the file downloaded as parameter (which will contain the instruction for your GUI). But most browser will not start automatically the app associated to the file after the download: again, you need to do some tricks on the client's operating system to make the "download and open" the default behavior for the browser.

Java: Control browser through Process

I'm remote controlling a Java application on a PC through an Android phone, and I needed my application to open a browser at the phones command, chrome in this case. I created a "Process" for chrome, opening a certain address. However, I need to be able to give tools on the Android phone for controlling the web page, such as scrolling. Can I programmatically send a command for chrome to scroll from my PC application containing the Process?
Sorry, it may have been unclear, but the only connection the android phone has to the program is through a socket. It is only used as a remote control for another Java application on a PC, which has its own screen.
I do not think that clean solution exists.
But I can suggest the following directions:
(1) try to investigate the native chrome API. If it has such ability call it with JNI.
(2) Try to use class java.awt.Robot. It allows to simulate user's activity, e.g. mouse clicks. Unfortunately it does not allow you to find any window outside your application, so it is a problem to decide where to perform the click.
(3) You can create proxy server and make browser you open to go to the target URL through the proxy. The proxy server will insert into the page your javascript that will communicate with server. The application that opens browser will send commands to server. The javascript that you inserted will receive these commands using AJAX and perform them. JavaScript can scroll browser window, so theoretically you can implement this.
If you can target the tab you want to control and edit the address bar you could send the command 'javascript:scrollTo(x, y)'. I just tested it on this page and it seems to work fine, replacing what I typed with the original address of the page.
Can I programmatically send a command for chrome to scroll from my PC
application containing the Process?
Not directly. What you could do is make some sort of web service that sits between the Android client and page that the Android client can send commands to and the page can periodically poll via AJAX calls to see what the client wants. That would be a clean DIY way that would work on other browsers besides Chrome.
You can use vnc viewer applications for that.
http://code.google.com/p/android-vnc-viewer/

WebBrowser1 problem with external web app

the external Application works good in IE so i know the problem is inside My App.
The external web app is java applet and java script based, and the problem im experiensing is when the java applet tryes to run a java script. The Applet opens a seconday window with a java script and tryes to do some operations but then i just stops. And my Application seems to be non responsive and i have to restart it.
in IE it works just fine, the java applet opens the window does its thing and updates back in to the Applet.
anyone know why it not working in the webBrowser control?
Two scenerio could be possible:
1) whenever we opens the web site in web browser control any previous session(authentication) cookie is not being reused, and new session will be created.
2) you have to override the eventhandler webBrowser_newWindow to allow opening the new window.

Categories