这是我用于Poc目的的示例代码...但是我无法在linux环境中前进(如果我不安装libwebkitgtk软件包),但是此代码在Windows中可以正常工作。 linux libwebkitgtk是必需的。
public class SimpleWebBrowserExample {
public static JComponent createContent() {
JPanel contentPane = new JPanel(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("/home/mphs/Softwares/eclipse/Home/Softwares/ews/Platform /src/Sample.html");
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
contentPane.add(webBrowserPanel, BorderLayout.CENTER);
// Create an additional bar allowing to show/hide the menu bar of the web browser.
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible());
menuBarCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED);
}
});
buttonPanel.add(menuBarCheckBox);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
return contentPane;
}
public static void main(String[] args) throws IOException {
NativeInterface.open();
UIUtils.setPreferredLookAndFeel();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(createContent(), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}
}
这是我得到的StackTrace ...
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at chrriis.dj.nativeswing.swtimpl.core.MessagingInterface.processCommandResult(MessagingInterface.java:342)
at chrriis.dj.nativeswing.swtimpl.core.MessagingInterface.syncSend(MessagingInterface.java:329)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.syncSend_(SWTNativeInterface.java:392)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.syncSend(NativeInterface.java:104)
at chrriis.dj.nativeswing.swtimpl.Message.syncSend(Message.java:64)
at chrriis.dj.nativeswing.swtimpl.CommandMessage.syncExec(CommandMessage.java:55)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent.runSync(SWTNativeComponent.java:201)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent.createNativePeer(SWTNativeComponent.java:1006)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent.access$17(SWTNativeComponent.java:989)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent$12.run(SWTNativeComponent.java:882)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent$CMN_createControl.run(SWTNativeComponent.java:663)
at chrriis.dj.nativeswing.swtimpl.CommandMessage.runCommand(CommandMessage.java:61)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.runMessageCommand(NativeInterface.java:172)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.runMessageCommand(SWTNativeInterface.java:1429)
at chrriis.dj.nativeswing.swtimpl.core.MessagingInterface.runMessage(MessagingInterface.java:121)
at chrriis.dj.nativeswing.swtimpl.core.MessagingInterface.processReceivedMessages(MessagingInterface.java:106)
at chrriis.dj.nativeswing.swtimpl.core.MessagingInterface.access$2(MessagingInterface.java:94)
at chrriis.dj.nativeswing.swtimpl.core.MessagingInterface$2$2.run(MessagingInterface.java:442)
at org.eclipse.swt.widgets.RunnableLock.run(Unknown Source)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Unknown Source)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface$OutProcess.runNativeSide(SWTNativeInterface.java:1352)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.main_(SWTNativeInterface.java:1461)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.main(NativeInterface.java:224)
Caused by: org.eclipse.swt.SWTError: No more handles [Unknown Mozilla path (MOZILLA_FIVE_HOME not set)]
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.browser.Mozilla.initMozilla(Unknown Source)
at org.eclipse.swt.browser.Mozilla.create(Unknown Source)
at org.eclipse.swt.browser.Browser.<init>(Unknown Source)
at chrriis.dj.nativeswing.swtimpl.components.core.NativeWebBrowser.createControl(NativeWebBrowser.java:431)
... 19 more
最佳答案
在Ubuntu中安装Weblibs。
命令
须藤apt-get install libwebkitgtk-1.0-0
它为我工作。安装此程序后,重新启动您的应用程序。希望这也会对您有帮助。