Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取异常无法在jxbrowser中获取浏览器频道_Java_Url_Jxbrowser - Fatal编程技术网

Java 获取异常无法在jxbrowser中获取浏览器频道

Java 获取异常无法在jxbrowser中获取浏览器频道,java,url,jxbrowser,Java,Url,Jxbrowser,我正在使用jxbrowser 我想使用browser.saveWebpage下载URL列表 public class Downloader{ public Downloader(String url) { System.setProperty("teamdev.license.info", "true"); LoggerProvider.getChromiumProcessLogger().setLevel(Level.OFF);

我正在使用jxbrowser

我想使用browser.saveWebpage下载URL列表

public class Downloader{
public Downloader(String url) {
           System.setProperty("teamdev.license.info", "true");
           LoggerProvider.getChromiumProcessLogger().setLevel(Level.OFF);
           Browser browser = new Browser(BrowserType.LIGHTWEIGHT);

         browser.addLoadListener(new LoadAdapter() {
             @Override
             public void onFinishLoadingFrame(FinishLoadingEvent event) {
                 if (event.isMainFrame()){
                      String filePath = "D:\\Downloads\\index"+System.currentTimeMillis()+".html";
                      String dirPath = "D:\\Downloads\\resources";
                      event.getBrowser().saveWebPage(filePath, dirPath, SavePageType.ONLY_HTML);
                 }
             }
         });
        browser.loadURL(url);
        if(!browser.isLoading())
        {
            browser.stop());
        }
    }


public static void main(String args[])
{
      JxBrowserDemo jxBrowserDemo=null;
        String yourInputFile="D:/file.txt";
        ArrayList<String> lines=getUrls(yourInputFile); //read urls from file 

for(int i=0; i<lines.size(); i++)
        {  
             Thread.sleep(5000);
             jxBrowserDemo=new JxBrowserDemo(lines.get(i));
        }
}   
公共类下载程序{
公共下载程序(字符串url){
System.setProperty(“teamdev.license.info”,“true”);
LoggerProvider.getChromiumProcessLogger().setLevel(Level.OFF);
Browser Browser=新浏览器(BrowserType.LIGHTWEIGHT);
addLoadListener(新的LoadAdapter(){
@凌驾
FinishLoadingFrame上的公共无效(FinishLoadingEvent事件){
if(event.isMainFrame()){
String filePath=“D:\\Downloads\\index”+System.currentTimeMillis()+“.html”;
String dirPath=“D:\\Downloads\\resources”;
event.getBrowser().saveWebPage(文件路径、目录路径、SavePageType.ONLY_HTML);
}
}
});
loadURL(url);
如果(!browser.isLoading())
{
browser.stop());
}
}
公共静态void main(字符串参数[])
{
JxBrowserDemo JxBrowserDemo=null;
字符串yourInputFile=“D:/file.txt”;
ArrayList lines=getUrls(yourInputFile);//从文件中读取URL
对于(int i=0;i“获取浏览器浏览器通道969失败”消息很可能表示由于某种原因,Chromium引擎无法创建浏览器实例并为其打开通道。
Chromium engine可能对同时创建的浏览器实例的数量有一些限制。这些限制可能取决于内存大小、环境配置等。由于您已经创建了968个浏览器实例,而没有对它们进行处理,这可能是此异常的原因。
另外,请注意,您存在内存泄漏,因为您为每个URL创建了新的浏览器实例,并且在此URL浏览器实例处理其URL后未调用
dispose()
方法

browser.stop();
行是不必要的。 在调用saveWebPage()方法之后,您应该等待网页被保存,然后处理浏览器实例。 请查看下面的示例代码:

                final int maxWaitingTime = 10000;
                final int sleepTime = 50;
                int currentWaitingTime = 0;
                File indexHTML = new File(filePath);
                File resourcesFolder = new File(dirPath);
                while (!indexHTML.exists() || !resourcesFolder.exists()) {
                    TimeUnit.MILLISECONDS.sleep(sleepTime);
                    currentWaitingTime += sleepTime;
                    if (currentWaitingTime == maxWaitingTime)
                        throw new RuntimeException(new TimeoutException("The web page could not be saved."));
                }
                event.getBrowser().dispose();

感谢您的快速回复,但我添加了browser.stop以停止创建的浏览器。根据stop()方法文档中,此方法取消任何挂起的导航或下载操作,并停止任何动态页面元素,如背景声音和动画。此方法不处理浏览器对象,在您处理它之前,它将存在于内存中。这会导致内存泄漏。您应该在不需要它时处理浏览器实例为了正确运行上面的程序,我应该对代码做什么修改?