V
V
Vanes Ri_Lax2015-09-23 08:57:46
Java
Vanes Ri_Lax, 2015-09-23 08:57:46

How to shove the library along with the program itself?

Hello, I am writing a program in NetBeans IDE. The program works with MySQL, and therefore I have attached a library to the project. When assembling the project, in the place with the jar file of my program, the lib folder appears in which all my libraries that I used in the project are located.
But is it possible to somehow assemble the project so that just one jar archive appears in which all the libraries and my program will be located?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evhen, 2015-09-23
@vanesxl

Use maven
If you want to build an executable jar file with dependencies included, I use the maven-assembly-plugin:

<packaging>jar</packaging>
   ....
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>ru.toster.MainClass</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
 </build>
...

then on the command line
To build a war archive, no additional. plugins can be omitted, but run a command
in the pom file, specify only the archive type "war"
If you do not use mvn, the IDE usually has built-in build tools, for example, in the Eclipse IDE:
You can build an executable jar file manually and pack all the necessary jar libraries into it, but the libraries themselves must be unpacked, i.e. prog.jar/lib/MySql.jar - this will not work, java cannot load jars from jars :) You will need to unpack everything manually so that it is: prog.jar/com.mysql.MySqlDriver.class (this is an example).
For example, when you use Eclipse to build, it embeds its loader in the archive, so jar/jar works. And the above maven plugin just unpacks all dependency archives.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question