2010-10-05 31 views
1

Je veux utiliser Runtime.exec() pour mettre à jour le registre pour HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run en utilisant l'utilitaire de commande Windows REG. Besoin de pouvoir ajouter/supprimer/lire une entrée de la touche "Exécuter" pour permettre à mon application Swing de s'exécuter au démarrage et vérifier si elle est configurée pour s'exécuter au démarrage afin que je puisse marquer l'option comme cochée ou décoché dans l'interface graphique. J'ai eu ce travail avec JNI, mais la bibliothèque était en 32 bits seulement, donc cela ne fonctionne pas sur 64 bits. Je pense que ce sera une meilleure approche. Je n'ai même pas besoin d'inclure une bibliothèque de cette façon et je ne pense pas que REG s'en va ou change.Comment utiliser Java Runtime.exec() avec l'utilitaire Windows REG pour lire/mettre à jour/supprimer des entrées dans HKEY_LOCAL_MACHINE ... CurrentVersion Run?

Est-ce que quelqu'un a déjà fait cela ou sait comment faire?

Merci!

+0

en double. voir http://stackoverflow.com/search?q=[java]+%2Bwindows+%2Bregistry – splash

Répondre

0

J'ai ajouté quelques nouvelles méthodes (addValue/DeleteValue) à l'exemple trouvé ici: read/write to Windows Registry using Java

/** 
* @author Oleg Ryaboy, based on work by Miguel Enriquez 
*/ 
public class WindowsReqistry 
{ 

    /** 
    * 
    * @param location 
    *   path in the registry 
    * @param key 
    *   registry key 
    * @return registry value or null if not found 
    */ 
    public static final String readRegistry(String location, String key) 
    { 
     try 
     { 
      // Run reg query, then read output with StreamReader (internal class) 
      Process process = Runtime.getRuntime().exec("reg query \"" + location + "\" /v \"" + key + "\""); 

      StreamReader reader = new StreamReader(process.getInputStream()); 
      reader.start(); 
      process.waitFor(); 
      reader.join(); 
      String output = reader.getResult(); 

      // Output has the following format: 
      // \n<Version information>\n\n<key>\t<registry type>\t<value> 
      if (!output.contains("\t")) 
      { 
       return null; 
      } 

      // Parse out the value 
      String[] parsed = output.split("\t"); 
      if(parsed.length > 0) 
      { 
       String result = parsed[parsed.length - 1].trim(); 
       result = result.substring(1, result.length() - 1); 
       return result; 
      } 
     } 
     catch (Exception e) 
     { 
     } 
     return null; 
    } 

    static class StreamReader extends Thread 
    { 
     private InputStream is; 
     private StringWriter sw = new StringWriter();; 

     public StreamReader(InputStream is) 
     { 
      this.is = is; 
     } 

     public void run() 
     { 
      try 
      { 
       int c; 
       while ((c = is.read()) != -1) 
        sw.write(c); 
      } 
      catch (IOException e) 
      { 
      } 
      finally 
      { 
       try 
       { 
        is.close(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     } 

     public String getResult() 
     { 
      return sw.toString(); 
     } 
    } 

    public static boolean deleteValue(String key, String valueName) 
    { 
     try 
     { 
      // Run reg query, then read output with StreamReader (internal class) 
      Process process = Runtime.getRuntime().exec("reg delete \"" + key + "\" /v \"" + valueName + "\" /f"); 

      StreamReader reader = new StreamReader(process.getInputStream()); 
      reader.start(); 
      process.waitFor(); 
      reader.join(); 
      String output = reader.getResult(); 

      // Output has the following format: 
      // \n<Version information>\n\n<key>\t<registry type>\t<value> 
      return output.contains("The operation completed successfully"); 
     } 
     catch (Exception e) 
     { 
     } 
     return false; 
    } 

    public static boolean addValue(String key, String valName, String val) 
    { 
     try 
     { 
      // Run reg query, then read output with StreamReader (internal class) 
      Process process = Runtime.getRuntime().exec(
        "reg add \"" + key + "\" /v \"" + valName + "\" /d \"\\\"" + val + "\\\"\" /f"); 

      StreamReader reader = new StreamReader(process.getInputStream()); 
      reader.start(); 
      process.waitFor(); 
      reader.join(); 
      String output = reader.getResult(); 

      // Output has the following format: 
      // \n<Version information>\n\n<key>\t<registry type>\t<value> 
      return output.contains("The operation completed successfully"); 
     } 
     catch (Exception e) 
     { 
     } 
     return false; 
    } 

} 
+0

Cela fonctionne sur XP mais pas Vista (les deux 32 bits). Je suppose que c'est à cause du contrôle d'accès des utilisateurs. Je reçois "ERREUR: Accès refusé" quand je viens d'exécuter ces commandes à partir de l'invite de commande, mais ils fonctionnent sur XP. Je ne sais pas comment résoudre ce problème. – Cal