0

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.

user2953054
  • 41
  • 1
  • 7
  • The stacktrace doesn't contain `LoaderSwt` at all. Make sure you do all the loading in the static initialiser of the class where the `Display`s are used (it's probably best to not use an instatiated custom loader at all) – blgt Jun 26 '14 at 16:55
  • thanks for your answer. yes i know, the code in LoaderSwt runs without errors but "Display" is a Swt widget. So if the program is unable to resolve "Display" and the other widgets it means that the swt jar is not loaded. or i am missing something? To create a swt crossplatform program i removed the swt.jar from my buildpath and loaded it at runtime based on os... so in eclipse i get correctly a lot of " cannot be resolved to a type" but after the jar is loaded at runtime i keep getting the errors. what do you mean with "loading in the static initialiser of the class"? – user2953054 Jun 26 '14 at 17:14
  • How does it run? The question really should say how/where/when `new LoaderSwt().loadSwtJar()` is called. – blgt Jun 26 '14 at 17:21
  • `Unresolved compilation problems` means your code did not compile properly - if it does not compile cleanly it will not run. – greg-449 Jun 26 '14 at 17:25
  • i added some details. my code won't compile properly because i don't have the swt.jar reference in my buildpath. because i have to add it dinamically – user2953054 Jun 26 '14 at 17:47
  • 1
    This saved my life :) http://stackoverflow.com/questions/23359351/build-one-jar-per-platform-for-multi-platform-swt-application – user2953054 Jun 27 '14 at 17:43

0 Answers0