Java杀死弹出进程

发布于 2024-12-04 07:03:17 字数 1519 浏览 1 评论 0原文

我现在正在编写一个简单的程序来模拟真实用户上网冲浪。但是,如果屏幕上弹出任何弹出窗口或程序,我需要最小化或关闭它,以防止它与错误的程序交互。所以我必须监视正在运行的进程或检测新进程?我尝试使用 Javascript,但它需要 IE 中的 ActiveX,并且存在很大问题。有人可以建议我如何开始编写这部分代码吗?我用谷歌搜索了一下,它说那些进程 window.opener.close() 或 Process.Destroy() 命令。谢谢。下面是我的代码的简单草稿。

package javaapplication1;
import java.util.Random;

public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            Random rand = new Random();
            int n = 1 + rand.nextInt(3);
            if (n == 1)
            {
                String myURL = "www.facebook.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
            if (n == 2)
            {
                String myURL = "www.google.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
            if (n == 3)
            {
                String myURL = "www.yahoo.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
        }
        catch(Exception e)
        {
        }
    }
}

I'm now writing a simple program to simulate real user surfing web. However, if there is any popup or program pop up on the screen, I need to either minimize or close it to prevent it from interacting with the wrong program. So I have to monitor the running process or detect for new process? I tried using Javascript but it requires ActiveX in IE and quite problematic. Can someone kindly advise me on how to start on writing that part of the code? I googled for a bit and it says those process window.opener.close() or Process.Destroy() commands. Thanks. Below is a simple draft of my code.

package javaapplication1;
import java.util.Random;

public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            Random rand = new Random();
            int n = 1 + rand.nextInt(3);
            if (n == 1)
            {
                String myURL = "www.facebook.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
            if (n == 2)
            {
                String myURL = "www.google.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
            if (n == 3)
            {
                String myURL = "www.yahoo.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
        }
        catch(Exception e)
        {
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2024-12-11 07:03:17

这是不可能的。 Desktop.browse 启动默认浏览器来显示 URI。浏览器显示的文档 (html) 内容可能会导致浏览器打开新窗口或执行您在浏览器中手动浏览该 URI 时浏览器可能执行的任何其他操作。

大多数情况下,浏览器不会创建新进程。无论它创建一个新进程还是仅创建一个窗口,它都不会向您的调用程序提供任何通知;所以你的调用程序无法控制这些窗口。

您可以使用嵌入式浏览器执行类似的操作。

This cannot be done. Desktop.browse Launches the default browser to display the URI. The content of the document (html) that browser displays may cause the browser to open a new window or do anything else that a browser might do when you browse that URI manually in your browser.

In most cases, the browser will not create a new process. Irrespective of whether it creates a new process or only a window, it will not provide any notification to your calling program; so your calling program has no way of controlling those windows.

You may be able to do something like this by using an embedded browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文