【问题标题】:How do I rectify the Native.loadlibrary error如何纠正 Native.loadlibrary 错误
【发布时间】:2017-08-12 02:02:04
【问题描述】:

这是我的代码。我导入了 JNA 外部 jar,但我无法编译它,因为我收到“SPI INSTANCE = (SPI) Native.loadLibrary("user32", SPI.class, new HashMap() {" 的错误。错误是: “Native类型中的方法loadLibrary(String, Class, Map)不适用于参数(String, Class, new HashMap(){})”

package desktop;
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 WallpaperChanger {
    public static void main(String[] args) {
        //supply your own path instead of using this one
        String path = "D:\\stone.png";

        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;

            @SuppressWarnings("serial")

            //This is where the error starts:
            SPI INSTANCE = (SPI) Native.loadLibrary("user32", SPI.class, new HashMap<Object, Object>() {
            {
                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
        );
    }
}

【问题讨论】:

  • Native 类型的 loadLibrary(String, Class, Map) 方法不适用于参数 (String, Class, new HashMap(){}) 更新错误消息。这就是我要打字的意思。对于任何混淆,我深表歉意。

标签: java compiler-errors native jna loadlibrary


【解决方案1】:

试试这个:

    @SuppressWarnings("serial")
    SPI INSTANCE = (SPI) Native.loadLibrary("user32", SPI.class,
            new HashMap<String, Object>() {
                {
                    put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
                    put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
                }
            });

答案很简单,方法签名是com.sun.jna.Native#loadLibrary(java.lang.String, java.lang.Class&lt;T&gt;, java.util.Map&lt;java.lang.String,?&gt;)。

您必须传递 java.util.Map&lt;java.lang.String,?&gt; 实例而不是 java.util.Map&lt;java.lang.Object,java.lang.Object&gt; 实例。

【讨论】:

  • @Corbin 很高兴它有帮助。你可以接受我的回答。 (^o^)/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 2010-10-18
  • 1970-01-01
  • 2017-05-15
  • 2014-06-03
  • 2013-04-23
  • 1970-01-01
相关资源
最近更新 更多