首页 > 解决方案 > Java - 在未安装 SWT 的情况下运行依赖于 SWT 的程序

问题描述

我有一个 java 程序,其中包括org.eclipse.swt像“Display”和“Shell”这样的库

该程序首先要做的事情之一是:

private void authenticationFlow() {
    Display display = new Display();

    Shell shell = new Shell(display);
    final Browser browser;
    //some other code here
}

我将此程序导出到一个可运行的 jar 文件中,并且可以在我的 PC 上运行它。

但是,当试图在没有安装 eclipse 的 PC 上运行它时,程序不会启动。没有任何例外。它只是退出并且不运行其余代码。

我试图通过创建一堆这样的警报框来进行调试:

private void authenticationFlow() {
    popAlertBox(AlertType.INFORMATION, "Nice", "Something happened", "Starting auth");
    Display display = null;
    try {
        display = new Display();
    } catch (Exception e) {
        popAlertBox(AlertType.ERROR, "Oh oh", "Something went wrong", e.getMessage());
    }
    popAlertBox(AlertType.INFORMATION, "Nice", "Something happened", "created display");
    Shell shell = new Shell(display);
    popAlertBox(AlertType.INFORMATION, "Nice", "Something happened", "created shell");
    final Browser browser;
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    try {
        shell.setLayout(gridLayout);
    } catch (Exception e) {
        popAlertBox(AlertType.ERROR, "Shell error", "Could not instantiate Shell: ", e.getMessage());
    }
    //rest of code

private void popAlertBox(AlertType type, String title, String header, String contentText) {
    Alert alert = new Alert(type);
    alert.setTitle(title);
    alert.setHeaderText(header);
    alert.setContentText(contentText);
    alert.setX(GUIMain.getStageX() + GUIMain.getWidth()*0.4);
    alert.setY(GUIMain.getStageY()+ GUIMain.getHeight()*0.4);
    alert.showAndWait();
}

我最终看到了“Starting auth”AlertBox,就是这样,程序立即退出。我从来没有到达“创建的显示”警报框。

所以我认为它与 SWT 本身有关。

现在我的问题是

1. Is this really directly related to SWT or am I misunderstanding something.
2. If it is, how can I have this program run on a PC without eclipse installed?

编辑:

我对所有依赖项使用 maven 这里是我的库的图像,包括 swt 在此处输入图像描述

我尝试过围绕我的方法,该方法在 try catch 中调用,如下所示:

        try{
            authenticationFlow();
        }catch (Exception e) {
            popAlertBox(AlertType.ERROR, "oh oh", "Something went wrong", e.getMessage());
        }
        popAlertBox(AlertType.INFORMATION, "Nice", "Something happened", "If you see this then something is in fact happening. final popup");

并且没有显示这两个弹出窗口。不是 catch 块内的那个,也不是后面的那个。

我在 pom.xml 中添加了以下依赖项:

    <dependency>
        <groupId>org.eclipse</groupId>
        <artifactId>swt</artifactId>
        <version>3.3.0-v3346</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.swt</groupId>
        <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
        <version>4.3</version>
    </dependency>

它仍然没有在没有安装eclipse的pc上运行

标签: javaeclipseswt

解决方案


如果你想运行一个设计为用 SWT 编写的 ui 的程序,最好的方法是在应用程序的类路径中提供 SWT 库。这并不意味着您必须提供整个日食。Eclipse 也只是使用 SWT 作为 ui 的库。只需抓住 SWT jar 并将它们放到类路径中,程序就会开始向您呈现一个窗口,该窗口在该行中表示Shell shell = new Shell(display)

使用 Maven 添加 SWT:

<properties>
  <swt.version>4.3</swt.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.eclipse.swt</groupId>
        <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
        <version>${swt.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.swt</groupId>
        <artifactId>org.eclipse.swt.win32.win32.x86</artifactId>
        <version>${swt.version}</version>
        <!-- To use the debug jar, add this -->
        <classifier>debug</classifier>
    </dependency>       
    <dependency>
        <groupId>org.eclipse.swt</groupId>
        <artifactId>org.eclipse.swt.gtk.linux.x86</artifactId>
        <version>${swt.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.swt</groupId>
        <artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
        <version>${swt.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.swt</groupId>
        <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>
        <version>${swt.version}</version>
    </dependency>
</dependencies>

这将为每个平台添加 SWT。你也可以只添加你需要构建的那个。

将应用程序构建到“fat-jar”中时,请确保提供所有依赖项。


推荐阅读