简体   繁体   English

如何通过java程序创建注册表项?

[英]how to create registry key through java program?

I want to create registry key through java program to add the jar file in the start up. 我想通过java程序创建注册表项,在启动时添加jar文件。

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

But i got the error: 但我得到了错误:

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)

How can i do that? 我怎样才能做到这一点?
Thanks 谢谢

From the Javadoc : 来自Javadoc

Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native. 如果Java虚拟机无法找到声明为native的方法的适当本机语言定义,则抛出该异常。

You wouldn't be on a win 64 OS by any chance? 你不会在任何机会赢得64胜利?

If not, the manual for jreg mentions : 如果没有, jreg手册提到

jRegistryKey is a JNI library. jRegistryKey是一个JNI库。 To use jRegistryKey , the following files are required: 要使用jRegistryKey ,需要以下文件:

  • 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是包含打包的Java™类文件的Java™Archive(JAR)文件,而jRegistryKey.dll是Windows®动态链接库(DLL),其中包含访问注册表所需的本机(C / C ++)代码。

jRegistryKey.jar must be included in the CLASSPATH available to the Java™ Virtual Machine (JVM); jRegistryKey.jar必须包含在Java™虚拟机(JVM)可用的CLASSPATH ;

jRegistryKey.dll must be located in a directory included in the Windows® PATH environment variable or java.lang.UnsatisfiedLinkError 's will be generated jRegistryKey.dll必须位于Windows®PATH环境变量中包含的目录中,否则将生成java.lang.UnsatisfiedLinkError

Add the JRegistryKey.jar in the library. 在库中添加JRegistryKey.jar
Then copy and paste JRegistryKey.dll in my project. 然后在我的项目中复制并粘贴JRegistryKey.dll

After that I run the same program ,The registry key is created successfully. 之后,我运行相同的程序,成功创建注册表项。

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);

Adding jregistrykey.dll in my project didn't work for me. 在我的项目中添加jregistrykey.dll并不适合我。 I included this block in my class and it worked. 我把这个块包含在我的课程中并且有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM