java - 在 Eclipse 中使用 Composites with Shell for java

标签 java eclipse maven dependencies swt

我在 eclipse 中有一个 java maven 项目,它使用了一些 Composites,但是我在创建一个 main 方法来启动我的小部件时遇到了麻烦。

我添加了以下依赖:

    <dependency>
        <groupId>org.eclipse.rap</groupId>
        <artifactId>org.eclipse.rap.rwt</artifactId>
        <version>3.0.0-M5</version>
    </dependency>

这是我的主要方法(MainWindow 是我的复合方法)

public static void main(String[] args){
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(450,256);
    shell.setText("My Title");
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    MainWindow window = new MainWindow(shell, SWT.ALL);
    shell.open();
    while(!shell.isDisposed()){
        if(!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
}

如果我尝试从 eclipse 中运行这个应用程序,我会收到以下错误:

Exception in thread "main" java.lang.IllegalStateException: No context available outside of the request processing.
    at org.eclipse.rap.rwt.internal.service.ContextProvider.getContext(ContextProvider.java:95)
    at org.eclipse.rap.rwt.internal.service.ContextProvider.getUISession(ContextProvider.java:183)
    at org.eclipse.rap.rwt.internal.lifecycle.LifeCycleUtil.setSessionDisplay(LifeCycleUtil.java:24)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:275)

如果我在将它编译为 jar 后执行它,我会得到:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

如何启动我的应用程序? 谢谢

更新:

NoClassdefFound 是通过在构建插件中使用 jar-with-dependency 解决的。

但现在在两次执行(cmd 和 eclipse)时都会弹出“无上下文可用”错误

最佳答案

您包括了 SWT 的 Eclipse RAP 版本,它不用于普通的 SWT 应用程序。

删除 RAP 依赖项并仅使用普通 SWT。

关于java - 在 Eclipse 中使用 Composites with Shell for java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28915602/

相关文章:

java - Maven - 在当前项目中找不到前缀为 'tomcat7' 的插件

java - 如何在不刷新的情况下关闭 BufferedWriter?

java - 数据在 Struts2 中是如何传输的?

java - 当 Eclipse 没有自动创建 getter 和 setter 时,如何强制它提示您创建它?

java - 如何调用使用可能未初始化的数组作为参数的方法?

maven - 如何查看 Artifactory 中 Artifact 之间的依赖关系?

java - Spring Integration 5.2.2.RELEASE - 使用 @IntegrationConverter 的集成流转换不适用于 jackson databind

java - 这是如何编写此 <class name> <object name > = new constructor name 的正确方法?

通过 javac 奇怪的错误未找到 Java 符号(通过 Eclipse 运行)

java - 将 Gradle Tomcat 配置转换为 Maven