How to run Java programs by clicking on their icon on Windows?

后端 未结 12 2663
太阳男子
太阳男子 2020-12-01 22:52

I have written a Java program that uses Java swing library. Now I would like to execute this program by double clicking on the executable file on Windows just like any other

12条回答
  •  温柔的废话
    2020-12-01 23:33

    There are two ways. Both involve packaging your code in a .jar.

    The first way is to build an actual .exe file using a tool like Launch4j. It will require you to set up things like tell it which class to execute, which icon to use, which JRE is OK, what JRE parameters to use, etc.

    The second option is to make the .jar itself executable. You do this by adding a manifest to the .jar. The manifest is a small configuration file that describes the jar. One of the attributes is Main-Class which defines the entry point. In other words, it says which class has the main function that should be called when the user double-clicks the file. Here's a basic tutorial about manifests: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

    The 2nd option is easier to get going, but users will know what to do with a .exe far more often.

    Note that if either approach complains that it can't find the class, make sure to set the classpath manifest attribute to match your project.

提交回复
热议问题