How to create a Java application that can be run by clicking?

I want a Java application that can be launched easily

So far, I've managed to create a jar file, but I don't see any benefit Before "Java helloworldswing" runs my application, I use "Java - jar helloworldswing. Jar" (this is more complex than the previous command. As far as I know, the use of jar files requires the existence of *. MF files in the same directory)

I want to have one of the following two situations:

>Just one file can be copied to another operating system, and the project can be started by clicking this file in the file browser (at present, if you click my jar file, Ubuntu will start extracting the archive (because jar is a file, I know)). > Create a pictogram that can be placed on the desktop and click which Java program to start

Solution

It's easy to make a jar executable without dependencies You basically only need to specify the main class to be used in manifest You can then use Java - jar executablejar. Jar Jar starts, but most operating systems support double clicking in this case

Making a jar that depends on other jar executables is tricky You can specify the classpath in manifest, but it still means that you need to copy multiple jars to distribute your application Another method is to "pack" reliable cans in the main container

You can use Maven and Maven assembly plugin to do everything Jar - by - dependencies. Using a preconfigured descriptor

<configuration>
     <descriptorRefs>
             <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
</configuration>

The main class can be specified as follows:

<archive>
     <manifest>
        <mainClass>org.company.MyClass</mainClass>
     </manifest>
 </archive>

Edit: a complete POM XML can be found in this question

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>