如何从SWT应用程序运行SWT应用程序?

时间:2012-09-05 10:34:56

标签: java swt

我有两个SWT应用程序。从app 1开始,我打电话给app2:

Runtime.getRuntime().exec("App2.bat");

App2已运行但在显示时会抛出异常:

java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: 
    no swt-win32-3740 in java.library.path
    no swt-win32 in java.library.path
    C:\Documents and Settings\Tran Dinh Thoai\.swt\lib\win32\x86\swt-win32-3740.dll: The referenced assembly is not installed on your system
    Can't load library: C:\Documents and Settings\Tran Dinh Thoai\.swt\lib\win32\x86\swt-win32.dll

    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
    at com.escapenotes.updater.Checker.run(Checker.java:58)
    at com.escapenotes.updater.Runner.main(Runner.java:20)

当我从命令行调用app2(运行App2.bat)时,它运行良好,但从app1运行时抛出异常。如何从app1运行app2?

这是App2.bat的内容:

  @echo off
  set JH=C:\Program Files\Java\jre6
  set CP=C:\Data\Projects\EscapeNotes\extensions\Updater\bin
  set XNP="%CP%";"%CP%\commons-logging-1.1.1.jar";"%CP%\aws-java-sdk-1.0.007.jar";"%CP%\commons-codec-1.4.jar";"%CP%\commons-httpclient-3.0.1.jar";"%CP%\jsoup-1.6.2.jar";"%CP%\log4j-1.2.8.jar";"%CP%\text.jar";"%CP%\jface.jar";"%CP%\jfacetext.jar";"%CP%\osgi.jar";"%CP%\runtime.jar";"%CP%\swt.jar"
  "%JH%\bin\java.exe" -cp %XNP% com.escapenotes.updater.Runner %1

当我添加-Djava.library.path

@echo off
set JH=C:\Program Files\Java\jre6
set CP=C:\Data\Projects\EscapeNotes\extensions\Updater\bin
set XNP="%CP%";"%CP%\commons-logging-1.1.1.jar";"%CP%\aws-java-sdk-1.0.007.jar";"%CP%\commons-codec-1.4.jar";"%CP%\commons-httpclient-3.0.1.jar";"%CP%\jsoup-1.6.2.jar";"%CP%\log4j-1.2.8.jar";"%CP%\text.jar";"%CP%\jface.jar";"%CP%\jfacetext.jar";"%CP%\osgi.jar";"%CP%\runtime.jar";"%CP%\swt.jar"
"%JH%\bin\java.exe" -Djava.library.path="C:\Data\Projects\EscapeNotes\extensions\Updater\bin" -cp %XNP% com.escapenotes.updater.Runner %1

虽然有C:\ Data \ Projects \ EscapeNotes \ extensions \ Updater \ bin \ swt-win32-3740.dll,但我有例外:

java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: 
    C:\Data\Projects\EscapeNotes\extensions\Updater\bin\swt-win32-3740.dll: The referenced assembly is not installed on your system
    no swt-win32 in java.library.path
    C:\Documents and Settings\Tran Dinh Thoai\.swt\lib\win32\x86\swt-win32-3740.dll: The referenced assembly is not installed on your system
    Can't load library: C:\Documents and Settings\Tran Dinh Thoai\.swt\lib\win32\x86\swt-win32.dll

    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
    at com.escapenotes.updater.Checker.run(Checker.java:58)
    at com.escapenotes.updater.Runner.main(Runner.java:20)

2 个答案:

答案 0 :(得分:0)

从异常看起来,swt-win32库不是由java.library.path环境变量定义的。在您的“App2.bat”中,请通过-D选项提供,如:

java -Djava.library.path=[path to your library]

答案 1 :(得分:0)

在SWT源的代码段项目中查看SnippetLauncher。它演示了如何以编程方式启动片段。

https://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/SnippetLauncher.java

上述工作跨平台btw。我在Linux和Windows上使用过它。

要尝试的另一件事是复制已终止代码段的运行配置。在调试视图中,查看已终止运行的属性,它显示用于运行代码段的命令。 (我在Gtk上这样做,不确定它是否适用于Windows)。

如果能为您解决问题或者您还有其他问题,请告诉我。