jshortcut.jar通常用于Java的在窗户下生成快捷方式该项目在github上的地址为:
https://github.com/jimmc/jshortcut
使用该库时需要将jshortcut.dll放在JRE / bin中下配合使用,但是由于该库的开发时间比较久远,现在网上能够搜索到的DLL大多是32位版的,根本无法使用。
所以我们需要重新编译的DLL文件。(最终编译的64位的DLL文件及罐文件可在文章底部获取)
- 首先下载jshortcut项目,解压后进入jshortcut主目录,查看README.build文件,里面是一些编译说明,其中写明了在窗户下编译需要运行的compile.bat文件。
- 我们可以在jshortcut主\ SRC \ JNI找到编译用的球棒文件,其中的compile.bat用于编译32位的dll,编译oberzalek.bat用于编译64位DLL。
- 用记事本打开编译oberzalek.bat,修改其中的jdk的目录,如果不修改的会报“找不到jni.h”的错误。
- 在compile-oberzalek.bat中我们可以看到使用cl命令编译的,如果你的电脑上安装了Visual studio,可以用以下方式进行编译(本文以vs2015为例)。
- 在开始中搜索“VS2015开发人员命令提示”,右键,以管理员方式运行。
- 打开VS2015的控制台窗口后,首先要将编译环境转换为amd64,先进入/ vs2015 / VC目录下(vs2015的安装目录),运行vcvarsall.bat amd64
- 然后进入之前下载好的/ jshortcut主/ SRC / JNI目录,直接运行编译oberzalek.bat,编译成功后会在该目录下生成jshortcut_amd64.dll,将该文件名改为jshortcut.dll就可以使用了。
在DLL使用过程中,发现该DLL运行存在中文乱码问题(本人在爪哇中使用的是UTF-8编码方式),查看了该项目的jshortcut.cpp文件,发现UTF-8的编译开关并未打开,所以修改了以下几个地方:
第12行将#define USE_UTF_8的注释去掉,
将501行的CP_ACP改为CP_UTF8
将396行的CP_ACP改为CP_UTF8
修改后重新编译,解决中文乱码问题。
以下为jshortcut的使用方法:
1.创建快捷方式:(注:shortcutpath中最后的测试不是文件夹名,而是最终生成的快捷方式名称)
/**
* 创建快捷方式
* @param sourceFilePath 原文件的路径 (例:d:\\test.exe)
* @param shortcutPath 最终生成的快捷方式路径(例:c:\\user\\Desktop\\test)
*/
public static void createShortcut(String sourceFilePath, String shortcutPath) {
String sourceAbsolutePath = FileUtil.getAbsolutePath(sourceFilePath);
String folder = shortcutPath.substring(0, shortcutPath.lastIndexOf("\\"));
String name = shortcutPath.substring(shortcutPath.lastIndexOf("\\") + 1, shortcutPath.length());
JShellLink link = new JShellLink();
link.setName(name);
link.setFolder(folder);
link.setPath(sourceAbsolutePath);
try{
link.save();
} catch(RuntimeException e) {
}
}
2.删除快捷方式
/**
* 删除快捷方式
* @param shortcutPath 快捷方式路径
*/
public static void deleteShortCut(String shortcutPath) {
String shortcutPathActual = shortcutPath;
if(!shortcutPathActual.endsWith(".lnk")) shortcutPathActual += ".lnk";
File shortcutFile = new File(shortcutPathActual);
shortcutFile.delete();
}
3.获取快捷方式指向的目标
/**
* 获取快捷方式指向的目标
* @param shortcutPath 快捷方式路径
* @return
*/
private static String getShortcutTarget(String shortcutPath) {
String folder = shortcutPath.substring(0, shortcutPath.lastIndexOf("\\"));
String name = shortcutPath.substring(shortcutPath.lastIndexOf("\\") + 1, shortcutPath.length());
JShellLink link = new JShellLink(folder, name);
try{
link.load();
} catch(RuntimeException e) {
return null;
}
String targetPath = link.getPath();
return targetPath;
}
附件:64位的dll与罐包
https://files.cnblogs.com/files/xlqwe/jshortcut_amd64.zip