Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java创建快捷方式文件_Java_Windows - Fatal编程技术网

从Java创建快捷方式文件

从Java创建快捷方式文件,java,windows,Java,Windows,几个星期以来一直困扰我的一个问题是如何从Java创建快捷方式文件。现在,在我说其他任何事情之前,我已经浏览了整个谷歌(以及这个网站;包括这个:)试图找到一些有用的东西。我需要的不是创建快捷方式的安装程序包,而是从代码创建快捷方式。我所说的快捷方式是指通常在桌面上找到的.lnk文件 我发现其中一个有用的东西是这个程序: Java代码: import java.io.*; public class WindowsUtils { private WindowsUtil

几个星期以来一直困扰我的一个问题是如何从Java创建快捷方式文件。现在,在我说其他任何事情之前,我已经浏览了整个谷歌(以及这个网站;包括这个:)试图找到一些有用的东西。我需要的不是创建快捷方式的安装程序包,而是从代码创建快捷方式。我所说的快捷方式是指通常在桌面上找到的.lnk文件

我发现其中一个有用的东西是这个程序:

Java代码:

import java.io.*;       
public class WindowsUtils {     
     private WindowsUtils() { }
     private static final String WINDOWS_DESKTOP = "Desktop";
     public static String getWindowsCurrentUserDesktopPath() { //return the current user desktop path
         return System.getenv("userprofile") + "/" + WINDOWS_DESKTOP ;
     }
     public static void createInternetShortcutOnDesktop(String name, String target) throws IOException {
         String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
         createInternetShortcut(name, path, target, "");
     }
     public static void createInternetShortcutOnDesktop(String name, String target, String icon) throws IOException {
         String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
         createInternetShortcut(name, path, target, icon);
     }
     public static void createInternetShortcut(String name, String where, String target, String icon) throws IOException {
         FileWriter fw = new FileWriter(where);
         fw.write("[InternetShortcut]\n");
         fw.write("URL=" + target + "\n");
         if (!icon.equals("")) {
             fw.write("IconFile=" + icon + "\n");*
         }
         fw.flush();
         fw.close();
     }
     public static void main(String[] args) throws IOException {
         WindowsUtils.createInternetShortcutOnDesktop("GOOGLE", "http://www.google.com/");
     }
}
我玩弄它,并设法在我的桌面上创建了一个.lnk快捷方式。然而,我发现了两个问题:

我无法更改图标,尽管路径将其链接到正确的图标。 我创建了一个指向C:/Users/USER/Documents的路径,但是,每当我单击快捷键时,它就会将我带到C:/。当我删除快捷方式时,对话框显示路径确实是正确的file:////C:/Users/USER/Documents.

我知道上面的代码最初是用来创建Internet快捷方式的,所以我相信我可能采取了错误的方法。如果您能给我任何帮助/链接,我将不胜感激。

使用jni使用库jShellLink的简单教程 我可以在GitHub上推荐此存储库: 在那里,我找到了创建快捷方式的简单解决方案:
ShellLink.createLink(“path/to/existing/file.txt”、“path/to/the/future/shortcut.lnk”)
如果您想阅读快捷方式:

如果要更改快捷方式的图标,请使用:

ShellLink sl = ...;
sl.setIconLocation("/path/to/icon/file");
希望这对您有所帮助:) 亲切的问候 约书亚·弗兰克

看看这个图书馆:
ShellLink sl = ...;
sl.setIconLocation("/path/to/icon/file");