i'm trying to load a jar (swt.jar) file at runtime based on os in my java application. The jar is in my res folder because i need to export my project as a runnable jar.
my code:
public class LoaderSwt {
public void loadSwtJar() {
try {
Class.forName ("main.LoaderSwt");
} catch (ClassNotFoundException e) {
System.out.println (" ! Need to add the proper swt jar: "+e.getMessage());
}
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
//NOTE - I have not added the mac and *nix swt jars.
String osPart =
osName.contains("win") ? "win" :
osName.contains("mac") ? "cocoa" :
osName.contains("linux") || osName.contains("nix") ? "gtk" :
null;
if (null == osPart)
throw new RuntimeException ("Cannot determine correct swt jar from os.name [" + osName + "] and os.arch [" + osArch + "]");
String archPart = osArch.contains ("64") ? "64" : "32";
System.out.println ("Architecture and OS == "+archPart+"bit "+osPart);
String swtFileName = "swt_" +osPart+"_"+ archPart +".jar";
URL is = this.getClass().getClassLoader().getResource("res/classifiers/"+swtFileName);
if (is == null)
System.out.println("Can't locate SWT Jar ");
try {
URLClassLoader classLoader = (URLClassLoader) getClass().getClassLoader ();
Method addUrlMethod = URLClassLoader.class.getDeclaredMethod ("addURL", URL.class);
addUrlMethod.setAccessible (true);
URL swtFileUrl = is;
//System.out.println("Adding to classpath: " + swtFileUrl);
addUrlMethod.invoke (classLoader, swtFileUrl);
}
catch (Exception e) {
throw new RuntimeException ("Unable to add the swt jar to the class path: ", e);
}
}
if i try to export my program as external jar and run it i get this error:
C:\Users\michele\Desktop>java -jar YoutubeAnalyzer.jar
Architecture and OS == 64bit win
rsrc:res/classifiers/swt_win_64.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.Error: Unresolved compilation problems:
Display cannot be resolved to a type
Display cannot be resolved
at main.WindowYoutube.init(WindowYoutube.java:74)
at main.Launcher.main(Launcher.java:8)
... 5 more
Caused by: java.lang.Error: Unresolved compilation problems: Display cannot be resolved to a type means that the program is not able to load my swt.jar
solutions?
[EDIT] this is my main class:
public class Launcher {
public static void main(String[] args){
LoaderSwt loader = new LoaderSwt();
loader.loadSwtJar();
WindowYoutube.init();
}
}
loader.loadSwtJar() has the task to load dinamically my swt.jar based on the OS. WindowYoutube.init() call and create my SWT application.
here is windowsYoutube.init():
import org.eclipse.swt.SWT;
....
import org.eclipse.swt.widgets.Display;
....
public static void init(){
Display display = Display.getDefault();
....
the imports in eclipse give me the "cannot resolved to a type"
If i add manually the correct swt jar my program run fine. But if i want to load the swt jar dinamically i had to remove all references of swt.jar from my buildpath, so in eclipse i have compiling errors (and it's supposed to do). is this not the correct way to create an swt crossplatform program? I followed a tutorial about this and it said not worrying about compiling errors in eclipse because once the program was exported with external jar it would works.