Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here and here.(注意:本文中的建议仅适用于JxBrowser6。请在此处和此处参阅有关JxBrowser 7的相应文章。)


Each Browser instance must be initialized with BrowserContext. The BrowserContext instance holds the context needed for a browsing session and provides path to the directory where Chromium stores cookies, cache data files, etc.(每个浏览器实例必须使用BrowserContext初始化。 BrowserContext实例保存浏览会话所需的上下文,并提供Chromium存储cookie,缓存数据文件等的目录的路径。)


The following code creates a new Browser instance initialized with default context: (以下代码创建一个使用默认上下文初始化的新Browser实例:)

Browser browser = new Browser();

The code above equals to: (上面的代码等于:)

Browser browser = new Browser(BrowserContext.defaultContext());

The BrowserContext.defaultContext() method returns default BrowserContext that is configured to store Chromium data files such as cookies, cache, etc. in user's temp directory on macOS and Linux and AppData\Local\JxBrowser on Windows. You can get path to the directory where data files are stored using the BrowserPreferences.getDefaultDataDir() method.(BrowserContext.defaultContext()方法返回默认的BrowserContext,默认配置为将Chromium数据文件(例如cookie,缓存等)存储在macOS和Linux上的用户的temp目录中,以及Windows上的AppData \ Local \ JxBrowser中。您可以使用BrowserPreferences.getDefaultDataDir()方法获取数据文件存储目录的路径。)


Two Browser instances with the same BrowserContext instances will use the same user data directory. As result they will share cookies and cache files. For example: (具有相同BrowserContext实例的两个Browser实例将使用相同的用户数据目录,他们将共享cookie和缓存文件。例如:)

BrowserContext context = new BrowserContext(
    new BrowserContextParams("C:\\my-data1"));
Browser browser1 = new Browser(context);
Browser browser2 = new Browser(context);

To create independent Browser instances that don't share cookies and cache data, you must initialize each Browser instance with a different BrowserContext instance configured to use a different data directory. For example: (要创建不共享Cookie和缓存数据的独立浏览器实例,必须为每个浏览器实例配置使用不同数据目录的BrowserContext实例。例如:)

BrowserContext context1 = new BrowserContext(
    new BrowserContextParams("C:\\my-data1"));
Browser browser1 = new Browser(context1);

BrowserContext context2 = new BrowserContext(
    new BrowserContextParams("C:\\my-data2"));
Browser browser2 = new Browser(context2);

To get BrowserContext of the Browser instance you can use the browser.getContext() method.(要获取Browser实例的BrowserContext,可以使用browser.getContext()方法。)


Important to Know(重要须知)

Using several BrowserContext instances configured to use same data directory in a single or multiple Java application instances is forbidden. In this case Browser constructor will throw the BrowserException exception to prevent unexpected behavior or errors such as native crash in the Chromium engine.(禁止在单个或多个Java应用程序实例中使用多个配置为使用相同数据目录的BrowserContext实例,以防止出现异常行为或错误。在这种情况下,浏览器构造函数将抛出BrowserException异常,造成Chromium引擎崩溃。)


If it's possible to launch more than one instance of your Java application at the same time on the same machine, then please don't create Browser instance in your application using default constructor. In this case both Browser instances running in different Java applications at the same time will be configured to use the same Chromium data directory which is forbidden. The BrowserException exception will be thrown when the second Java application instance will try to create a new Browser instance.(如果在同一台计算机上同时启动多个Java应用程序实例,那么请不要使用默认构造函数在您的应用程序中创建Browser实例。在这种情况下,两个在不同Java应用程序中同时运行的浏览器实例将被配置为使用相同的Chromium数据目录,这是禁止的。当第二个Java应用程序实例尝试创建新的Browser实例时,将引发BrowserException异常。)


To solve this issue please make sure that only one instance of your Java application can be used at the same time. If you would like to let end users to run multiple instances of your Java application at the same time, then please make sure that each instance of your Java application configures JxBrowser library to use unique Chromium data directory.(要解决此问题,请确保您的Java应用程序只能同时使用一个实例。如果您想让最终用户同时运行您的Java应用程序的多个实例,请确保您的Java应用程序的每个实例都将JxBrowser库配置为使用唯一的Chromium数据目录。)


Multiple instances of Java application:(Java应用程序的多个实例:)

App instance #1
Browser browser = new Browser(); // OK
 App instance #2
