How do I compile a java file that has jar dependencies?

44,954

For windows:

javac -cp ".;/dir/commons.jar;/dir/more_jar_files.jar" MyClass.java

For unix or mac (thanks for the tip Dawood):

javac -cp ".:/dir/commons.jar:/dir/more_jar_files.jar" MyClass.java

Which means:

javac -cp <path to jar> MyClass.java
Share:
44,954
Yevgeny Simkin
Author by

Yevgeny Simkin

I fled Soviet Russia as a child and have spent my life bouncing from music to comedy to software engineering. You can follow my comedy Twitter feed here. I'm also the founder and CEO of The Russian Mob™—an agency specializing in developing SAAS, Mobile, AR, VR, and Web applications. (No: we won’t help you hack a foreign election so don’t bother asking.) On occasion, things that I think are published over at The Bulwark, which you should be reading, even if my ideas weren't being published there. If you would like to connect with me, the easiest way is through LinkedIn.

Updated on April 29, 2020

Comments

  • Yevgeny Simkin
    Yevgeny Simkin about 4 years

    I have a helper lib that I wrote to sit on top of Apache Commons, and when I try to javac it (so that I can make it into a jar) it complains, quite reasonably, that it has no idea what I'm talking about when I make reference to the stuff that's in the commons.jar.

    How does one include a jar so as that javac can compile?

  • Dawood
    Dawood over 12 years
    This, or if you're on Unix then use a colon : instead of the semicolon ;
  • Dawood
    Dawood over 12 years
    Yep, for Mac you'll use : too
  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    So... other than making the class (now done) and packing it into a jar (also done) and including the jar in my project (ditto), is there anything else I need to do? Cause my helper class isn't showing up in eclipse's intellisense and the project won't compile, so clearly it's not finding the class I'm JARing.
  • zengr
    zengr over 12 years
    Didn't get you, what are you exactly trying to do?
  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    I'm trying to include my jar in the project, via Java Build Path. So, I'm making the jar out of my class (which contains my helper lib) but then when I go to actually use it in my project (ie. new MyServerCall(); ), it's not recognized.
  • zengr
    zengr over 12 years
    In your last comment, how did you pack the class: MyClass.class in your new jar? To create a jar file you need a command like this: jar cf jar-file input-file(s)
  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    yep... that's exactly what I'm doing... I'm not including the other jars (that my .java depends on)... just the .class that it produced by the javac that we covered earlier.