jshortcut x64重新编译 解决中文乱码问题

jshortcut.jar通常用于Java在windows下生成快捷方式。该项目在github的地址为:

https://github.com/jimmc/jshortcut

使用该库时需要将jshortcut.dll放在jre/bin下配合使用,但是由于该库的开发时间比较久远,现在网上能够搜索到的dll大多是32位版的,根本无法使用。

所以我们需要重新编译dll文件。(最终编译的64位dll文件及jar文件可在文章底部获取)

  • 首先下载jshortcut项目,解压后进入jshortcut-master目录,查看README.build文件,里面是一些编译说明,其中写明了在windows下编译需要运行compile.bat文件。
  • 我们可以在jshortcut-master\src\jni找到编译用的bat文件,其中compile.bat用于编译32位dll,compile-oberzalek.bat用于编译64位dll。
  • 用记事本打开compile-oberzalek.bat,修改其中的jdk目录,如果不修改的会报“找不到jni.h”的错误。
  • 在compile-oberzalek.bat中我们可以看到使用cl命令编译的,如果你的电脑上安装了Visual studio,可以用以下方式进行编译(本文以vs2015为例)。
  • 在开始中搜索“VS2015开发人员命令提示”,右键,以管理员方式运行。

  • 打开VS2015的控制台窗口后,首先要将编译环境转换为amd64,先进入/vs2015/VC目录下(vs2015的安装目录),运行vcvarsall.bat amd64

  • 然后进入之前下载好的/jshortcut-master/src/jni目录,直接运行compile-oberzalek.bat,编译成功后会在该目录下生成jshortcut_amd64.dll,将该文件名改为jshortcut.dll就可以使用了。

 

在dll使用过程中,发现该dll运行存在中文乱码问题(本人在Java中使用的是UTF-8编码方式),查看了该项目的jshortcut.cpp文件,发现UTF-8的编译开关并未打开,所以修改了以下几个地方:

第12行将#define USE_UTF_8的注释去掉,

将501行的CP_ACP改为CP_UTF8

将396行的CP_ACP改为CP_UTF8

修改后重新编译,解决中文乱码问题。

 

以下为jshortcut的使用方法:

1. 创建快捷方式:(注:shortcutpath中最后的test不是文件夹名,而是最终生成的快捷方式名称)

 1     /**
 2      * 创建快捷方式
 3      * @param sourceFilePath 原文件的路径 (例:d:\\test.exe)
 4      * @param shortcutPath 最终生成的快捷方式路径(例:c:\\user\\Desktop\\test)
 5      */
 6     public static void createShortcut(String sourceFilePath, String shortcutPath) {
 7         String sourceAbsolutePath = FileUtil.getAbsolutePath(sourceFilePath);
 8 
 9         String folder = shortcutPath.substring(0, shortcutPath.lastIndexOf("\\"));
10         String name = shortcutPath.substring(shortcutPath.lastIndexOf("\\") + 1, shortcutPath.length());
11         
12         JShellLink link = new JShellLink();
13         link.setName(name);
14         link.setFolder(folder);
15         link.setPath(sourceAbsolutePath);
16         try{
17             link.save();
18         } catch(RuntimeException e) {
19             
20         }
21         
22     }

2.删除快捷方式

 1     /**
 2      * 删除快捷方式
 3      * @param shortcutPath 快捷方式路径
 4      */
 5     public static void deleteShortCut(String shortcutPath) {
 6         String shortcutPathActual = shortcutPath;
 7         if(!shortcutPathActual.endsWith(".lnk")) shortcutPathActual += ".lnk";
 8         
 9         File shortcutFile = new File(shortcutPathActual);
10         shortcutFile.delete();
11     }

3.获取快捷方式指向的目标

 1     /**
 2      * 获取快捷方式指向的目标
 3      * @param shortcutPath 快捷方式路径
 4      * @return
 5      */
 6     private static String getShortcutTarget(String shortcutPath) {
 7         String folder = shortcutPath.substring(0, shortcutPath.lastIndexOf("\\"));
 8         String name = shortcutPath.substring(shortcutPath.lastIndexOf("\\") + 1, shortcutPath.length());
 9         
10         JShellLink link = new JShellLink(folder, name);
11         try{
12             link.load();
13         } catch(RuntimeException e) {
14             return null;
15         }
16         String targetPath = link.getPath();
17         return targetPath;
18     }

附件:64位dll与jar包

https://files.cnblogs.com/files/xlqwe/jshortcut_amd64.zip

猜你喜欢

转载自www.cnblogs.com/xlqwe/p/9182828.html
今日推荐