JavaFx 建立快捷方式及設定開機啟動

one發表於2021-06-11

原文地址:JavaFx 建立快捷方式及設定開機啟動 | Stars-One的雜貨小窩

原本是想整個桌面啟動器,需要在windows平臺上實現開機啟動,但我的軟體都是jar檔案,不是傳統的exe檔案,也不知道能不能設定開機啟動,稍微蒐集了資料研究了會,發現有思路,而且可以成功實現

本文只研究瞭如何在windows進行,不清楚macos和linux的情況,各位有具體的實現思路歡迎分享出來

簡單說明

windows如何實現開機啟動的?

在Windows系統中,設定軟體開機啟動並不是太難的事情,大多數工具類軟體都是有提供開機啟動的選項

那軟體沒有體用選項,就不能設定為開機啟動了?答案當然是否定的

看到網路的教程,都說要去設定任務定時器,其實有種更為方便的做法,就是將軟體或者快捷方式放在windows指定的資料夾即可

檔案路徑格式如下:C:\Users\starsone\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup,這個資料夾暫且稱為啟動資料夾

注意:如果你是將軟體放在這個資料夾想要實現開機啟動,需要保證你的軟體是綠色版,所以更為推薦使用快捷方式的方式進行設定開機啟動

實現思路

但是,剛開始我不確定jar檔案是否也能直接被windows啟動,於是便是拿了之前藍奏雲批量下載作為測試,由於我這是單檔案,所以我直接把jar包放在啟動資料夾,測試是可以的

所以,想要實現jar檔案自動啟動,思路就是給jar檔案建立一個快捷方式,然後將此快捷方式移動到啟動資料夾即可實現

難點在於如何使用java給檔案建立快捷方式?

網上的資料十分少,有個方法還需要使用dll檔案,我也不懂window開發,於是便放棄了

然後找的過程中,發現了有位大佬通過瀏覽微軟官方文件,直接通過位元組流方式實現建立了快捷方式,而且程式碼及其簡單,於是稍微參考了他的原始碼,改造了個工具類,用來實現建立快捷方式及開機啟動

考慮到原本的Java使用者,工具類程式碼補充了Java版本的,用Java同學可以直接拷貝一份,直接使用,如果是Kotlin的,兩份都可使用

吐槽下Java版寫的有點繁瑣,有些API都沒有(如獲取不帶副檔名的檔名),Kotlin中直接有對應方法,不需要自己去處理實現...

Kotlin版

注:本工具類已整合在我的開源專案裡了Stars-One/common-controls: TornadoFx的常用控制元件 controls for tornadofx

建立快捷方式使用

val lnkFile = File("D:\\project\\javafx\\lanzou-downloader\\out\\藍奏雲批量下載器3.2.lnk")
val targetFile = File("D:\\project\\javafx\\lanzou-downloader\\out\\藍奏雲批量下載器3.2.jar")

ShortCutUtils.createShortCut(lnkFile,targetFile)

上述程式碼是將lnk檔案輸出在了同級目錄,我們到資料夾中檢視,可以發現已經生成成功了,點選也是能正常開啟

設定某軟體開機啟動

val targetFile = File("D:\\project\\javafx\\lanzou-downloader\\out\\藍奏雲批量下載器3.2.jar")

ShortCutUtils.setAppStartup(targetFile)

這裡可以看到,生成的快捷方式已經存在於啟動資料夾,這樣下次開機的時候就會自動啟動軟體了

原始碼

class ShortCutUtils{
    
