问题
Hello good Stack Overflow people, I do have a business problem and would like to get some answers/pointers/ideas on how to go about solving it. perhaps this has been mentioned in some other topic but so far I searched the internet but not able to find a straight answer..
Business Problem: One of our clients would like to have a Java web based application but launched as a desktop application. i.e. the client would like to double click on an icon some where on their desktop and get a browser window launched pointing to the URL/context of the web application which will be running locally on a Jetty server that will get started up when the user clicks on the icon.
So far I have figured out how to package my application and create an exe launcher (using install4j) (basically i created an executable jar file with jetty server and my web app packaged together with a startup class that will start Jetty server and point it to the context of the web application).
My problem is I have no idea how to start up a (native) web browser and point it to the URL/context of the web app.
I have found this code that launch the user's default browser:
String url = "http://www.google.com";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
which is good but not good enough. and the reason its not good enough is that when the user close the browser the Jetty server would still be running in the back ground. What I want is when the user closes the browser I also want to detect this event and close down the Jetty server (without using any active X or JavaScript please)
Some one have mentioned creating a JPanel and include a native browser inside it using some DJ native swing API but I have no clue as how to do this.
Any ideas?
Many thanks in advance
回答1:
Here is a piece of code just FYI. You can fork native browser in a process and return a java Process object to your java code. You can invoke the processObject.waitFor()
method to wait for the invoked process to terminates.
The Launcher :
public class Launcher implements Runnable {
public void run() {
Process p;
try {
p = Runtime.getRuntime().exec("notepad.exe");
p.waitFor();
// Do something after the forked process terminates
System.out.println("The browser is exit");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The main class:
public class TestProcess {
public static void main(String[] args) throws Exception {
new Thread(new Launcher()).run();
}
}
回答2:
Ok, I have managed to find what I am looking for, so I am posting my findings here for anyone who might find this info useful. There are simply 2 solutions that I could use:
There is something called 'BareBoneBrowser' check out the project link here
this project allows you to launch the user's default browser on any platform (windows, Mac OS, linux) which is exactly what I wanted. and I have tried it and it seems to be working fine. There are few limitations to it (you can't detect if that browser windows has been closed by the user. Maybe there is a way if you have time to understand the code and alter it) but its a quick way to get a browser launched with Java and at the same time being cross platform.
The other solution that I found is the DJ Project click here for project home page
This is almost a complete solution that allows you to open a 'native' browser in a JPanel. Cross platform (works for Windows and Mac OS or even Linux). Because the browser gets launched inside a JPanel, you can detect when the user 'close' the JPanel and terminates the application so you can then do what ever you need to do to shutdown/terminate or stop your application and doing any cleanup work required.
Hope that this will help anyone who is looking to do something similar. My advice is to just download the code and start playing with it or do a small POC (Proof Of Concept) project to get a better understanding about the API.
Cheers.
回答3:
Try this
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
URL--//your url
it will open the default browser. Hope it works of other OS.
来源:https://stackoverflow.com/questions/6448993/launching-and-closing-a-native-browser-that-points-to-web-app