混合 AWT 和 SWT 时出现 Java UnsatisfiedLinkError?

发布于 2024-07-30 04:41:11 字数 2769 浏览 3 评论 0原文

我是 Eclipse 新手,我正在尝试构建混合 AWT/SWT 应用程序。 这是我的代码:

public class HelloWorldSWT {

    public static void main(String[] args) {
        Frame frame = new Frame("My AWT Frame"); // java.awt.Frame
        frame.setLayout( new BorderLayout() );
        Canvas canvas = new Canvas(); // java.awt.Canvas
        frame.add(canvas, BorderLayout.CENTER);
        frame.setVisible(true);

        Display display = new Display(); // display object to manage SWT lifecycle.
        Shell swtShell = SWT_AWT.new_Shell(display, canvas);
        Button m_button = new Button(swtShell, SWT.PUSH);
        m_button.setText( "button" );

        // invoke the AWT frame rendering by making the frame visible
        // This starts the EDT
        frame.setVisible(true);

        // standard SWT dispatch loop
        while(!swtShell.isDisposed())
        {
            if(!display.readAndDispatch())
                display.sleep();
        }
        swtShell.dispose();
    }
}

编译正常,但是当我在 Eclipse 中将其作为应用程序运行时,出现以下错误:

线程“main”中出现异常 java.lang.UnsatisfiedLinkError: sun.awt.SunToolkit.getAppContext(Ljava/lang/Object;)Lsun/awt/AppContext; 在 sun.awt.SunToolkit.getAppContext(本机 方法)在 sun.awt.SunToolkit.targetToAppContext(未知 来源)于 sun.awt.windows.WComponentPeer.postEvent(未知 来源)于 sun.awt.windows.WComponentPeer.postPaintIfNecessary(未知 来源)于 sun.awt.windows.WComponentPeer.handlePaint(未知 来源)于 sun.java2d.d3d.D3DScreenUpdateManager.repaintPeerTarget(未知 来源)于 sun.java2d.d3d.D3DScreenUpdateManager.createScreenSurface(未知 来源)于 sun.awt.windows.WComponentPeer.replaceSurfaceData(未知 来源)于 sun.awt.windows.WComponentPeer.replaceSurfaceData(未知 来源)于 sun.awt.windows.WComponentPeer.setBounds(未知 来源)于 sun.awt.windows.WWindowPeer.setBounds(未知 来源)于 sun.awt.windows.WComponentPeer.initialize(未知 来源)于 sun.awt.windows.WCanvasPeer.initialize(未知 来源)于 sun.awt.windows.WPanelPeer.initialize(未知 来源)于 sun.awt.windows.WWindowPeer.initialize(未知 来源)于 sun.awt.windows.WFramePeer.initialize(未知 来源)于 sun.awt.windows.WComponentPeer.(未知 来源)于 sun.awt.windows.WCanvasPeer。(未知 来源)于 sun.awt.windows.WPanelPeer.(未知 来源)于 sun.awt.windows.WWindowPeer.(未知 来源)于 sun.awt.windows.WFramePeer。(未知 来源)于 sun.awt.windows.WToolkit.createFrame(未知 来源)于 java.awt.Frame.addNotify(未知 来源)于 java.awt.Window.show(来源未知) 在 java.awt.Component.show(未知 来源)于 java.awt.Component.setVisible(未知 来源)于 java.awt.Window.setVisible(未知 来源)于 HelloWorldSWT.main(HelloWorldSWT.java:20)

我做错了什么?

I'm an Eclipse newbie and I'm trying to build a mixed AWT/SWT application. Here's my code:

public class HelloWorldSWT {

    public static void main(String[] args) {
        Frame frame = new Frame("My AWT Frame"); // java.awt.Frame
        frame.setLayout( new BorderLayout() );
        Canvas canvas = new Canvas(); // java.awt.Canvas
        frame.add(canvas, BorderLayout.CENTER);
        frame.setVisible(true);

        Display display = new Display(); // display object to manage SWT lifecycle.
        Shell swtShell = SWT_AWT.new_Shell(display, canvas);
        Button m_button = new Button(swtShell, SWT.PUSH);
        m_button.setText( "button" );

        // invoke the AWT frame rendering by making the frame visible
        // This starts the EDT
        frame.setVisible(true);

        // standard SWT dispatch loop
        while(!swtShell.isDisposed())
        {
            if(!display.readAndDispatch())
                display.sleep();
        }
        swtShell.dispose();
    }
}

