JAVA:错误 Java.util.prefs.WindowsPreferences.WindowsRegOpenKey

问题描述

我必须在应用程序上工作。此应用使用regedit 来保存首选项。 此应用程序必须读取一个文件,如果它不存在,则必须将其写入 Regedit。一切都已经奏效,这是一个已经存在的项目。我刚回去工作做一些 Java 14 测试 我有这个错误

java.lang.NoSuchMethodException: java.util.prefs.WindowsPreferences.WindowsRegOpenKey(int,[B,int)
    at java.base/java.lang.class.getDeclaredMethod(Class.java:2553)
    at com.myapp.application.WinRegistry.<clinit>(WinRegistry.java:62)
    at com.myapp.application.XDeclicManager.plot(XDeclicManager.java:421)
    at com.myapp.gui.XDeclicGUI$2.actionPerformed(XDeclicGUI.java:249)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setpressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)

这是我的 WinRegistry 文件

public class WinRegistry {
    
  public static final int HKEY_CURRENT_USER  = 0x80000001;
  public static final int HKEY_LOCAL_MACHINE = 0x80000002;
  public static final int REG_SUCCESS        = 0;
  public static final int REG_NOTFOUND       = 2;
  public static final int REG_ACCESSDENIED   = 5;
  private static final int KEY_ALL_ACCESS    = 0xf003f;
  private static final int KEY_READ          = 0x20019;
  
  private static Preferences userRoot = Preferences.userRoot();
  private static Preferences systemRoot = Preferences.systemRoot();
  private static Class<? extends Preferences> userClass = userRoot.getClass();
  
  private static Method regOpenKey      = null;
  private static Method regCloseKey     = null;
  private static Method regQueryValueEx = null;
  private static Method regEnumValue    = null;
  private static Method regQueryInfoKey = null;
  private static Method regEnumKeyEx    = null;
  private static Method regCreateKeyEx  = null;
  private static Method regSetValueEx   = null;
  private static Method regDeleteKey    = null;
  private static Method regDeleteValue  = null;

  static {
    try {
      regOpenKey = userClass.getDeclaredMethod( "WindowsRegOpenKey",new Class[] { int.class,byte[].class,int.class });
      regOpenKey.setAccessible(true);
      
      regCloseKey = userClass.getDeclaredMethod( "WindowsRegCloseKey",new Class[] { int.class });      
      regCloseKey.setAccessible(true);
      
      regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",byte[].class });      
      regQueryValueEx.setAccessible(true);
      
      regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",int.class,int.class });
      regEnumValue.setAccessible(true);
      
      regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",new Class[] { int.class });
      regQueryInfoKey.setAccessible(true);
      
      regEnumKeyEx = userClass.getDeclaredMethod( "WindowsRegEnumKeyEx",int.class });  
      regEnumKeyEx.setAccessible(true);
      
      regCreateKeyEx = userClass.getDeclaredMethod( "WindowsRegCreateKeyEx",byte[].class });  
      regCreateKeyEx.setAccessible(true);
      
