Results 1 to 9 of 9
  1. #1
    Mr Wogan is offline Member
    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Default Can't remember how to package library jars in my simple applications

    Hello. I cant remember how to package my library packages in with my simple applications without having to put the library packages in the java ext directory. I haven't done anything with Java for quite a long time now and I can't remember how I used to do it. I used to have a few small packages with a few classes and things in them that I would include in applications. I used them from the jarfile directly rather than requiring that they be in the Java ext directory so I could avoid worrying about cluttering up the ext directory and it made uninstalling applications straightfoward for all platforms because all that was needed was to merely delete the applications' jar file. I don't have any of my old code left that I can just look at to re-learn from. I had a look around the web to see if andthing jogged my memory but I'm not really sure of the exact terms I should be searching on. All I can remember is that it was just a case of putting the jarfiles of my packages in a folder in the applications' jar file and then referencing it in some way from a short block of code that involved getClassLoader() or something.

    Anyone got any ideas what I probably used?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can't remember how to package library jars in my simple applications

    Put that .jar file on your classpath list; it's a (semi) colon (*) separated list of directories (where individual .class files can be found) and .jar files. The classpath can be specified as a command line argument to java or javaw.

    kind regards,

    Jos

    (*) *nix boxes use the colon; windows boxes use the semi colon
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Mr Wogan is offline Member
    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Default Re: Can't remember how to package library jars in my simple applications

    I did something similar to that, but my eyesight is very poor so I might well have made a typo and simply not seen it. I'm fairly sure you already understand what I want to do but I'll try to do a better job of explaining it than I did in my original post, just to make sure we're talking about the same thing. Let's take the example from Adding Classes to the JAR File's Classpath (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files), which I find rather confusing. It seems to be describing how to do what I want to do, but has a warning in the middle of the page saying that it doesn't work.
    Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
    That use of classes in MyUtils.jar from within MyJar.jar is exactly what I want to do. so that if you did
    Java Code:
    jar tf MyJar.jar
    MyUtils.jar would be listed as one of the items in MyJar.jar. Although as you can see from the quote it says that simply adding the Class-Path: MyUtils.jar line to the manifest file won't work. In my own test that I made to try figure it out the inner .jar file is in a folder called "lib", that's about the only difference I can see with the the example on the tutorial page and my own test. It must be something very simple that I am doing wrong, but I just can't figure it out

    Could Netbeans be doing something that I'm using incorrectly? I add the package to the Libraries node so that it compiles, and also to a lib folder in the src folder, then compile it. This puts the inner package in the jar file and next to the jar file in the dist folder.
    then just
    Do I not need to put some code in the main class to load the inner jar and disregard the outer one? I have "Class-Path: lib/image.jar" in the manifest.mf but it still doesn't work yet. I'll have another look again tomorrow.
    Last edited by Mr Wogan; 12-23-2012 at 02:12 AM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can't remember how to package library jars in my simple applications

    Short answer: a .jar file in a .jar file is just another ordinary resource; long answer: a .jar file contains .class files and (optionally) other resources; the other resources don't have any special properties w.r.t. class loaders. Think of all the hoopla a class loader has to go through if it had to detect other .jar files in a .jar file and go through the inner .jar files to find .class files and (shuddder) other doubly nested .jar files. There exists custom code that tries to do just that but that functionality is not part of the standard class loader(s). Don't do it; instead make your utilities .jar file another top level .jar file and mention it on the classpath.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Mr Wogan is offline Member
    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Default Re: Can't remember how to package library jars in my simple applications

    So I shouldn't be trying to do it? I guess that explains why it's so difficult to get Netbeans to go allong with it then. Just keep everything in a Program Files/MyApp/ or ~username/bin/ directory to keep everything together and easy to remove for the user? I just wasn't sure about where to put libraries if I were to avoid sticking them in the java/ext/directory.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can't remember how to package library jars in my simple applications

    Better stay away from that nested .jar fiddling; keep your .jars in one place and put them on the classpath for your application; putting them in the ext directory is dangerous (although you don't have to put them on your classpath anymore) because those .jar files are checked everytime, whatever application starts up (the classes in your .jar file might hide other classes from the other application).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Mr Wogan is offline Member
    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Default Re: Can't remember how to package library jars in my simple applications

    Well "don't do it" seems like a better answer than any other. I take it then that I'd need to write a very basic installer application that contained the various jars, with the sole purpose of puting them in the correct directories? So I'd have say MyApp.jar, lib1.jar and lib2.jar in the installer application which would do nothing more than make directories like ~/bin/wogan/ and ~/bin/wogan/libs/ then put the MyApp.jar file in the wogan directory and the libX.jar files in the libs directory. Then put a header like"Class-Path: lib/libX.jar lib/libY.jar lib/libZ.jar" That way if someone installed another application I wrote, both the new and old application could reference classes from jars in the libs directory without each application having its own libs directory with what are likely to be identical contents?

    Sorry if I seem to cover the same thing over and over, I just have difficulty retaining information as well as having trouble reading. Your patience is very much appreciated.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can't remember how to package library jars in my simple applications

    Google for 'IzPack'; it's a nice, flexible installer written in Java; most important is that it's free ;-)

    kind regards,

    Jos

    ps. oh, before I forget: welcome to the .jar hell ;-)
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Mr Wogan is offline Member
    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Default Re: Can't remember how to package library jars in my simple applications

    Heh. Thanks again. :)

Similar Threads

  1. Simple Package Help
    By DMarsh12 in forum New To Java
    Replies: 1
    Last Post: 02-08-2012, 01:43 AM
  2. simple 2D Vector API/library
    By hawk66 in forum New To Java
    Replies: 3
    Last Post: 01-31-2012, 09:58 PM
  3. Simple Package Manager 0.1.1
    By java software in forum Java Software
    Replies: 0
    Last Post: 11-24-2011, 06:08 PM
  4. How to install library package
    By billchenxi in forum Eclipse
    Replies: 1
    Last Post: 12-10-2010, 09:35 PM
  5. Replies: 0
    Last Post: 08-19-2008, 04:44 PM

Posting Permissions

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