How to create shortcut icon for java program

前端 未结 11 2537
野性不改
野性不改 2020-12-29 10:57

Hi I have created executable jar of my java program i want to create shortcut icon for that jar. Means may be my jar is in any other memory location of hard drive(eg- D or E

11条回答
  •  一生所求
    2020-12-29 11:04

    There is a JNI library named jshortcut-0.4-oberzalek.jar ,you can download it from this link just given below and add to your project libraries.

    https://github.com/jimmc/jshortcut/downloads

    It works unbelievably perfect in my project.Here I use an additional function named getdir(),to get current location of your deployed project folder, and store value in a variable named 'PRJT_PTH'.

    After that your deployed project can save on any drive,no matter where it is.It will automatically creates a shortcut on desktop

    Here is the code that I used for creating a shortcut to deployed project.(JMM.jar in my case)

    import net.jimmc.jshortcut.JShellLink;
    
    String PRJT_PATH="";
    private void getdir() throws IOException{
        File f=new File(".");
        File[] f1=f.listFiles();
        PRJT_PATH=f.getCanonicalPath();
    }    //you can call this function at windowOpened event,this will get path of current directory where your project located.
    
    JShellLink link;
    String filePath;
    
    public void createDesktopShortcut() { //after that call createDesktopShortcut() function to create shortcut to desktop.
        try {
            link = new JShellLink();
            filePath = JShellLink.getDirectory("") + PRJT_PTH +"\\JMM.jar";
        } catch (Exception e) {
    
        }
    
        try {
            link.setFolder(JShellLink.getDirectory("desktop"));
            link.setName("JMM");  //Choose a name for your shortcut.In my case its JMM.
            link.setPath(filePath); //link for our executable jar file
            link.setIconLocation(PRJT_PATH1+ "\\jmm.ico"); //set icon image(before that choose your on manual icon file inside our project folder.[jmm.ico in my case])
            link.save();
        } catch (Exception ex) {
           ex.getmessage();
        }
    
    }
    

提交回复
热议问题