Results 1 to 6 of 6
  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Compiling with external libraries

    I have a few jars that I am using with my program as external library. In eclipse it runs fine, but when I export it as a runnable jar file it doesn't. How do I tell java without command line arguments where to look for these jars?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Compiling with external libraries

    Use the classpath command line option for java to point the jar's. Depending upon the licenses of the external libraries, you could also export the runnable jar from eclipse with the libraries packaged within your jar (when you exporta Runnable jar, you should be presented with an option to package required libraries).

  3. #3
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Re: Compiling with external libraries

    Is this the only way of doing it? What I mean is there a way for me to double click on the jar and have it run?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Compiling with external libraries

    Quote Originally Posted by sublixt View Post
    Is this the only way of doing it? What I mean is there a way for me to double click on the jar and have it run?
    Specify the relative path to the libraries within the Manifest file. See Adding Classes to the JAR File's Classpath (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Compiling with external libraries

    Quote Originally Posted by doWhile View Post
    Use the classpath command line option for java to point the jar's. Depending upon the licenses of the external libraries, you could also export the runnable jar from eclipse with the libraries packaged within your jar (when you exporta Runnable jar, you should be presented with an option to package required libraries).
    It's a runnable jar, so the -cp option is not useable if executed as 'java -jar'. Only the classpath in the manifest is used.

    As for packaging them in the jar itself (ie unzipping them), though that can produce a working jar file, it's a real hack when they should simply be distributed with the jar file itself. It's not exactly complex to produce a distribution folder, and I'm sure Eclipse can do that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Compiling with external libraries

    It's a runnable jar, so the -cp option is not useable if executed as 'java -jar'. Only the classpath in the manifest is used.
    It is not ignored when not using the -jar option (eg passing the main class name instead) - I should have specified more clearly in my earlier post.

    As for packaging them in the jar itself (ie unzipping them), though that can produce a working jar file, it's a real hack when they should simply be distributed with the jar file itself. It's not exactly complex to produce a distribution folder, and I'm sure Eclipse can do that.
    Agreed. Hence post 4
    Last edited by doWhile; 05-04-2012 at 06:21 PM.

Similar Threads

  1. Open external file with external program
    By Wietse de Vries in forum New To Java
    Replies: 1
    Last Post: 12-31-2011, 04:52 PM
  2. Compiling a Jar w/ a external lib
    By ateixeira in forum New To Java
    Replies: 12
    Last Post: 09-14-2011, 12:40 PM
  3. How to use libraries?
    By Josep_16 in forum New To Java
    Replies: 1
    Last Post: 09-06-2011, 01:50 PM
  4. Using libraries that others dont have(j3d)
    By scorpion9 in forum Advanced Java
    Replies: 7
    Last Post: 02-19-2010, 11:24 AM
  5. Replies: 1
    Last Post: 11-04-2009, 02:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •