Jar within Jar

If I have a program that uses another Jar file can I put the jar file into the jar file of my application and will it be in the classpath because it is in the jar or how can I go about doing that?

It is NOT possible to use an embeded jar file from classes of the outer jar file. The URLClassLoader, which is used to search .jar files, does not have the functionality to search embeded .jar files as well. Unfortanately there are two "ugly" choices. Either you unjar the jar file so that the embeded .jar files are now on disk, or you write a custom classloader that can search embeded .jar files at runtime. The first is not much different than packaging everything into a .zip file and enforcing the entire program be unzipped before it can be used. This is probably the best way to go for most cases. The latter, a custom loader, is difficult. In my plugin engine, we actually do have our custom plugin loader do this. Each .par file (Plugin ARchive, but identical to a .jar/.zip file), can embed other .jar/.zip files. Anything found in the /lib dir of the .par file is automatically handled by our custom loader code. Basically, when a .class is being looked for, it tries the main .par file /classes dir first. If it is not there, it then tries any embeded .jar/.zip file found in the /lib dir. Along the way we "index" package names so future lookups within embeded jar/zip files are much faster.
There is a caveat to this though. Any code in an embeded jar/zip may do something like getClass().getResource(), or getClass().getClassLoader().getResource(). Problem is, getClass().getResource() should work on the embeded jar/zip file, not the outer par file. It took a bit of time, but our plugin engine is now able to read .class files out of any embeded jar/zip file, and it properly handles the getResource() call as well, looking in the embeded jar/zip if the call is made from a class inside an embeded jar/zip. This allows a plugin to package everything up into a single .par file and distribute it without it having to be unzipped.
Why the URLClassLoader does NOT add this functionality is beyond me. It is really a bad design that you specify a manifest.mf file, class-path files in the .jar, but there is NO documentation explaining that you need the embeded jar/zip files to be unzipped to the disk for them to be used. The bit of code we did could easily be added to URLClassLoader to handle this situation and would prevent the numerous threads on this topic. Why the Sun team doesn't do this is beyond me!

