1

I have an SWT app running fine within Eclipse. Now, I want to bundle the project as a stand-alone app with Maven:

$ mvn compile assembly:single

I am using this pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>pmu</groupId>
  <artifactId>swtsandbox</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>swtsandbox</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <swt.version>4.6.1</swt.version>
  </properties>

<repositories>
    <repository>
        <id>maven-eclipse-repo</id>
        <url>http://maven-eclipse.github.io/maven</url>
    </repository>
</repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- select prefered one, or move the preferred on to the top: -->

        <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>



<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.6.2</version>
          <configuration>
            <!-- put your configurations here -->
          </configuration>
        </plugin>

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>pmu.swtsandbox.App</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

      </plugins>
    </pluginManagement>
  </build>
</project>

When I run the resulting jar I get:

pm:target pmu$ java -jar swtsandbox-0.0.1-SNAPSHOT-jar-with-dependencies.jar Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons:
    no swt-gtk-4626 in java.library.path
    no swt-gtk in java.library.path
    Can't load library: /Users/pmu/.swt/lib/macosx/x86_64/libswt-gtk-4626.jnilib
    Can't load library: /Users/pmu/.swt/lib/macosx/x86_64/libswt-gtk.jnilib

    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.internal.Converter.wcsToMbcs(Unknown Source)
    at org.eclipse.swt.internal.Converter.wcsToMbcs(Unknown Source)
    at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
    at pmu.swtsandbox.App.main(App.java:14)

Apparently, the Eclipse app is finding the SWT JNI libs, but they are not included in the Maven build.

When I search for the SWT libs, this gets:

/Users/pmu/eclipse/cpp-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/409/0/.cp/libswt-cocoa-4628.jnilib
/Users/pmu/eclipse/cpp-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/409/0/.cp/libswt-pi-cocoa-4628.jnilib
/Users/pmu/eclipse/cpp-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/58/0/.cp/os/macosx/x86_64/libpty.jnilib
/Users/pmu/eclipse/cpp-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/58/0/.cp/os/macosx/x86_64/libspawner.jnilib
/Users/pmu/eclipse/cpp-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/99/0/.cp/os/macosx/libunixfile_1_0_0.jnilib
/Users/pmu/eclipse/java-mars/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/387/0/.cp/libswt-cocoa-4530.jnilib
/Users/pmu/eclipse/java-mars/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/387/0/.cp/libswt-pi-cocoa-4530.jnilib
/Users/pmu/eclipse/java-mars/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/79/0/.cp/os/macosx/libunixfile_1_0_0.jnilib
/Users/pmu/eclipse/java-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/389/0/.cp/libswt-cocoa-4628.jnilib
/Users/pmu/eclipse/java-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/389/0/.cp/libswt-pi-cocoa-4628.jnilib
/Users/pmu/eclipse/java-neon/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/79/0/.cp/os/macosx/libunixfile_1_0_0.jnilib
poseid
  • 6,986
  • 10
  • 48
  • 78
  • 1
    Maven doesn't usually handle files of type `.jnilib`, and does not see them "as dependencies". I can help, but I need to understand first if these files are somewhat bundled within a jar you-re using or they come from somewhere else (the fact that they are present in Eclipse doesn't help the Maven build). Can you (1) run a Maven build like you mentioned above, (2) Search in your project's `target` folder to see if any `.jnilib` is in there? If so, then you could tell your `assembly` plugin to explicitly include those files. P.S. You can remove the `compile` invocation from your cmd line. – Andrei Aug 12 '17 at 16:35
  • i can't see any jnilib in the output part, I've added this repo into github https://github.com/mulderp/swtsandbox - maybe it helps – poseid Aug 12 '17 at 17:13
  • I've looked around a bit and found that `.jnilib` files are bundled in this artifact (which you already have in your project) : https://mvnrepository.com/artifact/org.eclipse.swt/org.eclipse.swt.cocoa.macosx.x86_64/4.3 Are you sure that your final jar doesn't already have these files? It might be just a case of tools not knowing how to take non-class files from jars. If those files are indeed not present, then you can have a look at the `jar-with-dependencies` config at tell it to include static files as well. – Andrei Aug 12 '17 at 17:49
  • 1
    I don't think that my previous comment is quite right (because I've found those files in an artifact like `org.eclipse.swt`, while yours are in a path that contains `org.eclipse.osgi`. The key point now would be to figure out where Eclipse takes those files from (look at eclipse build paths, or possible SDKs involved in this SWT thing), and make Maven use the same files. Unfortunately I'm not familiar at all with SWT. – Andrei Aug 12 '17 at 18:06
  • thanks, still a bit confused about the JNI and SWT dependencies maybe this post on using ant for swt packaging can help other https://stackoverflow.com/questions/2706222/create-cross-platform-java-swt-application – poseid Aug 13 '17 at 10:53
  • 1
    Sorry, unsure if you've managed to solve your problem or not. If not, then could you share which version of SWT_LIB are you using? (according to the SWT_LIB entry here: https://github.com/mulderp/swtsandbox/blob/master/.classpath ) I still believe the key is in figuring out where eclipse takes those libs. I've found this jar with `.jnilib` files whose version is closer to those in your above listing: https://github.com/maven-eclipse/maven-eclipse.github.io/blob/master/maven/org/eclipse/swt/org.eclipse.swt.cocoa.macosx.x86_64/4.6.1/org.eclipse.swt.cocoa.macosx.x86_64-4.6.1.jar – Andrei Aug 13 '17 at 11:28
  • Which SWT jar ends up in your bundle? Your pom has GTK and Cocoa dependencies. Running on macOS, only Cocoa dependency can work. Make sure you don't have the other jar because if that is found first it might cause problems. – user2543253 Aug 14 '17 at 08:31
  • actually I just read about getting the SWT binaries from here: https://www.eclipse.org/swt/git.php in the bundles/ directories are all needed files – poseid Aug 15 '17 at 20:17

0 Answers0