7

Pretty straightforward question. Can it be done without the use of Ants or Maven? (And by that, I mean the command line tool specifically)

Please notice that I don't want to create an uberjar, I just want the archived unit to "know" where its external dependencies are.

2
  • Reference them to do what? Create the manifest? The fact that you're creating the JAR suggests that you've already compiled to .class files. True?
    – duffymo
    Commented Jul 20, 2011 at 20:38
  • My god, that really sounded messy. I'm gonna edit it to make it clearer.
    – makoshichi
    Commented Jul 20, 2011 at 20:44

3 Answers 3

5

Presuming you're talking about a command line invocation of javac, what you're talking about is "can I provide libraries as arguments to javac to fulfill requirements during compilation".

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.

Effectively I suspect you just need to say

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

2
  • Holy crap, how could I be so stupid? Yeah, you spend a long time using IDE's to make everything for you and forget about the basics. I wasn't compiling the files before adding them to the .jar file (basically, I was archiving a bunch of .java files). Ok, we're done here, thanks.
    – makoshichi
    Commented Jul 20, 2011 at 20:51
  • 1
    Once in a while ditch Eclipse and use vim and mvn. You'd be surprised how often a straight mvn package can highlight stuff that Eclipse hides. Bonus points for adding Hudson (or any other CI) to the normal practice.
    – ptomli
    Commented Jul 20, 2011 at 21:08
5

You can make it through META-INF/MANIFEST.MF. You can add other jars to the classpath like this:

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 works only if you define Main-Class and start your application like this:

java -jar my-app.jar

Also notice, that classpath paths are relative to the main jar. So in my example directory structure should look like this:

  • my-app.jar
  • lib
    • slf4j-log4j12-1.5.8.jar
    • slf4j-api-1.5.8.jar
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.