      regSetValueEx = userClass.getDeclaredMethod( "WindowsRegSetValueEx",byte[].class });  
      regSetValueEx.setAccessible(true);
      
      regDeleteValue = userClass.getDeclaredMethod( "WindowsRegDeleteValue",byte[].class });  
      regDeleteValue.setAccessible(true); 
      
      regDeleteKey = userClass.getDeclaredMethod( "WindowsRegDeleteKey",byte[].class });  
      regDeleteKey.setAccessible(true); 
    }
    catch (Exception e) {
      e.printstacktrace();
    }
  }

  private WinRegistry() {  }

  /**
   * Read a value from key and value name
   * @param hkey   HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
   * @param key
   * @param valueName
   * @return the value
   * @throws IllegalArgumentException
   * @throws illegalaccessexception
   * @throws InvocationTargetException
   */
  public static String readString(int hkey,String key,String valueName) 
    throws IllegalArgumentException,illegalaccessexception,InvocationTargetException 
  {
    if (hkey == HKEY_LOCAL_MACHINE) {
      return readString(systemRoot,hkey,key,valueName);
    }
    else if (hkey == HKEY_CURRENT_USER) {
      return readString(userRoot,valueName);
    }
    else {
      throw new IllegalArgumentException("hkey=" + hkey);
    }
  }

  /**
   * Read value(s) and value name(s) form given key 
   * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
   * @param key
   * @return the value name(s) plus the value(s)
   * @throws IllegalArgumentException
   * @throws illegalaccessexception
   * @throws InvocationTargetException
   */
  public static Map<String,String> readStringValues(int hkey,String key) 
    throws IllegalArgumentException,InvocationTargetException 
  {
    if (hkey == HKEY_LOCAL_MACHINE) {
      return readStringValues(systemRoot,key);
    }
    else if (hkey == HKEY_CURRENT_USER) {
      return readStringValues(userRoot,key);
    }
    else {
      throw new IllegalArgumentException("hkey=" + hkey);
    }
  }

  /**
   * Read the value name(s) from a given key
   * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
   * @param key
   * @return the value name(s)
   * @throws IllegalArgumentException
   * @throws illegalaccessexception
   * @throws InvocationTargetException
   */
  public static List<String> readStringSubKeys(int hkey,InvocationTargetException 
  {
    if (hkey == HKEY_LOCAL_MACHINE) {
      return readStringSubKeys(systemRoot,key);
    }
    else if (hkey == HKEY_CURRENT_USER) {
      return readStringSubKeys(userRoot,key);
    }
    else {
      throw new IllegalArgumentException("hkey=" + hkey);
    }
  }

  /**
   * Create a key
   * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
   * @param key
   * @throws IllegalArgumentException
   * @throws illegalaccessexception
   * @throws InvocationTargetException
   */
  public static void createKey(int hkey,InvocationTargetException 
  {
    int [] ret;
    if (hkey == HKEY_LOCAL_MACHINE) {
      ret = createKey(systemRoot,key);
      regCloseKey.invoke(systemRoot,new Object[] { new Integer(ret[0]) });
    }
    else if (hkey == HKEY_CURRENT_USER) {
      ret = createKey(userRoot,key);
      regCloseKey.invoke(userRoot,new Object[] { new Integer(ret[0]) });
    }
    else {
      throw new IllegalArgumentException("hkey=" + hkey);
    }
    if (ret[1] != REG_SUCCESS) {
      throw new IllegalArgumentException("rc=" + ret[1] + "  key=" + key);
    }
  }

  /**
   * Write a value in a given key/value name
   * @param hkey
   * @param key
   * @param valueName
   * @param value
   * @throws IllegalArgumentException
   * @throws illegalaccessexception
   * @throws InvocationTargetException
   */
  public static void writeStringValue
    (int hkey,String valueName,String value) 
    throws IllegalArgumentException,InvocationTargetException 
  {
    if (hkey == HKEY_LOCAL_MACHINE) {
      writeStringValue(systemRoot,valueName,value);
    }
    else if (hkey == HKEY_CURRENT_USER) {
      writeStringValue(userRoot,value);
    }
    else {
      throw new IllegalArgumentException("hkey=" + hkey);
    }
  }

  /**
   * Delete a given key
   * @param hkey
   * @param key
   * @throws IllegalArgumentException
   * @throws illegalaccessexception
   * @throws InvocationTargetException
   */
  public static void deleteKey(int hkey,InvocationTargetException 
  {
    int rc = -1;
    if (hkey == HKEY_LOCAL_MACHINE) {
      rc = deleteKey(systemRoot,key);
    }
    else if (hkey == HKEY_CURRENT_USER) {
      rc = deleteKey(userRoot,key);
    }
    if (rc != REG_SUCCESS) {
      throw new IllegalArgumentException("rc=" + rc + "  key=" + key);
    }
  }

  /**
   * delete a value from a given key/value name
   * @param hkey
   * @param key
   * @param value
   * @throws IllegalArgumentException
   * @throws illegalaccessexception
   * @throws InvocationTargetException
   */
  public static void deleteValue(int hkey,InvocationTargetException 
  {
    int rc = -1;
    if (hkey == HKEY_LOCAL_MACHINE) {
      rc = deleteValue(systemRoot,value);
    }
    else if (hkey == HKEY_CURRENT_USER) {
      rc = deleteValue(userRoot,value);
    }
    if (rc != REG_SUCCESS) {
      throw new IllegalArgumentException("rc=" + rc + "  key=" + key + "  value=" + value);
    }
  }

  // =====================

  private static int deleteValue(Preferences root,int hkey,String value)
    throws IllegalArgumentException,InvocationTargetException 
  {
    int[] handles = (int[]) regOpenKey.invoke( root,new Object[] {new Integer(hkey),toCstr(key),new Integer(KEY_ALL_ACCESS) });
    
    if (handles[1] != REG_SUCCESS) {
      return handles[1];                    // can be REG_NOTFOUND,REG_ACCESSDENIED
    }
    
    int rc =((Integer) regDeleteValue.invoke(root,new Object[] { new Integer(handles[0]),toCstr(value) })).intValue();
    regCloseKey.invoke(root,new Object[] { new Integer(handles[0]) });
    return rc;
  }

  
  private static int deleteKey(Preferences root,InvocationTargetException 
  {
    int rc =((Integer) regDeleteKey.invoke(root,new Object[] { new Integer(hkey),toCstr(key) })).intValue();
    return rc;  // can REG_NOTFOUND,REG_ACCESSDENIED,REG_SUCCESS
  }

  
  private static String readString(Preferences root,new Integer(KEY_READ) });
    if (handles[1] != REG_SUCCESS) {
      return null; 
    }
    
    byte[] valb = (byte[]) regQueryValueEx.invoke( root,new Object[] {new Integer(handles[0]),toCstr(value) });
    
    regCloseKey.invoke(root,new Object[] { new Integer(handles[0]) });
    return (valb != null ? new String(valb).trim() : null);
  }

  
  private static Map<String,String> readStringValues(Preferences root,String key)
    throws IllegalArgumentException,InvocationTargetException 
  {
    HashMap<String,String> results = new HashMap<String,String>();
    int[] handles = (int[]) regOpenKey.invoke( root,new Integer(KEY_READ) });
    if (handles[1] != REG_SUCCESS) {
      return null;
    }
    
    int[] info = (int[]) regQueryInfoKey.invoke( root,new Object[] { new Integer(handles[0]) });

    int count  = info[0];   // count  
    int maxlen = info[3];   // value length max
    
    for(int index = 0; index < count; ++index)  {
      byte[] name = (byte[]) regEnumValue.invoke( root,new Integer(index),new Integer(maxlen + 1)});
      
      String value = readString(hkey,new String(name));
      results.put(new String(name).trim(),value);
    }
    
    regCloseKey.invoke(root,new Object[] { new Integer(handles[0]) });
    return results;
  }

  private static List<String> readStringSubKeys(Preferences root,InvocationTargetException 
  {
    List<String> results = new ArrayList<String>();
    int[] handles = (int[]) regOpenKey.invoke( root,new Integer(KEY_READ)});
    
    if (handles[1] != REG_SUCCESS) {
      return null;
    }
    
    int[] info = (int[]) regQueryInfoKey.invoke( root,new Object[] { new Integer(handles[0]) });

    // Fix: info[2] was being used here with wrong results. 
    // Suggested by davenpcj,confirmed by Petrucio
    int count  = info[0];   
    int maxlen = info[3];   // value length max
    
    for( int index = 0; index < count; ++index)  {
      byte[] name = (byte[]) regEnumKeyEx.invoke(root,new Integer(maxlen + 1)});
      results.add(new String(name).trim());
    }
    
    regCloseKey.invoke(root,new Object[] { new Integer(handles[0]) });
    return results;
  }

  private static int [] createKey(Preferences root,InvocationTargetException 
  {
    return  (int[]) regCreateKeyEx.invoke(root,toCstr(key) });
  }

  private static void writeStringValue(Preferences root,InvocationTargetException 
  {
    int[] handles = (int[]) regOpenKey.invoke(root,new Integer(KEY_ALL_ACCESS) });

    regSetValueEx.invoke(root,toCstr(valueName),toCstr(value)}); 
    regCloseKey.invoke(root,new Object[] { new Integer(handles[0]) });
  }

  /*
   *  utility method
   *  convert String to byte array
   */
  private static byte[] toCstr(String str) 
  {
    byte[] result = new byte[str.length() + 1];

    for (int i = 0; i < str.length(); i++) {
      result[i] = (byte) str.charat(i);
    }
    result[str.length()] = 0;
    
    return result;
  }
}

