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

后端 未结 12 2658
太阳男子
太阳男子 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:35

    you need to create exe from the java program.

    Creating executable jar files

    1. First, make sure you have installed Java 1.2 or above. This facility is not available in previous versions of Java.
    2. Next, create your working java system. In general, you will want to put it into a package. For this example, I created a trivial HelloWorld application that prints out "Hello World" plus the first command line argument, and placed it into the package "psae". Therefore, the HelloWorld files (HelloWorld.class, HelloWorld.java) were located in the directory psae. I tested the system to make sure it worked before going on to the next step.
    3. In the directory in which the psae is located, created a file called "mainClass". This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line: Main-Class: psae.HelloWorld Note: make sure you type a carriage return after this line; some windows systems need it and will report a "Failed to load Main-Class manifest attribute" error.
    4. Next, I create a jar file called psae.jar using the "jar" command in Java2. I use the "m" command line argument to specify the manifest file mainClass, which adds information to the jar file on where the main class will be found. Here is the jar command: bertha:~ > jar cmf mainClass psae.jar psae
    5. Just for fun, and to check what's happened, I print the table of contents for the jar file I just created. Here's the command and its result: bertha:~ > jar tf psae.jar META-INF/ META-INF/MANIFEST.MF psae/ psae/HelloWorld.java psae/HelloWorld.class
    6. Having successfully created the jar file, I can now invoke java2 on it with the command line argument:

      bertha:~ > java -jar psae.jar Philip
      Hello World Philip
      

提交回复
热议问题