如果正好有这样的需求,那么CEF框架就有了用武之地,该框架其实早已被大量使用了。CEF全称为Chromium Embedded Framework,关于它的介绍可以直接去它的官网查看,这里仅引用维基百科上的第一段话:
The Chromium Embedded Framework (CEF) is an open source framework for embedding a web browser engine based on the Chromium core. It allows developers to add web browser control and implement an HTML5-based layout GUI in a desktop application or to provide web browser capabilities to a software application or game, and provides the infrastructure for developers to add HTML rendering and JavaScript to a C++ project. It also comes with bindings for C, C++, Delphi, Go, Java, .NET / Mono, Visual Basic 6.0, and Python and runs on Linux, Mac OS X and Windows.
而java-cef(简称jcef)则是在CEF基础上为Java程序员和Java应用提供一样嵌入式浏览器的框架。在它的官网上也有相关介绍:
The Java Chromium Embedded Framework (JCEF) is a simple framework for embedding Chromium-based browsers in other applications using the Java programming language.
其本质和CEF一样,只不过已经用Java实现了相关接口,这样java程序员就可以直接使用jcef而就能够实现一些窗口应用下浏览器的集成。
废话不多说,下面直接介绍如何在win10上编译jcef的Release版本。
现在无论是Windows、Mac OS 还是Linux 都支持编译JCEF的源代码,这篇文章主要是讲在Windows下的编译。在编译之前需要有以下一个工具:
工具准备好之后就可以使用Git来下载JCEF的源码,例如下载到D盘下新建的java-cef下:
Alvin Is@DESKTOP-6T33RRO MINGW64 /d
$ cd java-cef
$ git clone https://bitbucket.org/chromiumembedded/java-cef.git src
Alvin Is@DESKTOP-6T33RRO MINGW64 /d/java-cef (master)
$ git clone https://bitbucket.org/chromiumembedded/java-cef.git src
Cloning into 'src'...
remote: Counting objects: 3550, done.
remote: Compressing objects: 100% (1604/1604), done.
remote: Total 3550 (delta 2341), reused 2591 (delta 1682)
Receiving objects: 100% (3550/3550), 27.47 MiB | 908.00 KiB/s, done.
Resolving deltas: 100% (2341/2341), done.
这样在src下就是java-cef的源代码了。
下载之后就需要开始build了,在src目录下创建jcef_build并进入该目录,
$ cd src
$ Alvin Is@DESKTOP-6T33RRO MINGW64 /d/java-cef/src (master)
$ mkdir jcef_build && cd jcef_build
接下来就要生成64位VS2013项目文件,你会看到控制台正在下载:
Alvin Is@DESKTOP-6T33RRO MINGW64 /d/java-cef/src/jcef_build (master)
$ cmake -G "Visual Studio 12 Win64" ..
-- The C compiler identification is MSVC 18.0.40629.0
-- The CXX compiler identification is MSVC 18.0.40629.0
-- Check for working C compiler: D:/Applications/VS2013/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: D:/Applications/VS2013/VC/bin/x86_amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: D:/Applications/VS2013/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: D:/Applications/VS2013/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Downloading D:/java-cef/src/third_party/cef/cef_binary_3.3239.1716.g735b746_windows64.tar.bz2.sha1...
-- Downloading D:/java-cef/src/third_party/cef/cef_binary_3.3239.1716.g735b746_windows64.tar.bz2...
-- [download 0% complete]
-- [download 1% complete]
...
-- [download 100% complete]
但是,有可能会遇到如下的错误:
-- Extracting D:/java-cef/src/third_party/cef/cef_binary_3.3239.1716.g735b746_windows64.tar.bz2...
CMake Error: Problem with archive_read_next_header(): Truncated input file (needed 90666496 bytes, only 0 available)
CMake Error: Problem extracting tar: D:/java-cef/src/third_party/cef/cef_binary_3.3239.1716.g735b746_windows64.tar.bz2
CMake Warning at third_party/cef/cef_binary_3.3239.1716.g735b746_windows64/cmake/cef_variables.cmake:344 (message):
CEF sandbox is not compatible with the current MSVC version (1800)
Call Stack (most recent call first):
third_party/cef/cef_binary_3.3239.1716.g735b746_windows64/cmake/FindCEF.cmake:38 (include)
CMakeLists.txt:153 (find_package)
-- Found PythonInterp: C:/Python26/python.exe (found version "2.6.6")
-- Found JNI: C:/Program Files/Java/jdk1.8.0_151/lib/jawt.lib (Required is at least version "1.7")
-- Generating native/jcef_version.h file...
File native/jcef_version.h is already up to date.
-- Downloading clang-format from Google Storage...
0> Failed to fetch file gs://chromium-clang-format/6ddedd571c56b8c184f30a3c1fc36984e8c10ccd for tools/buildtools/win/clang-format.exe, skipping. [Err: Traceback (most recent call last):
File "D:\java-cef\src\tools\buildtools\gsutil.py", line 160, in <module>
sys.exit(main())
当从Google云存储下载时会报错,不用担心,这是由于D:\java-cef\src\tools\buildtools\download_from_google_storage下会利用到链接gs://来下载,但控制台下貌似无法获取到,因此可以用浏览器打开,将gs://
替换为https://storage.googleapis.com/
,然后在地址栏下输入完整的地址https://storage.googleapis.com/chromium-clang-format/6ddedd571c56b8c184f30a3c1fc36984e8c10ccd
将文件下载到D:\java-cef\src\tools\buildtools\win
目录中,并将其改名为clang-format.exe
,这是再继续执行cmake -G "Visual Studio 12 Win64" ..
就会发现已build完成。
Alvin Is@DESKTOP-6T33RRO MINGW64 /d/java-cef/src/jcef_build (master)
$ cmake -G "Visual Studio 12 Win64" ..
CMake Warning at third_party/cef/cef_binary_3.3239.1716.g735b746_windows64/cmake/cef_variables.cmake:344 (messa ge):
CEF sandbox is not compatible with the current MSVC version (1800)
Call Stack (most recent call first):
third_party/cef/cef_binary_3.3239.1716.g735b746_windows64/cmake/FindCEF.cmake:38 (include)
CMakeLists.txt:153 (find_package)
-- Generating native/jcef_version.h file...
File native/jcef_version.h is already up to date.
-- Downloading clang-format from Google Storage...
0> File tools/buildtools/win/clang-format.exe exists and SHA1 matches. Skipping.
Success!
Downloading 1 files took 0.008000 second(s)
-- *** CEF CONFIGURATION SETTINGS ***
-- Generator: Visual Studio 12 2013 Win64
-- Platform: Windows
-- Project architecture: x86_64
-- Binary distribution root: D:/java-cef/src/third_party/cef/cef_binary_3.3239.1716.g735b746_windows6 4
-- CEF Windows sandbox: OFF
-- Visual Studio ATL support: ON
-- Standard libraries: comctl32.lib;rpcrt4.lib;shlwapi.lib;ws2_32.lib
-- Compile defines: __STDC_CONSTANT_MACROS;__STDC_FORMAT_MACROS;WIN32;_WIN32;_WINDOWS;UNICODE;_UNI CODE;WINVER=0x0601;_WIN32_WINNT=0x601;NOMINMAX;WIN32_LEAN_AND_MEAN;_HAS_EXCEPTIONS=0;CEF_USE_ATL
-- Compile defines (Debug):
-- Compile defines (Release): NDEBUG;_NDEBUG
-- C compile flags: /MP;/Gy;/GR-;/W4;/WX;/wd4100;/wd4127;/wd4244;/wd4481;/wd4512;/wd4701;/wd4702;/ wd4996;/Zi
-- C compile flags (Debug): /MTd;/RTC1;/Od
-- C compile flags (Release): /MT;/O2;/Ob2;/GF
-- C++ compile flags: /MP;/Gy;/GR-;/W4;/WX;/wd4100;/wd4127;/wd4244;/wd4481;/wd4512;/wd4701;/wd4702;/ wd4996;/Zi
-- C++ compile flags (Debug): /MTd;/RTC1;/Od
-- C++ compile flags (Release): /MT;/O2;/Ob2;/GF
-- Exe link flags: /MANIFEST:NO;/LARGEADDRESSAWARE
-- Exe link flags (Debug): /DEBUG
-- Exe link flags (Release):
-- Shared link flags:
-- Shared link flags (Debug): /DEBUG
-- Shared link flags (Release):
-- CEF Binary files: chrome_elf.dll;d3dcompiler_43.dll;d3dcompiler_47.dll;libcef.dll;libEGL.dll;lib GLESv2.dll;natives_blob.bin;snapshot_blob.bin;v8_context_snapshot.bin;swiftshader
-- CEF Resource files: cef.pak;cef_100_percent.pak;cef_200_percent.pak;cef_extensions.pak;devtools_re sources.pak;icudtl.dat;locales
-- *** JCEF CONFIGURATION SETTINGS ***
-- Python executable: C:/Python26/python.exe
-- Java directory: C:/Program Files/Java/jdk1.8.0_151
-- JNI libraries: C:/Program Files/Java/jdk1.8.0_151/lib/jawt.lib;C:/Program Files/Java/jdk1.8.0 _151/lib/jvm.lib
-- JNI include directories: C:/Program Files/Java/jdk1.8.0_151/include;C:/Program Files/Java/jdk1.8.0_151/ include/win32;C:/Program Files/Java/jdk1.8.0_151/include
-- Configuring done
-- Generating done
-- Build files have been written to: D:/java-cef/src/jcef_build
用VS2013打开src/jcef_build/jcef.sln
,然后点击菜单Build下的Configuration Manager修改为Release,然后右击解决方案,选择Build Solution。Build期间可能会出现一些错误,可能是缺少一些依赖,如果遇到这些,可以逐个google排查。
最后就能够得到完整的Release版本,如果不想自己编译的,可以下载我编译好的版本。
D://Release
;src
下的java目录下的文件拷贝到项目中;D://Release
加入到系统的PATH变量中,并重启Eclipse;提示: 若遇到缺少动态链接库,则需要将上面下载的动态链接库文件拷贝到对应的文件中。
那么这样的话,就很容易在一些桌面应用中嵌入一些web前端框架,从而得到一些渲染效果非常棒的框架,从而达到一些模拟、预览等效果。