0

I've got the SWT file ".jar" added on to my project as an external jar. So it's imported into my project and I can call it's method via:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

There are no import errors when I do this so I suspect everything is working accordingly. However, when I export this as a ".jar" and use it, there are errors saying that the "org.eclipse.swt.[etc]..." don't exist.

I'm trying to make a Bukkit plugin that uses a GUI (SWT) and I have no idea why this isn't working - any suggestions?

user3422952
  • 319
  • 2
  • 8
  • 17

1 Answers1

0

After you ran your main class in eclipse just go to File > Export > Runnable JAR. Select the matching configuration to your file. You should get that :

enter image description here

If you need to load swt dynamically:

public static String getArchFilename(String prefix) 
{ 
   return prefix + "_" + getOSName() + "_" + getArchName() + ".jar"; 
} 

private static String getOSName() 
{ 
   String osNameProperty = System.getProperty("os.name"); 

   if (osNameProperty == null) 
   { 
       throw new RuntimeException("os.name property is not set"); 
   } 
   else 
   { 
       osNameProperty = osNameProperty.toLowerCase(); 
   } 

   if (osNameProperty.contains("win")) 
   { 
       return "win"; 
   } 
   else if (osNameProperty.contains("mac")) 
   { 
       return "osx"; 
   } 
   else if (osNameProperty.contains("linux") || osNameProperty.contains("nix")) 
   { 
       return "linux"; 
   } 
   else 
   { 
       throw new RuntimeException("Unknown OS name: " + osNameProperty); 
   } 
} 

private static String getArchName() 
{ 
   String osArch = System.getProperty("os.arch"); 

   if (osArch != null && osArch.contains("64")) 
   { 
       return "64"; 
   } 
   else 
   { 
       return "32"; 
   } 
}
public static void addJarToClasspath(File jarFile) 
{ 
   try 
   { 
       URL url = jarFile.toURI().toURL(); 
       URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); 
       Class<?> urlClass = URLClassLoader.class; 
       Method method = urlClass.getDeclaredMethod("addURL", new Class<?>[] { URL.class }); 
       method.setAccessible(true);         
       method.invoke(urlClassLoader, new Object[] { url });             
   } 
   catch (Throwable t) 
   { 
       t.printStackTrace(); 
   } 
}

Then :

File swtJar = new File(getArchFilename("lib/swt")); 
addJarToClasspath(swtJar);

assuming te structure outside the jar is :

lib/swt_win_32.jar 
lib/swt_win_64.jar 
lib/swt_linux_32.jar 
lib/swt_linux_64.jar

From http://www.chrisnewland.com/select-correct-swt-jar-for-your-os-and-jvm-at-runtime-191

Vinz243
  • 9,654
  • 10
  • 42
  • 86