How do I create a symlink in Windows Vista?

前端 未结 3 1202
执笔经年
执笔经年 2021-01-12 15:43

I am looking to create symlinks (soft links) from Java on a Windows Vista/ 2008 machine. I\'m happy with the idea that I need to call out to the JNI to do this. I am after h

3条回答
  •  鱼传尺愫
    2021-01-12 16:21

    Symbolic links in Windows are created using the CreateSymbolicLink API Function, which takes parameters very similar to the command line arguments accepted by the Mklink command line utility.

    Assuming you're correctly referencing the JNI and Win32 SDK headers, your code could thus be as simple as:

    JNIEXPORT jboolean JNICALL Java_ClassName_MethodName
        (JNIEnv *env, jstring symLinkName, jstring targetName)
    {
        const char *nativeSymLinkName = env->GetStringUTFChars(symLinkName, 0);
        const char *nativeTargetName = env->GetStringUTFChars(targetName, 0);
    
        jboolean success = (CreateSymbolicLink(nativeSymLinkName, nativeTargetName, 0) != 0);
    
        env->ReleaseStringUTFChars(symLinkName, nativeSymLinkName);
        env->ReleaseStringUTFChars(targetName, nativeTargetName);
    
        return success;
    }
    

    Note that this is just off the top of my head, and I haven't dealt with JNI in ages, so I may have overlooked some of the finer points of making this work...

提交回复
热议问题