2010-04-13 47 views
4

我想通過java程序創建註冊表項來在啓動時添加jar文件。如何通過java程序創建註冊表項?

RegistryKey r=new RegistryKey(RootKey.HKEY_CURRENT_USER,"Software/Microsoft/Windows/CurrentVersion/Run"); 
     r.createSubkey("sample"); 

但我得到的錯誤:

Exception in thread "main" java.lang.UnsatisfiedLinkError: ca.beq.util.win32.registry.RegistryKey.testInitialized()V 
     at ca.beq.util.win32.registry.RegistryKey.testInitialized(Native Method) 

我怎樣才能做到這一點?
感謝

+0

嘿它可以與65位操作系統一起工作嗎? 如果沒有,那麼有沒有其他的選擇? – Johnydep 2012-08-21 16:49:45

+0

有人在http://sourceforge.net/p/jregistrykey/support-requests/17/上發佈了一個指向DLL的64位版本的鏈接,儘管我無法使它直接工作 - 仍然得到上述異常。如果有其他人可以使其工作,讓我知道! – 2015-03-27 12:15:05

回答

4

庫添加JRegistryKey.jar
然後在我的項目中複製並粘貼JRegistryKey.dll

之後,我運行相同的程序,註冊表項已成功創建。

RegistryKey r=new RegistryKey(RootKey.HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run"); 
     RegistryValue v=new RegistryValue("name or the registrykey",ValueType.REG_SZ,"my jar file path"); 
     r.setValue(v); 
7

the Javadoc

Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.

你不會在任何機會贏得 OS?

如果沒有,manual for jreg mentions

jRegistryKey is a JNI library. To use jRegistryKey , the following files are required:

  • jRegistryKey.jar
  • jRegistryKey.dll

jRegistryKey.jar is the Java™ Archive (JAR) file containing the packaged Java™ class files, whereas jRegistryKey.dll is a Windows® dyanmically linked library (DLL) that contains the native (C/C++) code required to access the registry.

jRegistryKey.jar must be included in the CLASSPATH available to the Java™ Virtual Machine (JVM);

jRegistryKey.dll must be located in a directory included in the Windows® PATH environment variable or java.lang.UnsatisfiedLinkError 's will be generated

+0

我添加了JRegistryKey.jar.But在哪裏我必須添加JRegistryKey.dll? – Arivu2020 2010-04-13 06:57:41

+0

@ Arivu2020:任何地方都可以通過環境變量%PATH%引用的路徑之一,或者您可以將該DLL的路徑添加到Windows%PATH%。請參閱http://vlaurie.com/computers2/Articles/environment.htm或http://www.computerhope.com/issues/ch000549.htm或http://www.brightrev.com/how-to/windows/35 -add-a-utilities-directory-to-your-pc.html?start = 1 – VonC 2010-04-13 07:03:33

+0

只需在我的項目中添加dll文件即可運行。非常感謝你VonC – Arivu2020 2010-04-13 09:14:38

1

在我的項目中添加jregistrykey.dll對我無效。 我將這個塊包含在我的課程中,它工作。

static { 
    System.load("path\\to\\jregistrykey.dll"); 
}