如何为java程序创建快捷方式图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1496343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:28:13  来源:igfitidea点击:

How to create shortcut icon for java program

javaswing

提问by Sunil Kumar Sahoo

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 drive for windows) but my icon must be at Desktop. So that if i double click on icon from desktop then my application will start. How to create this

嗨,我已经创建了我的 Java 程序的可执行 jar,我想为该 jar 创建快捷方式图标。意味着可能是我的 jar 位于硬盘驱动器的任何其他内存位置(例如 Windows 的 D 或 E 驱动器),但我的图标必须在桌面上。这样如果我双击桌面上的图标,我的应用程序就会启动。如何创建这个

Thanks Sunil Kumar Sahoo

感谢 Sunil Kumar Sahoo

采纳答案by n002213f

You need to look around for Java Windows Installers, they have functionality to create desktop shortcuts. Take a look at this articleand thisone too.

您需要四处寻找 Java Windows 安装程序,它们具有创建桌面快捷方式的功能。看看这篇文章这篇文章

Install4Jis my personal favorite

Install4J是我个人的最爱

回答by RichN

You should be able to create shortcut as normal.

您应该可以正常创建快捷方式。

After all, installing Java on Windows should by default allow you to run .jar files directly; there should be an association of that extension to java.exe. Alternatively, you can create a batch file to run the jar and then create the shortcut for that file.

毕竟,在 Windows 上安装 Java 应该默认允许您直接运行 .jar 文件;该扩展名应该与 java.exe 相关联。或者,您可以创建一个批处理文件来运行 jar,然后为该文件创建快捷方式。

回答by PeterMmm

If Java is properly installed, the JAR extension is assigned to java. That means on commandline you can type

如果 Java 安装正确,则 JAR 扩展将分配给 java。这意味着您可以在命令行上输入

C:> myprogram.jar 

And if your jar is properly build that runs the program.

如果您的 jar 正确构建,则运行该程序。

You can copy/paste-link your JAR file to desktop.

您可以将 JAR 文件复制/粘贴链接到桌面。

回答by willcodejavaforfood

This is a short tutorial for creating shortcuts to existing programs: http://support.microsoft.com/kb/140443

这是创建现有程序快捷方式的简短教程:http: //support.microsoft.com/kb/140443

回答by harshad khandekar

  1. right click on desktop select option new - shortcut
  2. click on brows button and select path of jar file
  3. select next - finish button.
  4. shortcut file is created on desktop . right click on shortcut file
  5. select properties
  6. click on change icon and change icon of file
  1. 右键单击桌面选择选项新建 - 快捷方式
  2. 单击眉毛按钮并选择 jar 文件的路径
  3. 选择下一步 - 完成按钮。
  4. 快捷方式文件是在桌面上创建的。右键单击快捷方式文件
  5. 选择属性
  6. 单击更改图标并更改文件图标

Congratulations you created a shortcut

恭喜你创建了一个快捷方式

回答by Fathah Rehman P

Assume your jar file is in location c:\pgm\abc.jar

假设您的 jar 文件位于 c:\pgm\abc.jar

open notepad and type c: cd\ cd pgm java -jar abc.jar

打开记事本并输入 c: cd\ cd pgm java -jar abc.jar

Then save that notepad document as "anyname.bat"

然后将该记事本文档另存为“anyname.bat”

Then create a shortcut for that batch file (ie...right click that file and click "Create Shortcut"). You can use this shortcut to run your jar file.

然后为该批处理文件创建一个快捷方式(即...右键单击该文件并单击“创建快捷方式”)。您可以使用此快捷方式来运行您的 jar 文件。

回答by Hari Om Tiwari

Creating shortcut for java .class file is very easy, just follow the instructions:

为 java .class 文件创建快捷方式非常简单,只需按照说明操作:

  1. create a .class file using javac
  2. open a notepad and type following line (note: here filename is the name of my file. Type your file name without .java or .class )

     java  filename
     pause
    

    3.save it as anyname.bat in the same folder where your .class file resides 4.copy and paste shortcut of your new file

  1. 使用 javac 创建一个 .class 文件
  2. 打开记事本并输入以下行(注意:这里的文件名是我的文件名。输入你的文件名,不带 .java 或 .class )

     java  filename
     pause
    

    3.在.class文件所在的同一文件夹中将其另存为anyname.bat 4.复制并粘贴新文件的快捷方式

回答by Ordiel

I'm not entirely sure about this answer, but I've recently found this:

我不完全确定这个答案,但我最近发现了这个:

    try {
        Files.createLink(FileSystems.getDefault().getPath("C:/Documents and Settings/MyUser/Desktop", "MyLink.lnk")
                , FileSystems.getDefault().getPath("C:/Documents and Settings/MyUser/Desktop", "MyExistingFile.pdf"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

This example create a link (shortcut) in the desktop to another file in the desktop.

本示例在桌面中创建指向桌面中另一个文件的链接(快捷方式)。

it in deed creates a shortcut recognized by the OS, but I have not been able to make it work in Windows systems (the shortcut is been created and it is recognized by Windows but it does not work), but I'm working on it, so as soon as I get the answer I'll post it here, in the meantime, you may try to help me, lets see who is the first to make it work :D!

它确实创建了一个操作系统识别的快捷方式,但我无法让它在 Windows 系统中工作(快捷方式已创建,Windows 识别但它不起作用),但我正在研究它,所以一旦我得到答案,我就会将其发布在这里,同时,您可以尝试帮助我,让我们看看谁是第一个让它工作的人:D!

Greetings!

你好!

回答by Ricardas

100% Working native windows shortcut for jar execution:

用于 jar 执行的 100% 工作本机 Windows 快捷方式:

First create regular windows shortcut (right click on desktop, new-> shortcut)
In text field "Type the location of the item" write:
"C:\path\to\javaw.exe" -jar "C:\path\to\jar\myExecutableJar.jar"

首先创建常规 Windows 快捷方式(右键单击桌面,新建->快捷方式
在文本字段“键入项目的位置”中写入:
“C:\path\to\javaw.exe” -jar “C:\path\to \jar\myExecutableJar.jar"

enter image description here

在此处输入图片说明

Click next, type shortcut name ant press Finish.

单击下一步,键入快捷方式名称ant 按Finish

That's all! :)

就这样!:)

Then You can set custom icon:

然后您可以设置自定义图标:

Right click on shortcut-> Properties-> Change Icon...

右键单击快捷方式->属性->更改图标...

enter image description here

在此处输入图片说明

回答by Ricardas

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.

有一个名为 jshortcut-0.4-oberzalek.jar 的 JNI 库,您可以从下面给出的这个链接下载它并添加到您的项目库中。

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

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'.

它在我的项目中运行得令人难以置信的完美。在这里我使用一个名为 getdir() 的附加函数来获取您部署的项目文件夹的当前位置,并将值存储在名为“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)

这是我用于创建已部署项目的快捷方式的代码。(在我的例子中是 JMM.jar)

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();
    }

}