1. 程式人生 > >java建立快捷方式實現應用程式開機自啟

java建立快捷方式實現應用程式開機自啟

SpringBoot應用中在啟動的時候讓應用在啟動的時候,在windows系統中的啟動目錄下建立快捷方式,在系統啟動的時候啟動指定應用。這裡使用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