    companion object{
        /**
         * 建立快捷方式
         *
         * @param lnkFile 快捷檔案
         * @param targetFile 原始檔
         */
        fun createShortCut(lnkFile: File, targetFile: File) {
            if (!System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
                println("當前系統不是window系統,無法建立快捷方式!!")
                return
            }
            
            val targetPath = targetFile.path
            if (!lnkFile.parentFile.exists()) {
                lnkFile.mkdirs()
            }
            //原快捷方式存在,則刪除
            if (lnkFile.exists()) {
                lnkFile.delete()
            }

            lnkFile.appendBytes(headFile)
            lnkFile.appendBytes(fileAttributes)
            lnkFile.appendBytes(fixedValueOne)
            lnkFile.appendBytes(targetPath.toCharArray()[0].toString().toByteArray())
            lnkFile.appendBytes(fixedValueTwo)
            lnkFile.appendBytes(targetPath.substring(3).toByteArray(charset("gbk")))
        }

        /**
         * 設定軟體開機啟動
         *
         * @param targetFile 原始檔 
         */
        fun setAppStartup(targetFile: File) {
            val lnkFile = File(targetFile.parentFile, "temp.lnk")
            createShortCut(lnkFile, targetFile)
            val startUpFile = File(startup, "${targetFile.nameWithoutExtension}.lnk")
            //複製到啟動資料夾,若快捷方式已存在則覆蓋原來的
            lnkFile.copyTo(startUpFile, true)
            //刪除快取的快捷方式
            lnkFile.delete()
        }

        /**
         * 設定軟體開機啟動
         *
         * @param targetFile 原始檔路徑
         */
        fun setAppStartup(targetFilePath: String) {
            setAppStartup(File(targetFilePath))
        }

        /**
         * 建立快捷方式
         *
         * @param lnkFilePath 快捷方式檔案生成路徑
         * @param targetFilePath 原始檔路徑
         */
        fun createShortCut(lnkFilePath: String, targetFilePath: String) {
            createShortCut(File(lnkFilePath),File(targetFilePath))
        }
        /**
         * 開機啟動目錄
         */
        val startup =  "${System.getProperty("user.home")}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\"

        /**
         * 桌面目錄
         */
         val desktop = FileSystemView.getFileSystemView().homeDirectory.absolutePath + "\\"

        /**
         * 檔案頭,固定欄位
         */
        private val headFile = byteArrayOf(
            0x4c, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
            0xc0.toByte(), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46
        )

        /**
         * 檔案頭屬性
         */
        private val fileAttributes = byteArrayOf(
            0x93.toByte(), 0x00, 0x08, 0x00,  //可選檔案屬性
            0x20, 0x00, 0x00, 0x00,  //目標檔案屬性
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //檔案建立時間
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //檔案修改時間
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //檔案最後一次訪問時間
            0x00, 0x00, 0x00, 0x00,  //檔案長度
            0x00, 0x00, 0x00, 0x00,  //自定義圖示個數
            0x01, 0x00, 0x00, 0x00,  //開啟時視窗狀態
            0x00, 0x00, 0x00, 0x00,  //熱鍵
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //未知
        )

        private val fixedValueOne = byteArrayOf(
            0x83.toByte(), 0x00, 0x14, 0x00, 0x1F, 0x50, 0xE0.toByte(), 0x4F, 0xD0.toByte(),
            0x20, 0xEA.toByte(), 0x3A, 0x69, 0x10, 0xA2.toByte(),
            0xD8.toByte(), 0x08, 0x00, 0x2B, 0x30, 0x30, 0x9D.toByte(), 0x19, 0x00, 0x2f
        )

        /**
         * 固定欄位2
         */
        private val fixedValueTwo = byteArrayOf(
            0x3A, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00,
            0x32, 0x00, 0x04, 0x00, 0x00, 0x00, 0x67, 0x50, 0x91.toByte(), 0x3C, 0x20, 0x00
        )


    }

}

Java版

建立快捷方式使用

File lnkFile = new File("D:\\project\\javafx\\lanzou-downloader\\out\\藍奏雲批量下載器3.2.lnk");
File targetFile = new File("D:\\project\\javafx\\lanzou-downloader\\out\\藍奏雲批量下載器3.2.jar");

//建立快捷方式(可以傳File物件或者是路徑)
ShortCutUtil.createShortCut(lnkFile,targetFile);

上述程式碼是將lnk檔案輸出在了同級目錄,我們到資料夾中檢視,可以發現已經生成成功了,點選也是能正常開啟

設定某軟體開機啟動

File targetFile = new File("D:\\project\\javafx\\lanzou-downloader\\out\\藍奏雲批量下載器3.2.jar");

//設定開機啟動(可以是File物件或者是路徑)
ShortCutUtil.createShortCut(targetFile);

這裡可以看到,生成的快捷方式已經存在於啟動資料夾,這樣下次開機的時候就會自動啟動軟體了

原始碼

package site.starsone;


import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author StarsOne
 * @url <a href="http://stars-one.site">http://stars-one.site</a>
 * @date Create in  2021/06/11 21:28
 */