This compiles fine, but when I run it as application in Eclipse, I get the following error:

Exception in thread "main"
java.lang.UnsatisfiedLinkError:
sun.awt.SunToolkit.getAppContext(Ljava/lang/Object;)Lsun/awt/AppContext;
at
sun.awt.SunToolkit.getAppContext(Native
Method) at
sun.awt.SunToolkit.targetToAppContext(Unknown
Source) at
sun.awt.windows.WComponentPeer.postEvent(Unknown
Source) at
sun.awt.windows.WComponentPeer.postPaintIfNecessary(Unknown
Source) at
sun.awt.windows.WComponentPeer.handlePaint(Unknown
Source) at
sun.java2d.d3d.D3DScreenUpdateManager.repaintPeerTarget(Unknown
Source) at
sun.java2d.d3d.D3DScreenUpdateManager.createScreenSurface(Unknown
Source) at
sun.awt.windows.WComponentPeer.replaceSurfaceData(Unknown
Source) at
sun.awt.windows.WComponentPeer.replaceSurfaceData(Unknown
Source) at
sun.awt.windows.WComponentPeer.setBounds(Unknown
Source) at
sun.awt.windows.WWindowPeer.setBounds(Unknown
Source) at
sun.awt.windows.WComponentPeer.initialize(Unknown
Source) at
sun.awt.windows.WCanvasPeer.initialize(Unknown
Source) at
sun.awt.windows.WPanelPeer.initialize(Unknown
Source) at
sun.awt.windows.WWindowPeer.initialize(Unknown
Source) at
sun.awt.windows.WFramePeer.initialize(Unknown
Source) at
sun.awt.windows.WComponentPeer.(Unknown
Source) at
sun.awt.windows.WCanvasPeer.(Unknown
Source) at
sun.awt.windows.WPanelPeer.(Unknown
Source) at
sun.awt.windows.WWindowPeer.(Unknown
Source) at
sun.awt.windows.WFramePeer.(Unknown
Source) at
sun.awt.windows.WToolkit.createFrame(Unknown
Source) at
java.awt.Frame.addNotify(Unknown
Source) at
java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown
Source) at
java.awt.Component.setVisible(Unknown
Source) at
java.awt.Window.setVisible(Unknown
Source) at
HelloWorldSWT.main(HelloWorldSWT.java:20)

What am I doing wrong?

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

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

发布评论

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