Browser browser = new Browser(); // <= BrowserException
No 
App instance #1
BrowserContext context = new BrowserContext(
    new BrowserContextParams("C:\\data"));
Browser browser = new Browser(context); // OK
 App instance #2
BrowserContext context = new BrowserContext(
    new BrowserContextParams("C:\\data"));
Browser browser = new Browser(context); // <= BrowserException
No 
App instance #1
BrowserContext context = new BrowserContext(
    new BrowserContextParams("C:\\data1"));
Browser browser = new Browser(context);
  App instance #2
BrowserContext context = new BrowserContext(
    new BrowserContextParams("C:\\data2"));
Browser browser = new Browser(context);
Yes 


A single Java application instance:(一个Java应用程序实例:)

Browser browserOne = new Browser(); // OK

String dataDir = BrowserContext.defaultContext().getDataDir();
BrowserContext context = new BrowserContext(
    new BrowserContextParams(dataDir));
Browser browserTwo = new Browser(context); // <= BrowserException
No 
BrowserContext context1 = new BrowserContext(
    new BrowserContextParams("C:\\shared-data"));
Browser browser1 = new Browser(context1); // OK

BrowserContext context2 = new BrowserContext(
    new BrowserContextParams("C:\\shared-data"));
Browser browser2 = new Browser(context2); // <= BrowserException
No 
BrowserContext context1 = new BrowserContext(
    new BrowserContextParams("C:\\my-data1"));
Browser browser1 = new Browser(context1);
BrowserContext context2 = new BrowserContext(
    new BrowserContextParams("C:\\my-data2"));
Browser browser2 = new Browser(context2);
Yes 
Browser browser1 = new Browser();
Browser browser2 = new Browser();
Browser browser3 = new Browser(BrowserContext.defaultContext());
Yes 


Multiple BrowserContext Limitations(多个BrowserContext限制)

At the moment there is a limit on how many BrowserContext instances can be held by one Chromium process. Holding more than that limit can lead to unexpected crashes in Chromium process. The limit is around 50 instances of BrowserContext per process and varies depending on the operating system.(目前,一个Chromium进程可以容纳的BrowserContext实例有限制,超过该限制可能会导致Chromium进程意外崩溃。每个进程的限制约为50个BrowserContext实例,具体取决于操作系统。)


On Linux and MacOS Chromium does not release its file descriptors. That behavior leads to the file descriptors leak and after some point, it becomes impossible to create new BrowserContext instances because of hitting the hard limit of the file descriptors.(在Linux和MacOS上,Chromium不会释放其文件描述符。这种行为会导致文件描述符泄漏,并且在某些时候,由于达到了文件描述符的硬限制,因此无法创建新的BrowserContext实例。)


On Windows, new BrowserContext instances are acquiring thread-local storages from Chromium pool of thread-local storages and never releases them, which may lead to Chromium process crashes when trying to create new BrowserContext.(在Windows上,新的BrowserContext实例将从线程本地存储的Chromium池中获取线程本地存储,并且从不释放它们,这可能会导致在尝试创建新的BrowserContext时Chromium进程崩溃。)


To avoid issues with multiple BrowserContext instances, the following approaches can be used:(为了避免多个BrowserContext实例出现问题,可以使用以下方法:)


1. Multiple Browser instances can use the same BrowserContext. If you do not need to create the Browser instances that don't share cookies, caches and other user data, you can use a single BrowserContext instance. For example:(1.多个浏览器实例可以使用相同的BrowserContext。如果不需要创建不共享Cookie,缓存和其他用户数据的Browser实例,则可以使用单个BrowserContext实例。例如:)

BrowserContext browserContext = new BrowserContext(new BrowserContextParams(
        new File("user_data_dir").getAbsolutePath()));
Browser browserOne = new Browser(browserContext);
Browser browserTwo = new Browser(browserContext);


2. In case you need to use multiple Browser instances that don't share cookies and other user data and you have to create multiple BrowserContext instances, we recommend that you dispose all Browser instances periodically to destroy all created BrowserContext instances, and terminate Chromium native process to release file descriptors and other resources. After that, you can continue creating the new BrowserContext and Browser instances.(2.如果您需要使用多个不共享Cookie和其他用户数据的浏览器实例,并且必须创建多个BrowserContext实例,建议您定期干掉所有Browser实例,以销毁所有创建的BrowserContext实例,并终止Chromium native释放文件描述符和其他资源的过程。之后,您可以继续创建新的BrowserContext和Browser实例。)