Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Creating a jar File

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Creating a jar File

    Hello Everyone,

    I currently finding a way how can i package a required/dependency JAR with my runnable JAR only using a command prompt like
    how the ECLIPSE IDE export RUNNABLE JAR
    im running on a linux environment, is there a way to do this.

    i can only make Runnable Jar without the required JAR by using this command

    jar cvfm Test.jar manifest.txt package/*.class

    Thanks in advance , Happy Codings


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Creating a jar File

    Quote Originally Posted by skate101 View Post
    i can only make Runnable Jar without the required JAR by using this command

    jar cvfm Test.jar manifest.txt package/*.class
    External jars must be declared into the manifest. The manifest should have a line like:

    Class-Path: lib/other1.jar lib/other2.jar

    The "lib" sub folder is just an example. Other jars must be tied with a relative path to your main jar.

    And note: if you write the manifest yourself, beware about the line length limitation, from jar specs:
    No line may be longer than 72 bytes (not characters), in its UTF8-encoded form. If a value would make the initial line longer than this, it should be continued on extra lines (each starting with a single SPACE)
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    skate101 (March 6th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a jar File

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

  5. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Creating a jar File

    Hello Sir andbin,

    Great. it works fine when my compiled classes , and library are all in same directory.


    Thank you Sir. but can i asked again .

    Here's my scenario , considering the following directory
    home/skate/classes/ ----> contains all my compiled classes organized in package
    home/skate/bin/ ----> will contain my runnable jar
    home/skate/manifest/ ----> contain the Manifest File
    home/skate/lib/ ----> contains all required JAR files

    when i executed this command:

    jar cvfm ./bin/Skate.jar ./manifest/Manifest.txt ./classes/* ./lib/*

    it created the Skate.jar inside the bin directory but when i execute the jar, it throws an exception

    Exception in thread "main" java.lang.NoClassDefFoundError:

    i understand that java cannot find the entry point because in theManifest file i declared the entry point like this

    Main-Class: com.main.Main


    and when i checked the Skate.jar, i noticed the "classes" directory are also bundle with the Skate.jar

    classes/com/main/Main.class

    so the question is how can i get rid with the parent directory "classes". for me to bundle only the Main.class in my Skate.jar

  6. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Creating a jar File

    Quote Originally Posted by skate101 View Post
    when i executed this command:

    jar cvfm ./bin/Skate.jar ./manifest/Manifest.txt ./classes/* ./lib/*
    First: it has no sense (and it's useless) that you put external jars into your jar. The JVM will not search jars into your jar!

    Quote Originally Posted by skate101 View Post
    it created the Skate.jar inside the bin directory but when i execute the jar, it throws an exception

    Exception in thread "main" java.lang.NoClassDefFoundError:

    i understand that java cannot find the entry point because in theManifest file i declared the entry point like this

    Main-Class: com.main.Main


    and when i checked the Skate.jar, i noticed the "classes" directory are also bundle with the Skate.jar
    It's not correct that the jar contains the 'classes' directory. If you have a class com.main.Main, the jar must contain a "com" folder at the root of the jar (the "com" contains a "main" folder, etc...).

    Use the -C option of jar tool (see official documentation for further informations).
    Instead to use:

    ./classes/*

    use:

    -C ./classes .


    Final note: if you want to keep the relation
    ....anything/bin/yourjar
    ....anything/lib/otherjars

    then the Class-Path entry in your manifest must be like:

    Class-Path: ../lib/xyz1.jar ../lib/xyz2.jar

    because the JVM must know to go "up" (..) and then enter into lib folder to find the jar.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Creating a jar File

    Thank you again Sir.

    -C ./classes . really works

    from the start i already knew that using this command "jar cvfm ./bin/Skate.jar ./manifest/Manifest.txt ./classes/* ./lib/*"
    in command prompt will produce a runnable jar that CANNOT search its dependency jar inside of it .

    Okay Sir, back to my original question in my FIRST post.

    I currently finding a way how can i package a required/dependency JAR with my runnable JAR only using a command prompt like
    how the ECLIPSE IDE export RUNNABLE JAR .

    or should i rephrase my question..

    Is there a way in creating a RUNNABLE jar via Command prompt be like the way how the ECLIPSE IDE exports its RUNNABLE JAR package with the dependency JARS. ?

    Thank you again Sir. and God Bless .

  8. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Creating a jar File

    Quote Originally Posted by skate101 View Post
    Is there a way in creating a RUNNABLE jar via Command prompt be like the way how the ECLIPSE IDE exports its RUNNABLE JAR package with the dependency JARS. ?
    Eclipse has a "Fat Jar" plugin that can create a jar containing your classes/resources and all classes/resources from all dependencies.
    Technically it's possible to do this from command prompt but requires "some" work: a) first, you need to unpack (unzip) all required jars somewhere, b) you need to "pull in" all those classes/resources using that same -C command that now you know.

    However having a single jar with all classes/resources from all dependencies is not a good thing, and at least for 2 reasons:
    1) Licensing problems: the producer of a library could disagree about this type of bundling for any reason.
    2) Update/deploy problems: if you change just only a small thing in your application, you need to recreate this big jar. This may not be a big problem for you. But if the application is deployed through internet (e.g. you send the jar to a customer, or there is an automatic update system, or you store it with FTP somewhere) the big size may be a problem.
    And if a dependency need to be updated (e.g. you choose another version) you need to redo the a) step for this library.

    So please, don't do this.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  9. The Following User Says Thank You to andbin For This Useful Post:

    skate101 (March 9th, 2014)

  10. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Creating a jar File

    Thank you Sir andbin . you help me a lot God Bless Sir .

Similar Threads

  1. creating a jar file
    By viper_pranish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2013, 12:50 PM
  2. run jar file inside a jar file?
    By ufrubnuckle in forum Java Theory & Questions
    Replies: 1
    Last Post: February 21st, 2013, 06:58 AM
  3. creating runnable JAR file
    By olimpicco in forum Java IDEs
    Replies: 25
    Last Post: January 11th, 2012, 09:15 AM
  4. Creating JAR file in eclipse HELP !
    By Maheshkh in forum Java IDEs
    Replies: 3
    Last Post: September 16th, 2011, 12:37 PM
  5. Problem in Creating Jar file
    By sikriyogesh in forum Member Introductions
    Replies: 0
    Last Post: November 20th, 2009, 02:12 AM