导出到jar文件后java.lang.NoClassDefFoundError:com / sun / jna / win32 / StdCallLibrary

时间:2016-11-03 09:40:19

标签: java noclassdeffounderror jna

我正在尝试使用cmd编写一个java应用程序来更改我的桌面壁纸。

到目前为止我写的代码:

package me.kaes3kuch3n.main;

import java.util.HashMap;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.UINT_PTR;
import com.sun.jna.win32.*;

public class WallpaperUpdater {
    public static void main(String[] args) {

        if(args.length != 1) {
            System.err.println("Usage: java -jar WallpaperUpdater.jar <path_to_wallpaper>");
        } else {
            String path = args[0];

            SPI.INSTANCE.SystemParametersInfo(
                    new UINT_PTR(SPI.SPI_SETDESKWALLPAPER), 
                    new UINT_PTR(0), 
                    path, 
                    new UINT_PTR(SPI.SPIF_UPDATEINIFILE | SPI.SPIF_SENDWININICHANGE));
        }

    }

    public interface SPI extends StdCallLibrary {

        //from MSDN article
        long SPI_SETDESKWALLPAPER = 20;
        long SPIF_UPDATEINIFILE = 0x01;
        long SPIF_SENDWININICHANGE = 0x02;

        SPI INSTANCE = (SPI) Native.loadLibrary("user32", SPI.class, new HashMap<Object, Object>() {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            {
                put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
                put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
            }
        });

        boolean SystemParametersInfo(
                UINT_PTR uiAction,
                UINT_PTR uiParam,
                String pvParam,
                UINT_PTR fWinIni
                );
    }
}

当我使用Eclipse运行它时它工作得很好但是在将它导出到jar文件后它会抛出一个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/win32/StdCallLibrary
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at me.kaes3kuch3n.main.WallpaperUpdater.main(WallpaperUpdater.java:17)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.win32.StdCallLibrary
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more

我已将jna.jar和jna-platform.jar添加到构建路径中,但它不起作用。我错过了什么?

0 个答案:

没有答案