java - 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上运行
解决方案
如果你想运行一个设计为用 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”中时,请确保提供所有依赖项。
推荐阅读
- r - 评估神经网络性能
- sql - 在 SAS 上使用 Group By Proc SQL 对计算变量进行汇总
- excel - VBA代码:单击单元格并使用该单元格名称从模板创建新工作表
- javascript - 单击按钮时,Puppeteer 未按预期工作
- reactjs - 无法设置默认值
组件 - redux 表单 - c++ - 异常处理级别/重新抛出异常
- c - 如何使用 int 作为指针的地址?
- blogger - 博主特别标签
- java - 无法将参数从 maven 命令传递到 Testng Runner
- flutter - flutter run -bash: Desktop/flutter/bin/flutter: 没有这样的文件或目录