public class ShortCutUtil { ;
    /**
     * 開機啟動目錄
     */
    public final static String startup=System.getProperty("user.home")+
            "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\";
    /**
     * 桌面目錄
     */
    public final static String desktop= FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath()+"\\";
    /**
     * 檔案頭,固定欄位
     */
    private static byte[] headFile={0x4c,0x00,0x00,0x00,
            0x01, 0x14,0x02,0x00,0x00,0x00,0x00,0x00,
            (byte) 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46
    };
    /**
     * 檔案頭屬性
     */
    private static byte[] fileAttributes={(byte) 0x93,0x00,0x08,0x00,//可選檔案屬性
            0x20, 0x00, 0x00, 0x00,//目標檔案屬性
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//檔案建立時間
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//檔案修改時間
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//檔案最後一次訪問時間
            0x00,0x00,0x00,0x00,//檔案長度
            0x00,0x00,0x00,0x00,//自定義圖示個數
            0x01,0x00,0x00,0x00,//開啟時視窗狀態
            0x00,0x00,0x00,0x00,//熱鍵
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00//未知
    };
    /**
     * 固定欄位1
     */
    static byte[] fixedValueOne={
            (byte) 0x83 ,0x00 ,0x14 ,0x00
            ,0x1F ,0x50 ,(byte)0xE0 ,0x4F
            ,(byte)0xD0 ,0x20 ,(byte)0xEA
            ,0x3A ,0x69 ,0x10 ,(byte)0xA2
            ,(byte)0xD8 ,0x08 ,0x00 ,0x2B
            ,0x30,0x30,(byte)0x9D,0x19,0x00,0x2f
    };
    /**
     * 固定欄位2
     */
    static byte[] fixedValueTwo={
            0x3A ,0x5C ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
            ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
            ,0x00 ,0x54 ,0x00 ,0x32 ,0x00 ,0x04
            ,0x00 ,0x00 ,0x00 ,0x67 ,0x50 ,(byte)0x91 ,0x3C ,0x20 ,0x00
    };

    /**
     * 生成快捷方式
     * @param start 完整的檔案路徑
     * @param target 完整的快捷方式路徑
     */
    private static void start(String start,String target){
        FileOutputStream fos= null;
        try {
            fos = new FileOutputStream(createDirectory(start));
            fos.write(headFile);
            fos.write(fileAttributes);
            fos.write(fixedValueOne);
            fos.write((target.toCharArray()[0]+"").getBytes());
            fos.write(fixedValueTwo);
            fos.write(target.substring(3).getBytes("gbk"));
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 解決父路徑問題
     */
    private static File createDirectory(String file){
        File f=new File(file);
        //獲取父路徑
        File fileParent = f.getParentFile();
        //如果資料夾不存在
        if (fileParent!=null&&!fileParent.exists()) {
            //建立資料夾
            fileParent.mkdirs();
        }
        if (f.exists()) {
            f.delete();
        }
        
        return f;
    }

    /**
     * 複製檔案
     * @param source
     * @param dest
     * @throws IOException
     */
    private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            inputChannel.close();
            outputChannel.close();
        }
    }

    /**
     * 建立快捷方式
     * @param lnkFilePath 快捷方式檔案路徑
     * @param targetFilePath 快捷方式對應原始檔的檔案路徑
     */
    public static void createShortCut(String lnkFilePath,String targetFilePath) {
        if (!System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
            System.out.println("當前系統不是window系統,無法建立快捷方式!!");
            return;
        }
        start(lnkFilePath,targetFilePath);
    }

    /**
     * 生成快捷方式
     * @param lnkFile 快捷方式檔案
     * @param targetFile 快捷方式對應原始檔
     */
    public static void createShortCut(File lnkFile,File targetFile) {

        if (!System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
            System.out.println("當前系統不是window系統,無法建立快捷方式!!");
            return;
        }
        start(lnkFile.getPath(),targetFile.getPath());
    }

    /**
     * 設定某軟體開啟啟動
     * @param targetFile 原始檔
     * @return 是否設定成功
     */
    public static boolean setAppStartup(File targetFile) {

        File lnkFile = new File(targetFile.getParent(),"temp.lnk");
        createShortCut(lnkFile,targetFile);
        try {
            //將軟體複製到軟體想
            copyFileUsingFileChannels(lnkFile, new File(startup,lnkFile.getName()));
            return true;
        } catch (IOException e) {
            System.out.println("移動到startup資料夾失敗");
            return false;
        }
    }

    /**
     * 設定某軟體開啟啟動
     * @param targetFilePath 原始檔路徑
     * @return 是否設定成功
     */
    public static boolean setAppStartup(String targetFilePath) {
        File targetFile = new File(targetFilePath);
        return setAppStartup(targetFile);
    }
}

參考

相關文章