java創建快捷方式實現應用程序開機自啓

SpringBoot應用中在啓動的時候讓應用在啓動的時候,在windows系統中的啓動目錄下創建快捷方式,在系統啓動的時候啓動指定應用。這裏使用jshortcut來實現。

jshortcut的github地址:https://github.com/jimmc/jshortcut

可以下載下來自己打jar包,然後用VS編譯一下src/jni/ 目錄下的compile文件編譯jshortcut.cpp文件,編譯前想修改bat其中的jdk目錄,compile.bat編譯成32位的,compile-oberzalek.bat是編譯成64位的,編譯成jshortcut.dll文件。

 

import net.jimmc.jshortcut.JShellLink;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.boot.ApplicationArguments;
import
org.springframework.boot.ApplicationRunner;
import
org.springframework.stereotype.Component;

import
java.io.File;

/**
 *
程序第一次啓動的時候再系統的啓動目錄下創建應用的快捷方式,讓程序隨系統開機自啓
 * 目前請讓 jsshortcut.dll文件和應用程序在同級目錄
 */

@Component
public class LinkFileStartUp implements ApplicationRunner{

   
private static Logger logger = LoggerFactory.getLogger(Logger.class);

   
@Override
   
public void run(ApplicationArguments args) throws Exception {
       
/* 獲取系統的自啓目錄 */
       
String startFolder = "";
       
String osName = System.getProperty("os.name");
       
String userHome = System.getProperty("user.home");
        if
(osName.equals("Windows 7") || osName.equals("Windows 8") || osName.equals("Windows 10")
                || osName.equals(
"Windows Server 2012 R2") || osName.equals("Windows Server 2014 R2")
                || osName.equals(
"Windows Server 2016")) {
            startFolder = userHome
                    +
"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
       
}
        String savePath = startFolder
;
       
String appName = "test.exe";
       
String relativelyPath=System.getProperty("user.dir");
       
// 設置 jshortcut.dll 文件的路徑,設置爲與當前應用同級目錄
       
System.setProperty("JSHORTCUT_HOME",relativelyPath);
       
String exePath = relativelyPath+File.separator+"test.exe";
       
logger.info("系統自啓目錄:{}",startFolder);
       
logger.info("exe文件路徑:{}",exePath);

       
File file = new File(savePath+appName);
        if
(!file.exists()){//如果快捷方式不存在就創建指定的快捷方式
            /* 創建快捷方式到系統啓動目錄 */
           
boolean linkFileRet = createLinkFile(savePath, appName, exePath);
           
logger.info("快捷方式創建結果:{}",linkFileRet);
       
}
    }

   
/**
     * 
爲指定文件創建快捷方式到指定目錄下,指定文件名稱爲英文名稱,中文會導致執行無效
     * @param
savePath
    
* @param lnkName
    
* @param exePath
    
* @return
    
*/
   
public static boolean createLinkFile(String savePath, String lnkName, String exePath){

        File exeFile =
new File(exePath);
       
File saveDir = new File(savePath);
        if
(!exeFile.exists() || !saveDir.exists()){
            System.
out.println("路徑或者文件爲空。");
            return false;
       
}
       
try {
            JShellLink link =
new JShellLink();
           
link.setName(lnkName);//快捷方式的名稱
           
link.setFolder(savePath);//存放路徑
           
link.setPath(exePath);//指向的exe
           
link.save();
       
}catch (Exception e){
            e.printStackTrace()
;
            return false;
       
}
       
return true;
   
}

}

 

dll文件與本程序打的exe文件在同一級目錄,把jar包添加到項目的依賴,可以創建lib目錄放進去,然後需要在項目中的pom文件中添加座標依賴:

<!--快捷方式 依賴jar-->

<dependency>

  <groupId>jshortcut</groupId>

  <artifactId>jshortcut</artifactId>

    <version>0.4</version>

  <scope>system</scope>

  <systemPath>${project.basedir}/lib/jshortcut.jar</systemPath>

</dependency>

 

版本那行不能少,要不然項目啓動會報錯。

將項目打成jar包這時候會把jshortcut..jar打包進項目中,再將項目用exe4j打成exe程序在windows環境下運行,jshort.dll文件放在和exe文件同級目錄下。

附上文件鏈接:

jshortcut.jar和jshortcut.dll

https://pan.baidu.com/s/1DpzeYVIZfC6ld0Qk2HP2SA     密碼:io0b

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章