How can I add .jar file dependencies when creating it using the command line tool?

Pretty simple question. Can this be done without using ants or Maven? (And by that I mean the command line tool)

Please note that I do not want to create uberjar, I just want the archived block to “know” where its external dependencies are.

+4
source share
3 answers

Assuming you are talking about calling the javac command line, what you are talking about is "can I provide libraries as javac arguments to fulfill compile-time requirements."

Top entry for man javac says

  -classpath classpath Sets the user class path, overriding the user class path in the CLASSPATH environment variable. If neither CLASSPATH or -class- path is specified, the user class path consists of the current directory. See Setting the Class Path for more details. 

In fact, I suspect you just need to say

javac -classpath path/to/library1.jar Main.java

+3
source

You can do this through META-INF/MANIFEST.MF . You can add other banks to the classpath as follows:

 Manifest-Version: 1.0 Main-Class: org.domain.MyMainClass Class-Path: lib/slf4j-log4j12-1.5.8.jar lib/slf4j-api-1.5.8.jar 

I believe that it only works if you define a Main-Class and start your application as follows:

 java -jar my-app.jar 

Also note that pathpaths refer to the main bank. Therefore, in my example, the directory structure should look like this:

  • my-app.jar
  • Lib
    • slf4j-log4j12-1.5.8.jar
    • SLF4J-api-1.5.8.jar
+2
source

I think you are looking for a manifest file, see here for more details http://download.oracle.com/javase/tutorial/deployment/jar/downman.html

0
source

All Articles