简体   繁体   中英

Building executable from different jars

I wrote a program where various java library's used in the program. I made a jar file of my program using netbeans. Now if I use that jar on any other location, then I will have to manually include all the jar libraries.

Is there any way so that all the dependent libraries should get build with my program's jar to build a single jar instead of so many jar files ?

I know there are programs which converts jar to exe, they also do the same thing but I want to get the last file into jar format, so that it could also run on Linux.

Thanks

You can use use One-Jar

One-JAR provides custom classloader that knows how to load classes and resources from a jars inside an archive, instead of from jars in the filesystem. It discovers dependency jar files based on the internal structure of the archive, there is no custom code required to do this. One-JAR archives can be constructed using Ant or Maven2. Your application can be run using java -jar my-app.jar

Using Maven: you need to update the plugins section pom.xml :

<plugin>
        <groupId>org.dstovall</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <version>1.4.4</version>
        <executions>
          <execution>
            <goals>
                <goal>one-jar</goal>
            </goals>
          </execution>
        </executions>
    </plugin>

And update pluginRepositories section in pom.xml

<pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>

When you will execute the mvn package you will get yourappname-one-jar.jar and you can run it java -jar yourappname-one-jar.jar

Yes it can be done. Since you are using Netbeans, this article may help you.

如果你正在使用maven,你可以找到这个maven-shade-plugin: https//maven.apache.org/plugins/maven-shade-plugin/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM