简体   繁体   English

带有Maven的OSX上的SWT错误

[英]SWT error on OSX with Maven

I made a sample application where I want to test swt under osx using maven. 我制作了一个示例应用程序,我想使用Maven在osx下测试swt。 I've read hundreds of articles and create the display on main thread, but the app throws the same exception. 我已经阅读了数百篇文章,并在主线程上创建了显示内容,但是该应用程序抛出了相同的异常。

Could you check my pom file and my sample application ? 您可以检查我的pom文件和示例应用程序吗?

pom.xml pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>asdf.abcd</groupId>
    <artifactId>b</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example</name>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.swt</groupId>
            <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>
            <version>4.3</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jface</groupId>
            <artifactId>jface</artifactId>
            <version>3.9.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>

                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <arguments>
                        <argument>-d64</argument>
                        <argument>-XstartOnFirstThread</argument>
                        <argument>-classpath</argument>
                    </arguments>
                    <mainClass>standalone.App</mainClass>
                    <addClasspath>true</addClasspath>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>standalone.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

exception: 例外:

    ###Thread name: standalone.App.main() Thread[standalone.App.main(),5,standalone.App] <-- debug message


***WARNING: Display must be created on main thread due to Cocoa restrictions.
[WARNING] 
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 org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.createDisplay(Unknown Source)
    at org.eclipse.swt.widgets.Display.create(Unknown Source)
    at org.eclipse.swt.graphics.Device.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.getDefault(Unknown Source)
    at standalone.App.main(App.java:66)
    ... 6 more

Thanks for the ideas and your time! 感谢您的想法和您的宝贵时间!

在MAc OS X上运行SWT应用程序时,需要指定-XstartOnFirstThread选项。

You are using <goal>java</goal> which runs your app in the same JVM that Maven is running in. That means you can't pass JVM arguments when your app is launched because a new JVM is not being created. 您正在使用<goal>java</goal> ,它在运行Maven的同一个JVM中运行您的应用程序。这意味着您无法在启动应用程序时传递JVM参数,因为没有创建新的JVM。

You have two options: 您有两种选择:

1. Pass JVM arguments to the JVM that runs Maven (and which therefore also eventually runs your app). 1.将 JVM参数传递给运行Maven的JVM(因此最终也会运行您的应用程序)。 On the Mac, this is accomplished by setting the MAVEN_OPTS environment variable: 在Mac上,这是通过设置MAVEN_OPTS环境变量来完成的:

export MAVEN_OPTS="-XstartOnFirstThread`
(then run Maven)

In this case you can remove all the <arguments> from your <configuration> section as they are unnecessary and have no effect. 在这种情况下,您可以从<configuration>部分中删除所有<arguments> ,因为它们是不必要的并且没有效果。 You can also remove <addClasspath> . 您也可以删除<addClasspath>

2. Use <goal>exec</goal> which will run java as a regular shell command, and you can pass all the JVM configuration arguments you want to it: 2.使用<goal>exec</goal>它将作为常规shell命令运行java ,并且您可以将所需的所有JVM配置参数传递给它:

<goals>
  <goal>exec</goal>
</goals>

In this case you do need all the things you have in <arguments> as well as your actual main class, because it's just another argument to the JVM being run as a shell command. 在这种情况下,您确实需要<arguments>中的所有内容以及实际的主类,因为这只是JVM作为shell命令运行的另一个参数。 You can also add your classpath in a slightly simpler way: 您还可以以稍微简单的方式添加类路径:

<executable>java</executable>
<arguments>
   <argument>-XstartOnFirstThread</argument>
   <argument>-classpath</argument>
   <classpath/>
   <argument>standalone.App</argument>
</arguments>

This second option is generally more robust because you don't have to deal with Maven doing weird things to the JVM before running your app. 第二个选项通常更健壮,因为在运行应用程序之前,您不必处理Maven对JVM做奇怪的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM