9

ここ数週間、私を困惑させてきた 1 つの問題は、Java からショートカット ファイルを作成する方法です。他のことを言う前に、何か役に立つものを見つけようとして、Google 中 (およびこのサイト (このサイトも含む): Java からのショートカット リンク (.lnk) の作成) を調べました。私が必要としているのは、ショートカットを作成するインストーラー パッケージではなく、コードからショートカットを作成することです。ショートカットとは、通常デスクトップにある .lnk ファイルのことです。

私が見つけた便利なものの1つは、次のプログラムでした。

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 ショートカットを作成することができました。ただし、次の 2 つの問題が見つかりました。

パスが正しいアイコンにリンクしているにもかかわらず、アイコンを変更できませんでした。C:/Users/USER/Documents につながるパスを作成しましたが、ショートカットをクリックすると C:/ に移動しました。ショートカットを削除すると、パスが file:////C:/Users/USER/Documents であることがダイアログに表示されます。

上記のコードはもともとインターネット ショートカットを作成するためのものだったので、間違ったアプローチをとった可能性があると思います。あなたが私に与えることができる助け/リンクをいただければ幸いです。

4

3 に答える 3

10

GitHub でこのリポジトリをお勧めします。

https://github.com/BlackOverlord666/mslinks

そこで、ショートカットを作成するため の簡単な解決策を見つけました。

ShellLink.createLink("path/to/existing/file.txt", "path/to/the/future/shortcut.lnk");

ショートカットを読み たい場合:

File shortcut = ...;
String pathToExistingFile = new ShellLink(shortcut).resolveTarget();

ショートカットのアイコンを変更する場合は、次を使用します。

ShellLink sl = ...;
sl.setIconLocation("/path/to/icon/file");

これがお役に立てば幸いです:)

敬具

ジョスア・フランク

于 2016-08-15T09:52:32.493 に答える
0

http://www.mindfiresolutions.com/Creating-shortcut-from-a-Java-Application-1712.php

jShellLink というライブラリを使用して jni を使用するための素晴らしくシンプルなチュートリアル

于 2013-09-26T16:56:38.313 に答える