原文地址: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);
}
}
參考
- Java生成桌面快捷方式(位元組流生成)_賀馳宇的博客-CSDN博客_java生成桌面快捷方式
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/286745.html
標籤:其他
