Java - запуск SWT-зависимой программы без установленного SWT - PullRequest
0 голосов
/ 02 июля 2019

У меня есть Java-программа, которая включает в себя org.eclipse.swt библиотеки, такие как «Дисплей» и «Оболочка»

Первое, что делает эта программа:

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

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

Я экспортировалэта программа в исполняемый файл JAR и может нормально запускать ее на моем ПК.

Однако, при попытке запустить ее на ПК без установленного Eclipse, Программа не запускается.Без исключений.Он просто завершает работу и не запускает остальную часть кода.

Я попытался отладить, создав несколько блоков предупреждений, таких как:

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();
}

В итоге я вижу AlertBox "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 enter image description here

Я попытался окружить свой метод, который вызывается в 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>

, и он все еще не работает на ПК, что делаетне установлено затмение

1 Ответ

0 голосов
/ 02 июля 2019

Если вы хотите запустить программу, предназначенную для написания пользовательского интерфейса на языке SWT, лучшим способом является предоставление библиотеки SWT в пути к классам приложения. Это не значит, что вы должны обеспечить полное затмение. Eclipse также просто использует SWT в качестве библиотеки для пользовательского интерфейса. Просто возьмите файлы SWT и поместите их в путь к классам, и программа должна начать представлять вам окно, которое выражается в строке Shell shell = new Shell(display).

Добавление SWT с помощью Maven:

<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", пожалуйста, убедитесь, что все зависимости предоставлены.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...