I ran into a wall called Maven a while ago and learned from the fact that Maven was the first place to building an executable jar file.
This time, I will introduce the simplest way to make an executable jar collected from various places, which also serves as a memorandum of what I learned at that time.
If you know of any other easier way, I would appreciate it if you could point out. I wrote a separate article about the basic usage of Maven, so please see that as well.
java -jar <jar file name>
A jar file that can be executed with the command.
Normally, a program made with java must be built as an executable jar when it is executed.
java -classpath <jar file name> <class name with main> <command line arguments>
It is necessary to specify the jar file to be executed and the class path separately as shown in, but if you build it as an executable jar, you can execute it with only the command introduced at the beginning. If it is a GUI environment such as Windows, it can be executed by double-clicking. In that case, it's tough for console applications, but ...
The story goes awry, but I'm planning to write an article about how to create a GUI program using java, so if you are interested, please wait for a while.
From here, I will show you how to build an executable jar using Maven. In this article, we will use Eclipse and assume that you understand the basic usage of Maven. For those who say "I've heard of Maven, but I don't know what it is", please read the rest of the article after you can use Maven to some extent.
I have written such an article once, so please see here if you like.
Either Eclipse or Maven's Komano can be used, but for the time being, create a Maven project. This time, anything is fine as long as the executable jar can be built, so make a Hello World source file and put it in.
To build an executable jar file with Maven, use a Maven plugin called assembly-plugin.
Please describe the following contents in pom.xml and add the plugin.
<!-Plugins for executable jar files->
At this point, all Maven settings are complete. All you have to do now is build with the familiar commands. Execute the following command in the directory containing pom.xml of the created project.
mvn install
Then maven should be running and a jar file should be generated in the target directory. I think that two jar files have been generated, but the one with "jar-with-dependencies" is the executable jar file.
Execute the jar file with a command.
java -jar Generated jar file
What do you think? I think the jar file has been executed.
So far we have shown you how to use maven to generate an executable jar file. I spent a lot of time studying the maven series myself, so I think I'll post another article. I hope it will be useful for those who want to use maven, or who have no choice but to use it but do not know how to use it.
Recommended Posts