【问题标题】:Java - Run SWT dependent Program without SWT installedJava - 在未安装 SWT 的情况下运行依赖于 SWT 的程序
【发布时间】:2019-11-13 03:14:51
【问题描述】:

我有一个 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的电脑上运行

【问题讨论】:

    标签: java eclipse swt


    【解决方案1】:

    如果你想运行一个设计为用 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”时,请确保提供所有依赖项。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多