Java 在运行时从res文件夹加载jar

Java 在运行时从res文件夹加载jar,java,jar,swt,Java,Jar,Swt,我试图在运行时基于java应用程序中的os加载一个jar swt.jar文件。 jar在我的res文件夹中,因为我需要将我的项目导出为可运行的jar 我的代码: public class LoaderSwt { public void loadSwtJar() { try { Class.forName ("main.LoaderSwt"); } catch (ClassNotFoundException e) { System.out.pri

我试图在运行时基于java应用程序中的os加载一个jar swt.jar文件。 jar在我的res文件夹中,因为我需要将我的项目导出为可运行的jar

我的代码:

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);
    }
}
如果我尝试将我的程序导出为外部jar并运行它,则会出现以下错误:

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
原因:java.lang.Error:未解决的编译问题: 无法将显示解析为类型 意味着程序无法加载我的swt.jar

解决方案

[编辑] 这是我的主要课程:

public class Launcher {

public static void main(String[] args){
    LoaderSwt loader = new LoaderSwt();
    loader.loadSwtJar();
    WindowYoutube.init();
}

}
loader.loadSwtJar的任务是基于操作系统直接加载我的swt.jar。 WindowYoutube.init调用并创建我的SWT应用程序

以下是windowsYoutube.init:

import org.eclipse.swt.SWT;
....

import org.eclipse.swt.widgets.Display;

....
public static void init(){
    Display display = Display.getDefault();
....
eclipse中的导入为我提供了无法解析为类型的

如果我手动添加正确的swt jar,我的程序运行良好。 但是,如果我想以友好的方式加载swt jar,我必须从构建路径中删除swt.jar的所有引用,因此在eclipse中,我有编译错误,这是应该做到的。 这不是创建swt跨平台程序的正确方法吗?
我遵循了一个关于这一点的教程,它说不必担心eclipse中的编译错误,因为一旦使用外部jar导出程序,它就会工作。

stacktrace根本不包含LoaderWT。确保您在使用显示的类的静态初始化器中完成所有加载。最好不要使用安装的自定义加载程序。谢谢您的回答。是的,我知道,LoaderSwt中的代码运行时没有错误,但Display是一个Swt小部件。因此,如果程序无法解析Display和其他小部件,则意味着swt jar未加载。或者我错过了什么?为了创建swt跨平台程序,我从构建路径中删除了swt.jar,并基于os在运行时加载它。。。因此,在eclipse中,我正确地得到了很多无法解析为类型的错误,但是在运行时加载jar之后,我不断地得到错误。在类的静态初始化器中加载意味着什么?它是如何运行的?问题应该是如何/何处/何时调用new LoaderSwt.loadSwtJar。未解决的编译问题意味着您的代码没有正确编译-如果没有干净地编译,它将无法运行。这救了我的命: