swt libraries cannot be resolved
Asked Answered
O

2

6

I am using below maven dependency to develop swt application.

<dependency>
  <groupId>org.eclipse.platform</groupId>
  <artifactId>org.eclipse.swt</artifactId>
  <version>3.108.0</version>
</dependency>

But when I tried to import below packages,

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

I am getting the error like "The import org.eclipse cannot be resolved"

What is the right maven dependency to work with swt Desktop application?

Outpouring answered 10/10, 2018 at 14:33 Comment(0)
S
6

If you need to solve the SWT dependencies with Maven for Windows, Linux and Mac you could use a technique described by Olivier Cailloux here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=520337#c0:

The main problem is the dependency of the platform specific builds (for example, org.eclipse.swt.gtk.linux.x86_64) to some generic build (org.eclipse.swt), which in turn depends on platform specific builds [...]

I believe the strategy suggested at https://github.com/oliviercailloux/java-course/tree/master/SWT#maven, namely, excluding org.eclipse.platform:org.eclipse.swt, is better than those suggested in bug 510186 comment 12. Excluding org.eclipse.platform:org.eclipse.swt conveys the intent better (this dependency is not useful and triggers some problems, so let’s tell Maven that we don’t really need it)

Basically you need to set the corresponding Maven profile for the correct OS and insert this in your pom:

<profiles>
   <profile>
      <id>swt-unix</id>
      <activation>
         <os>
            <family>unix</family>
         </os>
      </activation>
      <properties>
         <swt.artifactId>org.eclipse.swt.gtk.linux.x86_64</swt.artifactId>
         <env>linux</env>
      </properties>
      <dependencies>
         <dependency>
            <groupId>org.eclipse.platform</groupId>
            <artifactId>${swt.artifactId}</artifactId>
            <version>3.108.0</version>
            <optional>true</optional>
            <exclusions>
               <exclusion>
                  <groupId>org.eclipse.platform</groupId>
                  <artifactId>org.eclipse.swt</artifactId>
               </exclusion>
            </exclusions>
         </dependency>
      </dependencies>
   </profile>
   <profile>
      <id>swt-mac</id>
      <activation>
         <os>
            <family>mac</family>
         </os>
      </activation>
      <properties>
         <swt.artifactId>org.eclipse.swt.cocoa.macosx.x86_64</swt.artifactId>
         <env>mac</env>
      </properties>
      <dependencies>
         <dependency>
            <groupId>org.eclipse.platform</groupId>
            <artifactId>${swt.artifactId}</artifactId>
            <version>3.108.0</version>
            <optional>true</optional>
            <exclusions>
               <exclusion>
                  <groupId>org.eclipse.platform</groupId>
                  <artifactId>org.eclipse.swt</artifactId>
               </exclusion>
            </exclusions>
         </dependency>
      </dependencies>
   </profile>
   <profile>
      <id>swt-windows</id>
      <activation>
         <os>
            <family>windows</family>
         </os>
      </activation>
      <properties>
         <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId>
         <env>windows</env>
      </properties>
      <dependencies>
         <dependency>
            <groupId>org.eclipse.platform</groupId>
            <artifactId>${swt.artifactId}</artifactId>
            <version>3.108.0</version>
            <optional>true</optional>
            <exclusions>
               <exclusion>
                  <groupId>org.eclipse.platform</groupId>
                  <artifactId>org.eclipse.swt</artifactId>
               </exclusion>
            </exclusions>
         </dependency>
      </dependencies>
   </profile>
</profiles>
Slattery answered 10/10, 2018 at 22:31 Comment(0)
S
1

You haven't specified which repository you're trying to pull from, but SWT is not available from Maven Central. The simplest method that I know is to use https://github.com/maven-eclipse/maven-eclipse.github.io.

You can add the repository:

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

And then the platform-specific dependency that you need to support. For example:

<dependency>
    <groupId>org.eclipse.swt</groupId>
    <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
    <version>${swt.version}</version>
</dependency>
Spinster answered 10/10, 2018 at 14:56 Comment(5)
Hi avijak, Thanks for the reply, 'org.eclipse.swt.win32.win32.x86_64' artifact is old, Can't I use the artifact 'org.eclipse.swt'?Outpouring
@HariKrishna I'm not sure what you mean. If you look at the link provided, you can see that maven-eclipse.github.io pulls from the official SWT releases and repackages the artifacts. If you specify a new version of SWT, you will get a new artifact. org.eclipse.swt.win32.win32.x86_64 is just the artifact ID that they use for the 64-bit Windows version of SWT, it's not an indication of how old the artifact is.Spinster
Thanks avojak, actually my swt app is crashing in Mac Mojave. When I raised this bug in eclipse forum, he suggested me to use the artifact 'org.eclipse.swt'. Reference: bugs.eclipse.org/bugs/show_bug.cgi?id=538377Outpouring
@HariKrishna It looks like the artifact that you're pulling should be transitively pulling in a platform-specific SWT jar (take a look at the POM file). Are you getting any errors about unresolved dependencies? But again, if you look in Maven Central, there are very few artifacts available, and those are even older than what I've suggested. IMO your best bet would be to use maven-eclipse.github.io directly.Spinster
@Spinster mvnrepository.com/artifact/org.eclipse.platform/org.eclipse.swt seems to be a more recently-updated artifact than the GitHub repository that you mentioned.Handful

© 2022 - 2024 — McMap. All rights reserved.