评论(5

诗笺 2024-08-06 04:41:11

如前所述,这表明 JVM 找不到本机库。 由于您正在混合 AWT/SWT,我假设 JVM 无法找到 SWT 库(对于 Windows 为 .dll,对于 Linux 为 .so,对于 Mac 不确定)。 我偏向于使用系统属性来告诉java去哪里寻找。

-Djava.library.path=<absolute path to the .dll/.so that SWT needs>

As mentioned, this indicates that the JVM cannot find a native library. Since you're mixing AWT/SWT I assume that the JVM can't find the SWT libraries (.dll for windows, .so for linux, not sure for mac). I'm partial to using an system property to tell java where to look.

-Djava.library.path=<absolute path to the .dll/.so that SWT needs>
人间不值得 2024-08-06 04:41:11

我遇到了完全相同的问题,只能通过完全删除并重新安装 Java 来解决。 似乎不知何故,其中一个包含本机 AWT 方法的 DLL 已经搞砸了。

I have had exactly the same problem, and could only get it resolved by completely removing and re-installing Java. It seems that somehow, one of the DLLs, containing the native AWT methods, had managed to get screwed up.

追我者格杀勿论 2024-08-06 04:41:11

UnsatisfiedLinkError 表示当您尝试运行应用程序时找不到您所依赖的本机库。 如果您在 IDE 中编译它,则该库位于您的构建路径中。 如果您使用 IDE 运行此程序并收到此错误,则说明该库不在您的运行路径中。 检查“运行”对话框以查看构建路径中的库是否位于运行路径中。

The UnsatisfiedLinkError is indicating that a native library that you are relying upon is not found when you are trying to run your app. If you are compiling this in your IDE, then the library is in your build path. If you are running this from with your IDE and getting this error, the libray is not in your Run path. Check your Run Dialog to see that the libraries you have in your build path are in your run path.

辞旧 2024-08-06 04:41:11

从版本 3.3 开始,SWT 自动查找其所需的特定于平台的库,这些库位于 swt.jar 内(位于 JAR 内容的顶层)。 因此,您所需要的只是类路径中的 swt.jar,它就可以工作了。

获得 UnsatisifiedLinkError 的一种方法是,如果您在另一个平台上使用 swt.jar —— 它们都被命名为“swt.jar”; 例如,如果您下载适用于 Linux 的版本,然后尝试在 Windows 上使用它。 该项目将编译正常,因为每个平台的所有 API 级代码都是相同的 Java,但是当您运行时它将失败,因为本机库是错误的。

但是,由于本例中的错误发生在 AWT 中,因此可能是其他原因,与 SWT 没有直接关系。 首先,确保您已下载 Windows SWT 版本。 当您将其导入工作区时,它会创建一个名为 org.eclipse.swt 的 Eclipse 项目,其中包含 swt.jar。 然后,您将 org.eclipse.swt 作为您的项目所需的项目,除了有效、干净的 JRE 之外,构建路径中没有其他任何内容(您可以尝试定义一个新的 [Window -> Preferences -> Java -> Installed JRE],或者仅使用您可能已安装的其他 JRE)。

您还可以从 shell/命令窗口测试它。 转到您的项目目录。 命令应该很简单:

java -cp bin;..\org.eclipse.swt\swt.jar HelloWorldSWT

我让你的代码运行(Vista-32,JDK 6_15),但窗口打开的非常小,并且不会关闭。 不过我对 SWT-AWT 桥一无所知,祝你好运......

Since version 3.3, SWT automatically finds its required platform-specific libraries, which are inside the swt.jar (at the top level of the JAR contents). So all you need is swt.jar in the classpath, and it works.

One way to get an UnsatisifiedLinkError is if you're using a swt.jar for another platform -- they're all named "swt.jar"; for example, if you download the one for Linux, and try to use it on Windows. The project will compile OK since all the API-level code is the same Java for every platform, but it will fail when you run because the native libraries are wrong.

However, since the error in this case happens in AWT, it might be something else, not directly related to SWT. First, make sure you've downloaded the Windows SWT release. When you import it into your workspace, it creates an Eclipse project named org.eclipse.swt, which contains the swt.jar. You then make org.eclipse.swt a required project for your project, and nothing else in the Build Path besides a valid, clean JRE (you can try defining a new one [Window -> Preferences -> Java -> Installed JREs], or just use a different one you might have installed).

You can also test it from the shell/command window. Go to your project directory. The command should be as simple as:

java -cp bin;..\org.eclipse.swt\swt.jar HelloWorldSWT

I got your code to run (Vista-32, JDK 6_15), but the window opened really small, and would not close. I don't know anything about the SWT-AWT bridge though, so good luck with that....

つ可否回来 2024-08-06 04:41:11

我已经解决了这个问题,
我查了很久这个资料,但找不到解决我的问题,
检查我的java版本后,

我发现jdk和jre版本不同,
例如,

我发现我的文件系统上有一个额外的文档

jdk 1.4 jre 1.4 jre 1.5(extra)

你应该删除新版本的 jre(jre1.5)

那么好吧,我的英语很差,我希望我的消息能起到作用青睐,
如果您有任何疑问,可以联系我,电子邮件:[电子邮件受保护]

I have had solved this problem,
I search this information for a long time,but i can't find one to solve my problem,
after i check my java version

i found that jdk and jre version is different,
for example

i found that one extra document on my file system

jdk 1.4 jre 1.4 jre 1.5(extra)

you should delete the new version of the jre(jre1.5)

then ok,my english is poor,i hope my message will do a favour,
if you have some question ,you can connect to me,email:[email protected]

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