Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4555664
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:15:27+00:00 2026-05-21T17:15:27+00:00

I have written a Java GUI using SWT. I package the application using an

  • 0

I have written a Java GUI using SWT. I package the application using an ANT script (fragment below).

<jar destfile="./build/jars/swtgui.jar" filesetmanifest="mergewithoutmain">
  <manifest>
    <attribute name="Main-Class" value="org.swtgui.MainGui" />
    <attribute name="Class-Path" value="." />
  </manifest>
  <fileset dir="./build/classes" includes="**/*.class" />
  <zipfileset excludes="META-INF/*.SF" src="lib/org.eclipse.swt.win32.win32.x86_3.5.2.v3557f.jar" />
</jar>

This produces a single jar which on Windows I can just double click to run my GUI. The downside is that I have had to explicitly package the windows SWT package into my jar.

I would like to be able to run my application on other platforms (primarily Linux and OS X). The simplest way to do it would be to create platform specific jars which packaged the appropriate SWT files into separate JARs.

Is there a better way to do this? Is it possible to create a single JAR which would run on multiple platforms?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:15:28+00:00Added an answer on May 21, 2026 at 5:15 pm

    I have a working implementation which is now referenced from the SWT FAQ.

    This approach is now available to use as an ANT task: SWTJar

    [EDIT] SWTJar has now been updated to use Alexey Romanov’s solution as described above.

    build.xml

    First I build a jar containing all of my application classes.

    <!-- UI (Stage 1) -->   
    <jarjar jarfile="./build/tmp/intrace-ui-wrapper.jar">
      <fileset dir="./build/classes" includes="**/shared/*.class" />
      <fileset dir="./build/classes" includes="**/client/gui/**/*.class" />
      <zipfileset excludes="META-INF/*.MF" src="lib/miglayout-3.7.3.1-swt.jar"/>
    </jarjar>
    

    Next, I build a jar to contain all of the following:

    • JARs
      • The jar which I just built
      • All the SWT jars
    • Classes
      • The “Jar-In-Jar” classloader classes
      • A special loader class – see below

    Here is the fragment from build.xml.

    <!-- UI (Stage 2) -->
    <jarjar jarfile="./build/jars/intrace-ui.jar">
      <manifest>
        <attribute name="Main-Class" value="org.intrace.client.loader.TraceClientLoader" />
        <attribute name="Class-Path" value="." />
      </manifest>
      <fileset dir="./build/classes" includes="**/client/loader/*.class" />
      <fileset dir="./build/tmp" includes="intrace-ui-wrapper.jar" />
      <fileset dir="./lib" includes="swt-*.jar" />
      <zipfileset excludes="META-INF/*.MF" src="lib/jar-in-jar-loader.jar"/>
    </jarjar>
    

    TraceClientLoader.java

    This loader class uses the jar-in-jar-loader to create a ClassLoader which loads classes from two jars.

    • The correct SWT jar
    • The application wrapper jar

    Once we have this classloader we can launch the actual application main method using reflection.

    public class TraceClientLoader
    {
      public static void main(String[] args) throws Throwable
      {    
        ClassLoader cl = getSWTClassloader();
        Thread.currentThread().setContextClassLoader(cl);    
        try
        {
          try
          {
            System.err.println("Launching InTrace UI ...");
            Class<?> c = Class.forName("org.intrace.client.gui.TraceClient", true, cl);
            Method main = c.getMethod("main", new Class[]{args.getClass()});
            main.invoke((Object)null, new Object[]{args});
          }
          catch (InvocationTargetException ex)
          {
            if (ex.getCause() instanceof UnsatisfiedLinkError)
            {
              System.err.println("Launch failed: (UnsatisfiedLinkError)");
              String arch = getArch();
              if ("32".equals(arch))
              {
                System.err.println("Try adding '-d64' to your command line arguments");
              }
              else if ("64".equals(arch))
              {
                System.err.println("Try adding '-d32' to your command line arguments");
              }
            }
            else
            {
              throw ex;
            }
          }
        }
        catch (ClassNotFoundException ex)
        {
          System.err.println("Launch failed: Failed to find main class - org.intrace.client.gui.TraceClient");
        }
        catch (NoSuchMethodException ex)
        {
          System.err.println("Launch failed: Failed to find main method");
        }
        catch (InvocationTargetException ex)
        {
          Throwable th = ex.getCause();
          if ((th.getMessage() != null) &&
              th.getMessage().toLowerCase().contains("invalid thread access"))
          {
            System.err.println("Launch failed: (SWTException: Invalid thread access)");
            System.err.println("Try adding '-XstartOnFirstThread' to your command line arguments");
          }
          else
          {
            throw th;
          }
        }
      }
    
      private static ClassLoader getSWTClassloader()
      {
        ClassLoader parent = TraceClientLoader.class.getClassLoader();    
        URL.setURLStreamHandlerFactory(new RsrcURLStreamHandlerFactory(parent));
        String swtFileName = getSwtJarName();      
        try
        {
          URL intraceFileUrl = new URL("rsrc:intrace-ui-wrapper.jar");
          URL swtFileUrl = new URL("rsrc:" + swtFileName);
          System.err.println("Using SWT Jar: " + swtFileName);
          ClassLoader cl = new URLClassLoader(new URL[] {intraceFileUrl, swtFileUrl}, parent);
    
          try
          {
            // Check we can now load the SWT class
            Class.forName("org.eclipse.swt.widgets.Layout", true, cl);
          }
          catch (ClassNotFoundException exx)
          {
            System.err.println("Launch failed: Failed to load SWT class from jar: " + swtFileName);
            throw new RuntimeException(exx);
          }
    
          return cl;
        }
        catch (MalformedURLException exx)
        {
          throw new RuntimeException(exx);
        }                   
      }
    
      private static String getSwtJarName()
      {
        // Detect OS
        String osName = System.getProperty("os.name").toLowerCase();    
        String swtFileNameOsPart = osName.contains("win") ? "win" : osName
            .contains("mac") ? "osx" : osName.contains("linux")
            || osName.contains("nix") ? "linux" : "";
        if ("".equals(swtFileNameOsPart))
        {
          throw new RuntimeException("Launch failed: Unknown OS name: " + osName);
        }
    
        // Detect 32bit vs 64 bit
        String swtFileNameArchPart = getArch();
    
        String swtFileName = "swt-" + swtFileNameOsPart + swtFileNameArchPart
            + "-3.6.2.jar";
        return swtFileName;
      }
    
      private static String getArch()
      {
        // Detect 32bit vs 64 bit
        String jvmArch = System.getProperty("os.arch").toLowerCase();
        String arch = (jvmArch.contains("64") ? "64" : "32");
        return arch;
      }
    }
    

    [EDIT] As stated above, for those looking for the “jar-in-jar classloader”: It’s included in Eclipse’s JDT (the Java IDE built on Eclipse). Open org.eclipse.jdt.ui_*version_number*.jar with an archiver and you will find a file jar-in-jar-loader.zip inside. I renamed this to jar-in-jar-loader.jar.

    intrace-ui.jar – this is the jar which I built using the process described above. You should be able to run this single jar on any of win32/64, linux32/64 and osx32/64.

    [EDIT] This answer is now referenced from the SWT FAQ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Java Agent along with an SWT GUI for controlling the
I have a simple GUI component written in Java. The class draws an analog
I have written a small java application for which I need to obtain performance
I have two applications written in Java that communicate with each other using XML
I have an application written in java, and I want to add a flash
Question I have an application written in Java. It is designed to run on
Hey all. I have a server written in java using the ServerSocket and Socket
I have written a java program that is actually works as a gui to
I have a GUI written using netbeans with a few simple components in place.
I have been attempting to enhance my GUI system written in Java to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.