我已经在 regedit 中创建了密钥: 软件/JavaSoft/Prefs

有什么想法吗?谢谢:)

解决方法

在 Java 11 中,#bloc-divi{ overflow-x: hidden !important; overflow-y: hidden !important; } .container-slider { position: absolute; width: 100vw; height: 100vh; background-color: grey; transition: 0.7s ease; } #photo1 { width: 50%; height: 50%; margin-right: auto; margin-left: auto; margin-top: 10%; background-color: blue; } #photo2 { width: 50%; height: 50%; margin-right: auto; margin-left: auto; margin-top: 10%; background-color: purple; } #photo3 { width: 50%; height: 50%; margin-right: auto; margin-left: auto; margin-top: 10%; background-color: green; } #photo3bis { width: 50%; height: 50%; margin-right: auto; margin-left: auto; margin-top: 10%; background-color: green; } .legende1 { width: 50%; height: 10%; margin-right: auto; margin-left: auto; margin-top: 5%; background-color: red; } .legende2 { width: 50%; height: 10%; margin-right: auto; margin-left: auto; margin-top: 5%; background-color: red; } #slider-2 { left: 100%; } #slider-3 { left: 200%; } #slider-3bis { left: -100%; } .btn-sliderS{ height: 40px; width: 30px; position: absolute; margin-top: calc((100vh - 40px)/2); background-color: yellow; } .btn-sliderS:hover { cursor: pointer; } #btnslider-left{ left: 0; } #btnslider-right{ right: 0; } 中的句柄从 <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <div id="bloc-divi"> <div id="slider-1" class="container-slider"> <div id="photo1"> </div> <div class="legende1"> </div> <div class="legende2"> </div> </div> <div id="slider-2" class="container-slider"> <div id="photo2"> </div> <div class="legende1"> </div> <div class="legende2"> </div> </div> <div id="slider-3" class="container-slider"> <div id="photo3"> </div> <div class="legende1"> </div> <div class="legende2"> </div> </div> <div id="slider-3bis" class="container-slider"> <div id="photo3bis"> </div> <div class="legende1"> </div> <div class="legende2"> </div> </div> <div id="btnslider-left" class="btn-sliderS">< </div> <div id="btnslider-right" class="btn-sliderS">> </div> </div> 更改为 WindowsPreferences。此更改发生在 here in OpenJDK11。您必须更改方法签名才能使此代码在 Java 11+ 环境中运行。例如:

int

必须改为:

long