启动和关闭指向Web应用程序的本机浏览器 [英] launching and closing a native browser that points to web app

查看:112
本文介绍了启动和关闭指向Web应用程序的本机浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,Stack Overflow的人, 我确实有业务问题,想就如何解决这个问题获得一些答案/建议/想法.也许在其他主题中已经提到了这一点,但到目前为止,我在互联网上进行搜索,但找不到直接的答案.

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..

业务问题:我们的一位客户希望拥有一个基于Java Web的应用程序,但希望将其作为桌面应用程序启动.也就是说,客户端希望双击其桌面上某个位置的图标,并启动一个浏览器窗口,指向该Web应用程序的URL/上下文,该URL/上下文将在Jetty服务器上本地运行当用户单击该图标时,即可开始使用.

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.

到目前为止,我已经弄清楚了如何打包我的应用程序并创建一个exe启动器(使用install4j)(基本上我创建了一个带有jetty服务器的可执行jar文件,并将我的Web应用程序与一个启动类打包在一起,该启动类将启动Jetty服务器并将其指向Web应用程序的上下文.)

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).

我的问题是我不知道如何启动(本机)Web浏览器并将其指向Web应用程序的URL/上下文.

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));

这很好,但还不够好.而且其效果还不够好的原因是,当用户关闭浏览器时,Jetty服务器仍将在后台运行.我想要的是当用户关闭浏览器时,我也想检测到此事件并关闭Jetty服务器(请不要使用任何活动的X或JavaScript)

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)

有人提到创建一个JPanel并在其中使用一些DJ本机swing API包含本机浏览器,但是我不知道如何执行此操作.

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.

有什么想法吗?

非常感谢

推荐答案

这里是一段代码,仅供参考.您可以在进程中派生本机浏览器,然后将Java Process对象返回到您的Java代码.您可以调用processObject.waitFor()方法来等待所调用的进程终止.

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.

启动器:

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();
        }
    }
}

主要类别:

public class TestProcess {

    public static void main(String[] args) throws Exception {
        new Thread(new Launcher()).run();
    }

}

这篇关于启动和关闭指向Web应用程序的本机浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