Similar Messages

  • How to use two different versions of the same jar within one project

    Hi everybody!
    I have a big problem while integrating different web service clients within one bigger java project. Both implemetations need some jar files to work but demand different version. So, i would like to know if there is any way to tell which implementation should use which version! Anyhow i add those jars to the classpath makes working only one or the other webservice working but never both! Any ideas somebody?
    Best regrads,
    Stefan G�tz

    Of course do have the jar files different names but the classes inside of them are the same ... means that the standard system classloader can distinguish them. When you add a jar to a classpath the name actually dosent matter.... any other ideas, please?
    PD.: i tryed to add a manifest file to the one jar which makes use of one of the dublicated jars to specify which jar to take direcly but it makes no difference since once classes loaded they cant be distinguish ....see:
    http://java.sun.com/docs/books/tutorial/ext/basics/load.html

  • How to use database file within jar archive

    Hi. I'm quite new in Java programming, and I'm wondering if what I did makes sense or not.
    In my applet I'm using a MS Access Database file, which is contained in the same Jar Archive like the Applet class file. I tried and tried to use this Database file directly but it wouldn't work. Than I read in some other forum that it is not possible to use the database file directly within the archive. First question: Is that true?
    Than I desided to extract the DB file in the init() method of my applet to the default temporary foulder. When the program is closed I use the destroy() method to delete it again. Everything works well now. But is this the typical way this is done?
    Thanks for help!

    Hi,
    here is the code which extracts the database file out of the jar archive (the same archive in which the class file is):
    //get the user temporary folder
    File TempFolder = new File(System.getProperty("java.io.tmpdir"));
    //create (empty) db file
    efile = new File(TempFolder, "steelSections.mdb");
    // if database file is not yet extracted
    if (!efile.exists()) {
    //get an input stream for the database file
    InputStream in = new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream(
    "FaST/db/steelSections.mdb"));
    //create an output stream for the db file on the file system
    OutputStream out = new BufferedOutputStream(new FileOutputStream(efile));
    //-Buffer to copy the data
    byte[] buffer = new byte[2048]; //buffer to copy the binary
    for (;;) {
    int nBytes = in.read(buffer); //read data
    if (nBytes <= 0)
    break; //no more data to read
    out.write(buffer, 0, nBytes); //write data
    out.flush(); //close out and in streams
    out.close();
    in.close();
    If you have the db file in an other jar archive file you need a referenze to the entry in the other jar archive. I'm not sure how to get this, but I'm sure you fill find a solution by searching in this forum...
    Good luck!

  • How to get the entries in a jar/zip file contained within a jar/zip file?

    If I want to list the jar/zip entries in a jar/zip file contained within a jar/zip file how can I do that?
    In order to get to the entry enumeration I need a Zip/JarFile:
    ZipFile zip = new ZipFile("C:/java_dev/Java_dl/java_jdk_commander_v36d.zip");
    // Process the zip file. Close it when the block is exited.
    try {
    // Loop through the zip entries and print the name of each one.
    for (Enumeration list =zip.entries(); list.hasMoreElements(); ) {
    ZipEntry entry = (ZipEntry) list.nextElement();
    System.out.println(entry.getName());
    finally {
    zip.close();
    Zip file "java_jdk_commander_v36d.zip" contains two zip entries:
    1) UsersGuide.zip
    2) JDKcommander.exe
    How to list the entries in "jar:file:/C:/java_dev/Java_dl/java_jdk_commander_v36d.zip!/UsersGuide.zip"?
    The following code:
    URL url = new URL("jar:file:/C:/java_dev/Java_dl/java_jdk_commander_v36d.zip!/UsersGuide.zip");
    JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
    zipFile = (ZipFile)jarConnection.getJarFile();
    would point to "jar:file:/C:/java_dev/Java_dl/java_jdk_commander_v36d.zip", which is no help at all and Class JarURLConnection does not have an enumeration method.
    How can I do this?
    Thanks.
    Andre

    I'm not sure I understand the problem. The difference between a zip and jar file is the manifest file; JarFile is extended from ZipFile and is able to read the manifest, other than that they are the same. Your code
    for (Enumeration list =zip.entries(); list.hasMoreElements(); ) {
    ZipEntry entry = (ZipEntry) list.nextElement();
    System.out.println(entry.getName());
    }is close to what I've use for a jar, below. Why not use the same approach? I don't understand what you're trying to do by using JarURLConnection - it's usually used to read the jar contents.
    String jarName = "";
    JarFile jar = null;
    try
        jar = new JarFile(jarName);
    catch (IOException ex)
        System.out.println("Unable to open jarfile" + jarName);
        ex.printStackTrace();
    for ( Enumeration en = jar.entries() ;  en.hasMoreElements() ;)
        System.out.println(en.nextElement());
    }

  • Loading applet for a jar within an ear

    Hi,
    Apologies if my question sounds ignorant, this is the first time i'm playing with applets.
    I'm trying to load an applet from a JAR (applet.jar) which contains all the files specific to the applet. The applet.jar resides within an ear (application.ear) which is deployed on JBoss.
    The applet related stuff is in the package com.dms.applet (which makes up the applet.jar)
    My index.html resides in a war file (webapp.war), which essentially contains a bunch of servlets. Index.html is a two frame page, which tries to load the applet in one frame and a servlet in another (servlets work fine).
    My <APPLET> tag for the frame in which the applet is displayed (is another html file - applet.html) looks something like this:
    <html><body>
    <APPLET
         code="com.dms.applet.NavigationApplet.class"
         codebase=./"
         archive="applet.jar, application.ear"
         width=200
         height=500
    >
    </APPLET></body></html>
    however, the applet always fails to load, i get the following exception when i open the java console:
    java.lang.ClassNotFoundException:
    com.dms.applet.NavigationApplet.class
         at sun.applet.AppletClassLoader.findClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
         at sun.applet.AppletClassLoader.loadClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
         at sun.applet.AppletClassLoader.loadCode(Unknown Source)
         at sun.applet.AppletPanel.createApplet(Unknown Source)
         at sun.plugin.AppletViewer.createApplet(Unknown Source)
         at sun.applet.AppletPanel.runLoader(Unknown Source)
         at sun.applet.AppletPanel.run(Unknown Source)
         at java.lang.Thread.run(Unknown Source)
    Caused by: java.io.IOException: open HTTP connection failed.
         at sun.applet.AppletClassLoader.getBytes(Unknown Source)
         at sun.applet.AppletClassLoader.access$100(Unknown Source)
         at sun.applet.AppletClassLoader$1.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         ... 10 more
    load: class com.dms.applet.NavigationApplet.class not found.
    java.lang.ClassNotFoundException:
    com.dms.applet.NavigationApplet.class
         at sun.applet.AppletClassLoader.findClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
         at sun.applet.AppletClassLoader.loadClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
         at sun.applet.AppletClassLoader.loadCode(Unknown Source)
         at sun.applet.AppletPanel.createApplet(Unknown Source)
         at sun.plugin.AppletViewer.createApplet(Unknown Source)
         at sun.applet.AppletPanel.runLoader(Unknown Source)
         at sun.applet.AppletPanel.run(Unknown Source)
         at java.lang.Thread.run(Unknown Source)
    Caused by: java.io.IOException: open HTTP connection failed.
         at sun.applet.AppletClassLoader.getBytes(Unknown Source)
         at sun.applet.AppletClassLoader.access$100(Unknown Source)
         at sun.applet.AppletClassLoader$1.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         ... 10 more
    It seems evident that the applet class loader can't find my applet.class and hence can't load it. I guess that its something wrong with my <APPLET> tag, but can't quite seem to get my finger on it. Do i need to explicitly tell the class loader to look for it in the applet.jar which is in the applicatin.ear?
    Any help would be very much appreciated.
    Thanks in advance
    EnterpriseJavaBones

    Hi,
    I managed to fix this problem. For those who mite come across the same issue in the future, this is how i did it.
    I put the applets code in the war file under an applet dir. That the the index.html is in the root (of the war dir) and the applets code is in a /applet dir.
    Hence the codebase simply reads, codebase="applet/"
    Cheers
    EnterpriseJavaBones

  • How to tell if a class within a jar file is being accessed?

    Hello,
    I have written a web app using NetBeans 5.5 and some external libraries.
    Rome 0.9 and a Rome module georss-rome.jar.
    Everything works fine with the integrated Netbeans tomcat 5.5...
    When I try to deploy this as a war using Tomcat 5.5 standalone It will not work properly. To make things worse, I get no error.
    In my code, I instantiate a class in the georss module by doing the following:
                    GeoRSSModule geoRssModule = GeoRSSUtils.getGeoRSS(entry);
                    if(geoRssModule != null)
    ...do something;
    }I then check for null and if not null I do stuff with methods within that jar.
    Basically I pass a rss feed or a georss feed. If its a georss feed then geoRssModule would not be null.
    As I said, this works fine within integrated tomcat but does nothing (no expected action) with standalone which leads me to believe that this is a deployment/jar/classpath issue.
    To me it seems that the class GeoRSSModule is never actually instantiated, but I never get any errors or expected output.
    I set tomcat logging.properties to FINEST for all types of logging.
    Is there a way to determine if the jar is even being accessed for that class file?
    TIA!
    BTW, I dont have the module source.
    I also verified that the jars (rome.jar and georss-rome.jar) are being deployed to webapps/myapp/WEB-INF/lib
    Message was edited by:
    gforty

    GeoRSSModule geoRssModule = GeoRSSUtils.getGeoRSS(entry);Try placing logging statements before and after this statement so that you can track execution. Make sure that your code at least gets to this particular line where you instantiate the GeoRSSModule object. Then place a logging statement afterwards to see if an object was indeed created.
    What helps me in situations like this is that I also use the NetBeans attached debugger listening on your standalone Tomcat installation. This allows you to trace your execution on the server side, one line at a time, giving you a clearer understanding of program flow.

  • How to import .jar files in order to use the classes within this file

    hello guys,
    I'm just wondering how can i import .jar files in order to uses the classes within this file.
    let's take an example:
    i have a folder in which I have many .jar files which contain classes to be called. the full path of this folder is set in the Classpath inside the enviroment variables.
    so does anyone have any idea how can i import these .jar files so i can use the classes?
    thank you.

    Hi,
    My problem is that: I created a Serializable class in a project. And I added this project (first project) to another project (second project). First project is appearing in the second project' s Libraries folder. No problem. I want to create a jar file from second project. i.e. I want to Build second project (I use NetBeans IDE). I am using Build Main Project tab under Run. It is falling out. I am finding it second project' s jar file under its folder. I am clicking on it. But it is NOT WORKING. Do you know WHY. Could you help me please?
    Thanks,

  • Is their a way to start a webproject within a maven jar project in nb6.1?

    Our project structure is as such, that many .jar maven projects will be assembled into one large .war when being deployed on the server. However each .jar file contains a WEB-INF structure to deploy and test each component seperately.
    In eclipse we would just use the tomcat/Devloader plugin to manually deploy each .jar component as a webapplication.
    In Netbeans maven recognizes the .jar file, of course, as a an standard application. However I was wondering if their is a way to also have the .jar file treated as a .war file and have it deployed also?
    Edited by: tomzi on Jun 4, 2008 7:21 AM

    There have been a few instances where I import files and one or two don't get conformed completely or not at all. Instead of having to hunt down the .cfa files and delete them, it would be nice to just manually re-trigger the conforming process within PPro.

  • Problem accessing resources in a built jar but works fine within eclipse

    Howdy,
    I have an image which I am trying to set as my jframe icon, quick overview:
    package: testpackage
    source directory: src (in root dir of package)
    image directory: icon (in root dir of package)
    So if i do this to aquire the image, within the IDE it works fine!
    Image image = Toolkit.getDefaultToolkit().getImage("icon/test.png");
    However, if I export the jar file and try to run it, the image is not being found. I used jar xf to open the jar file and the relitive path is still the same "icon/test.png"...ive tried everything I can think of including just trying to move/include the image file in my source directory but no luck
    FWIW when i right click on the image within eclipse and select "copy fully qualified name" i get a path like this:
    /project/src/package/icon/test.png
    Any ideas?
    thanks

    Try
    Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/icon/test.png"));

  • Text file within jar file

    Can anybody tell me if it's possible to read and write to a text file stored within the .jar file package? I can maintain a constant file length if that enables me to read and write.
    cheers,
    Steve

    reading a text from, from a jar file..... if the jar is in your classpath you can call
    getClass().getResourceAsStream ( "/myServerConfig.properties" );Note the "/", which stops the file from being interpreted as a class name, see Class.getResourceAsStream()
    writing... nothing as easy as the reading solution above.
    You'd probably have to actually "update" the JAR file contents with a new version of the text file.
    regards,
    Owen

  • How to update/replace Jars within sbconfig.jar during import

    Hi,
    I want to replace a jar file, used by java callout actions, during import. I am using WLST script to import configuration jar but cannot figure out how to replace jar file.
    That jar file contains environment specific properties which i intend to change as part of import, once config jar is uploaded and impoted within the same session.
    I have looked to Ref, ArchiveType, Refs, and ALSBConfigurationMBean, but cant figure out to replace specific jar file within imported config jar.
    Many thanks in advance!
    many thanks
    sal
    Edited by: sallo on 16-Feb-2010 07:40

    Hi vivik,
    I am already using custimization files to already to customize environment values etc. But what i need cannot be handled by the customization file.
    I want to replace a jar file (utility jar), already in the sbconfig jar, being referenced by Java Call out actions during import process. I am begining to convince myself that it is not possible to perform such action during import process. I have checked the MBeans API but not very helpful.
    I hope i have able to explain the problem here!
    many thanks
    sal

  • Deploying Java apps: Jars within a jar

    Dear Java gurus,
    Imagine I have a Java application. I wish to bundle it all within a jar file. The problem is, I also wish to add some dependencies, in the shape of a couple of other jars. This means that the user could run the app using the straight-forward "java -jar myapp.jar" command.
    So, I jar it all up. This Jar contains a MANIFEST.MF that specifies the main class, and also the classpath. ("Class-Path: lib/other.jar") as the extra jars were in a dir called lib within the jar.
    But, this doesn't work run as java complains about not being able to resolve classes that exist within other.jar.
    I have yet to find anything on the net beyond really basic examples of using jar. Anyone had more experience with it and can share some tips?

    Its a horrible thing. Basically, you can't include jars in jars.  I asked this question on the Java.sun.com forums a while ago if you want to search there. Its a common question and pisses people off.
    You have some other options:
    1: extract all dependency jars and include the classes in a single jar file.
    2: distribute multiple jars and set the Class-Path in the Manifest to point to them. Since you're pointing at lib/other.jar, you gotta use:
    myjar.jar
    lib/other.jar
    You won't find another way around this. I recall somebody suggesting that packaging jars with jars should be available in mustang, but as of yet, it is not so.
    Also, I would strongly recommend starting to use Java Webstart /JNLP for distributing java apps. I've never done it, but it takes all this hassle out for you by automatically maintaining dependencies. I'm intending to try to get Ensmer going using JNLP sometime.
    Dusty

  • Deploying jars within a single (exploded) ear

    Our project currently uses 8.1 SP3 with split deployment structure.
    Currently the project is split into multiple ears deployed on a single (managed) server. It is my understanding that calls made across ear's even on the same server (jvm) need to be marshalled and unmarshalled using pass by value.
    We are trying to consolidate all our EJBs and Web apps into a single .ear so that we can take advantage of call-by-reference. We also have a requirement of being able to deploy "groups" of EJBs (within a single jar) and/or a web app. Deploying a single ear for the entire project would be one extreme, as it would be painful to deploy individual modules within the ear. On the other hand, using an exploded ear would be too "granular" a way of deploying our changes (we have many EJBs, etc.).
    Is it possible to deploy an exploded ear consisting of jar and war files ? I am in the process of trying this and was looking for feedback. The idea is of being able to deploy parts of the app, such as a .jar or a .war file. Can wlpackage create an exploded ear using .jar and .war files instead of modules consisting of .java/.jsp, etc. files? Thanks for your help!

    There is a classpath issue. Normally ejbs are loaded on top of the ears classpath, so whenever you redeploy them, you have to redeploy the webapps, too. In 8.1 you can build your custom class loader hierarchy. Take a look at http://e-docs.bea.com/wls/docs81/programming/classloading.html#1065667
    -Kai

  • Embeded OC4J server within JDeveloper does not pick latest JAR files.

    Hi,
    Currently OC4J seems to be using an older version of JAXB1.0. Hence I created a shared library in my standalone OC4J server and imported the shared library while deploying my applicatoin in the ClassLoader task.I am trying to replicate the same in
    my embeded OC4J server within JDeveloper.
    I followed as indicated in readme.txt file in the \j2ee\home\shared-lib\ folder.
    1) I created a folder called "jaxb" within /j2ee/home/shared-lib/. I then created a folder with the name "2.0" indicating the version number of the API.
    2) I then added the relevant jaxb JARS to the jaxb/2.0 folder.
    3) In the server.xml file available under /j2ee/home/config/ , I added the following entries.
    <shared-library name="jaxb" version="2.0">
              <code-source path="jaxb-api.jar"/>
              <code-source path="activation.jar"/>
              <code-source path="jaxb1-impl.jar"/>
              <code-source path="jaxb-impl.jar"/>
              <code-source path="jsr173_1.0_api.jar"/>
              <code-source path="jaxb-xjc.jar"/>
         </shared-library>
    I am still facing the issue where my embedded OC4J server is picking up the jaxb version 1.0.
    What are the steps that needs to be taken to ensure that the embeded OC4J
    takes in the jaxb2.0 version.

    Refer
    Re: SUN Jaxb2.0 incompatible with OC4J included in JDEV/OC4J ??

  • Please help.! Problem with locating classes within Jars under WEB-INF/lib.

    Hi All,
    I have a problem which you guys would have faced already...Here it goes..
    I'm using OC4J 10.1.3.0 and use ADF for my web page. I have a controller class which refers to JSF/ADF APIs and this class is archived as a jar file and put under WEB-INF/lib, Also, all the classes related to JSF/ADF are available under the same WEB-INF/lib folder.
    When I invoke my page, the controller class is getting called but it throws NoClassDefFound Error for the ADF based class. i mean, these ADF/JSF classes are not getting found when my jar class is placed within WEB-INF/lib. However, when i extract my jar into WEB-INF/classes, it works.
    I'm sure, i miss some entry for classpath somewhere. Can any one please point me to right direction...?
    Thanks
    Rajasekaran

    Hello,
    I do not have a direct answer to your question, but could you try the ClassLoader query tools to see if your classes/jar are loaded by the applications:
    - Using Queries to Troubleshoot Class Loading Issues
    Regards
    Tugdual Grall

Maybe you are looking for

  • Inserting mp3 in 8.1.7

    hi everyone i just changed OS and i reinstalled oracle 8.1.7 my target is the same as before: insterting MP3 files in a table.. this is what i did: - i created a new user; - i granted connect, resource, create library, create table, create any direct

  • Is Mini DVI = Micro DVI??

    I have an iMac (Model N.: A1224) and I want o conect it to a Samsung Plasma TV. According to the manual, this model has a video output in MINI DVI. So I have to buy a Mini DVI to DVI converter and also a DVI to HDMI converter. But at Apple Store in B

  • Return Component flag in Good Receipt

    Hello, I'm creating a report which need to know which one is a Good Receipt and a Return Component from Production Order. I've tried to look at the table, but can't find which is the flag for Return Component and the other (Good Receipt). Does anyone

  • Unable to install Oracle 8i Personal Edition in Windows98 with Pentium4

    I am getting error "Performed illegal Operation" when ever I unzip and install Oracle 8i PE 8.1.7 for windows98. Error is JREW caused an invalid page fault in module JAVAI.DLL, suggestions was install new JRE Version. I have tried to format hard driv

  • I currently have CS5 student edition on a Dell, and I purchased a Mac.  Do I need to buy it again or

    Can I purchase the upgrade for CS5 to put it on my new Mac computer? Or do I have to buy it new since it's the student edition?