Replacing JAR files loaded by custom ClassLoader

Hi all,
I am trying to create an auto-updater for a stand alone application, which can look for new versions of relevant jar files on the internet and download them. I have code which will look for newly downloaded versions and can replace the old jar file with the new one (using simple File methods). This works fine for when the application is started initially, but as the application is a server and may not manually be started for a long time, I am trying to force the server to restart and reload with the new code.
if(newCode.exists()) {
     System.out.println("new code!");
     if(oldCode.exists()) {
          oldCode.delete();
     if(code.exists()) {
          if(! code.renameTo(oldCode)) {
                    throw new IOException("Error backing up code file");
     if(! newCode.renameTo(code)) {
          throw new IOException("Error installing new code file");
}I have looked around the forums a bit, and have come up with using a ClassLoader based on URLClassLoader:
class ProxyLoader extends URLClassLoader
     public ProxyLoader(String jarFile) throws MalformedURLException {
          super(new URL[] { (new File(jarFile)).toURL() });
     public Class findClass(String name) throws ClassNotFoundException {
          System.out.println("Finding: " + name);
          return super.findClass(name);
}This seems to work OK, as all the classes from the jar file are loaded by this classloader.
I have a method which can create an instance of the class loader, and load the server application.
private boolean startProxy(int port) {
     try {
          pl = new ProxyLoader("proxy.jar");
          Class ptClass = pl.loadClass("proxy.POPThread");
          pt = ptClass.newInstance();
          Method ptStart = ptClass.getMethod("start", new Class[0]);
          ptStart.invoke(pt, new Object[0]);
          return true;
     } catch(Exception e) {
          return false;
}I also have a method to shut down the server:
private boolean stopProxy() {
     try {
          Class ptClass = pl.loadClass("proxy.POPThread");
          Method ptInterrupt = ptClass.getMethod("interrupt", new Class[0]);
          ptInterrupt.invoke(pt, new Object[0]);
          return true;
     } catch(Exception e) {
          return false;
}These appear to be working fine, the problem I have however is when I try to rename the existing jar file (proxy.jar) in order to replace it with a new one (proxy.new), the renameTo method on the File object fails. I am guessing that is because the proxy.jar file is still in use by the JVM. I have tried setting the classloader var to null, but this doesnt seem to have any effect.
My main question is how can I 'free' the jar file so that it can be replaced with new code? Is it possible? My only otherway of working around this is to have a variable stored in a file pointing at the 'current' code, but the jar file isn't litterally replaced.
This is a standalone application, which will probably run in a startup script on a server, so webstart isn't really an option.
TIA
iklesteve

I have found this to be true anytime I try to use the renameTo() API to rename a file with a .jar file extension (regardless of the actual file type - i.e. it could be a text file with .jar appended and it will still fail) in the case where the class making the renameTo() call is loaded from another .jar file.
I run the same exact code after extracting from the .jar, then the rename to the other .jar file is successful. Also, if I rename the .jar file to a ".ja" file, I can successfully rename the jar file even if the code is running with the context of another .jar file.
Is this some kind of bogus security measure within the JVM or is it a bug?

Similar Messages

  • How to list the JAR files loaded into the Oracle Database ?

    How to list the JAR files loaded into the Oracle Database ?

    From 11.1 onwards, the below two views are available to identify the jar files loaded into the Database.
    JAVAJAR$
    JAVAJAROBJECTS$
    By querying the JAVAJAR$ view you can know information about the JAR files loaded into the Database and using JAVAJAROBJECTS$ view you can find all the java objects associated with the given JAR file.
    These views are populated everytime you use LOADJAVA with "-jarsasdbobjects" option to load your custom java classes.
    But unfortunately this feature is available only from 11.1 onwards and there is no clear workaround for above in 10.2 or earlier.

  • How to get list of jar files loaded by servlet container.

    Hi,
    I need to display in my servlet program about the list of jar files loaded by servlet container. Does it vary for each servlet container or is it same. Where can I get those details.
    I need to write code to support tomcat 4x, iplanet 5.0 and websphere 6.0.
    Thanks & Regards,
    Nasrin.N

    For curious, here are output prints for all 3 methods:
    1) parsing system property
    2) tschodt
    3) overcast SystemClassLoader to URLClassLoader
    /home/espinosa/workspace/jboss_embedded_test1/target/test-classes
    /home/espinosa/workspace/jboss_embedded_test1/target/classes
    /opt/javalibs/javax/ejb/ejb-api/3.0/ejb-api-3.0.jar
    /opt/javalibs/javax/jms/jms/1.1/jms-1.1.jar
    /opt/javalibs/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar
    package com.sun.org.apache.xerces.internal.impl.validation, Java Platform API Specification, version 1.6
    package com.thoughtworks.qdox.directorywalker
    package com.sun.org.apache.xerces.internal.parsers, Java Platform API Specification, version 1.6
    package java.util.jar, Java Platform API Specification, version 1.6
    package org.testng.internal.thread
    package com.sun.org.apache.xerces.internal.util, Java Platform API Specification, version 1.6
    package java.net, Java Platform API Specification, version 1.6
    package sun.reflect.misc, Java Platform API Specification, version 1.6
    package esp.ejb.samples1.test
    package sun.security.provider, Java Platform API Specification, version 1.
    file:/home/espinosa/workspace/jboss_embedded_test1/target/test-classes/
    file:/home/espinosa/workspace/jboss_embedded_test1/target/classes/
    file:/opt/javalibs/javax/ejb/ejb-api/3.0/ejb-api-3.0.jar
    file:/opt/javalibs/javax/jms/jms/1.1/jms-1.1.jar
    file:/opt/javalibs/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar
    ...Interestingly, method 1 and 3 gives the same list, same order, same count, just format of item is a little bit different. The order is same as in Eclipse .classpath file.
    Method 2 (tschodt) give significantly more items! rougly 3x! Different order (somewhat random it seems to me). Some items contain extra information, like version and string "Java Platform API Specification".
    It prints not absolute paths but logical Java names.

  • Loading jar at runtime using custom classloader

    Hi,
    I'm trying to load a jar file to an application running inside weblogic at runtime.
    I followed the example that was posted at
    http://dev2dev.bea.com/cs/user/blog?file=/blog/jcscoobyrs/archive/2005/05/realworld_use_f.html
    and did the following:
    URLClassLoader loader = null;
    URL[] urls = {new URL("file:///" + "C:/Program Files/aaa/WEB-INF/lib/ImportTest3.jar")};
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = new URLClassLoader(urls, classLoader);
    Thread.currentThread().setContextClassLoader(loader);
    Class customClass = Class.forName(className, true, loader);
    customCode = customClass.newInstance();
    Thread.currentThread().setContextClassLoader(classLoader);
    everything works fine when I access my application once.
    But when I'm trying to do the same again, everything fails.
    Any ideas?
    Thanks!

    When you say everything fails - can you clarify the failure? Can you paste the stacktrace? Is it a ClassNotFoundException?

  • Replacing jar files real-time in classpath

    Hi All
    I have a swing application and It runs perfectly. I have created a update manager that can download jar files for the application. This update manager downloads jar files and replaces the jar files that are currently in the class path. You could say the update is real-time.
    After the files are downloaded I get errors like class not found and other really strange Java errors. The class not found errors indicate that the application cant find classes that are in the downloaded jar file.
    Is there any way to prevent this from happening?
    Thanks in advance,
    Martyn Hiemstra

    I am currently just saving the downloaded files into the classpath directory, so in short I'm replacing them while the JVM is running.
    The problem is that my Java application is installed using install4j meaning that the application is started as a .exe file. The problem is that I would need to create an external application to update the files but the external programme would have not idea where to put the newly downloaded files. T o overcome this all I want to create an updater within my own application.
    Is there a way to shutdown real-time loading of jar files in the class path? That would solve the problem since the newly loaded jar files arent detected until the application is started again.

  • How to find the JAR files of a custom portal service?

    Hi all,
    I have created and deployed a custom protal service on to the portal. Now for any of the other apps to make use of this service, i need the JAR files which correspond to this portal service. Where do I find them??
    Regards,
    Narahari
    Message was edited by: Narahari Vedula

    Hi,
    Can you check up if this is located in the following path.
    System Administration ->Support->Portal Runtime
    Here navigate to ROOT/WEB-INF/portal/portalapps
    Open the folder with the name of the par file and check up if you can locate the jar.
    Regards,
    Harish
    (Please award points in case this helps)

  • Jar files loading in memory

    I have questions
    1. Are the jar files specified in classpath of a java program loaded in memory at the jvm startup ?
    2. If all the jar files of classpath are not loaded in memory at startup, then say a class pack1.MyClass needs to be loaded (instantiated) from a jar file say myclasses.jar - in such a case, will myclasses.jar be loaded in memory and remain loaded till the jvm process is on ? Or myclasses.jar is loaded, the class file is extracted and then the jar file is unloaded ?

    jschell wrote:
    anurag.kapur wrote:
    1. Are the jar files specified in classpath of a java program loaded in memory at the jvm startup ?
    No. Only class files are loaded in memory.
    2. If all the jar files of classpath are not loaded in memory at startup, then say a class pack1.MyClass needs to be loaded (instantiated) from a jar file say myclasses.jar - in such a case, will myclasses.jar be loaded in memory and remain loaded till the jvm process is on ?
    Only packk1.MyClass would be loaded in the JAVA heap. Not the JAR file.
    However there is no guarantee that the Class file remains loaded for the entire life cycle of the JVM process. Depending on the heap size available classes may be unloaded in a Garbage Collection cycle to make room of new Classes.
    Wrong. A class can not be unloaded unless the the class loader is collected by the GC. That means that a Class can actually be unloaded! That's what I said!
    The GC will not collect any class, normal java.lang.Class or class loader with active references even if the VM runs out of memory.True. Classes in perm gen and objects in the new and tenured generations get collected/unloaded only when they have no active references and there is a need for a collection cycle. Thats obvious. Did I actually ever say that a class would get unloaded even if it has an active reference? All I meant to say was that there is a possibility that a class gets unloaded and doesn't remain in the Heap for the entire lifecycle of the JVM.
    Am I actually getting anything wrong here? Please explain if so.
    Thanks
    Anurag

  • Jar files need for Custom connector API app

    hi all,
    I am doing an application regarding "Custom connector API", for doing the application I need "jdom,rome-0.6" jar files. kindly send these jar files it will be helpful to me. To do such custom connector API application any plugins must be added to  it, if so please let me know???
    thanks in advance,
    Aravind.

    Hi,
    You can find the jdom.jar file from the plugins/com.sap.km.rfwirad_7.1.5/lib/jdom.jar.
    If you add classlocator to your studio you can find the required jar file for your imported classes very easily. To add the classlocator to your studio go to the below blog
    Using JAR Class Finder
    Regards
    Suresh

  • How to add jar files to my custom component

    Hi,
    I have created a custom component through component wizard. I have to add jars, so the component will execute. Where to add those jar files? I tried adding it in classes folder, but its not working. Where to add it? and where else i have to make any changes?
    Please provide inputs.
    Thanks in advance

    in the Component manifest.hda file, mention the jar files to be installed...see example below
    @ResultSet Manifest
    2
    entryType
    location
    component
    MyCustomComponent/MyCustomComponent.hda
    componentClasses
    MyCustomComponent/classes/
    componentExtra
    MyCustomComponent/readme.htm
    componentClasses
    MyCustomComponent/classes/
    componentClasses
    MyCustomComponent/apachepoi/poi-3.8-20120326.jar
    componentClasses
    MyCustomComponent/apachepoi/poi-ooxml-3.8-20120326.jar
    componentClasses
    MyCustomComponent/apachepoi/poi-ooxml-schemas-3.8-20120326.jar
    componentClasses
    MyCustomComponent/apachepoi/stax-api-1.0.1.jar
    componentClasses
    MyCustomComponent/apachepoi/xmlbeans-2.3.0.jar
    componentClasses
    MyCustomComponent/apachepoi/ooxml-schemas-1.1.jar
    componentExtra
    MyCustomComponent/java/
    @endnext step is to add classpath in MyCustomComponent.hda file in LocalData...see example below
    @Properties LocalData
    blFieldTypes=
    preventAdditionalComponentDowngrade=0
    hasPreferenceData=0
    blDateFormat=M/d{/yy}{ h:mm[:ss]{ a}}!mAM,PM!tAmerica/Chicago
    classpath=$COMPONENT_DIR/classes:$COMPONENT_DIR/apachepoi/poi-3.8-20120326.jar:$COMPONENT_DIR/apachepoi/poi-ooxml-3.8-20120326.jar
    classpathorder=50
    ComponentName=MyCustomComponent

  • Creating an executable jar-file including a custom package

    I am trying to learn how to create executable jar-files. I have managed this with a single class. However having a class which is an extention of another class which in turn belongs to custom package, I can't make it work.Running the program normally is not a problem. I don't think the problem is the actual code, but I will include it here anyway. I create the jar-file using:
    jar -cmf Direkt2b.txt Direkt2b.jar *.class
    where Direkt2b.txt is just:
    Main-Class: Diriekt2b
    Only the main class-file is included this way. Copying the other file to the same directory before creation doesn't help either. There is no error messages, the jar-file just refuses to run no matter what.
    The main-class:
    import java.awt.*;
    import extra.*;
    class Direkt2b extends ExtendedFrame {
         Font f;
         Direkt2b() {
              f = new Font("SansSerif", Font.BOLD, 24);
              setBackground(Color.yellow);
              setSize(400,150);
         public void paint(Graphics g) {
              g.setFont(f);
              g.setColor(Color.blue);
              g.drawString("V?lkommen till Java Direkt", 45, 100);
         public static void main (String[] arg) {
              Direkt2b d2 = new Direkt2b();
              d2.setVisible(true);
    The help-class:
    package extra;
    import java.awt.*;
    import java.awt.event.*;
    public class ExtendedFrame extends Frame {
         private static boolean first = true;
         private boolean isFirst = first;
         public ExtendedFrame() {
              addWindowListener(theListener);
              first=false;
         WindowAdapter theListener = new WindowAdapter () {
              public void windowClosing(WindowEvent e) {
              dispose();
              if (isFirst)
                   System.exit(0);
    }

    My problem wasn't really getting more than one class in a jar-file,
    but rather that my jar-file with multiple classes wouldn't execute.
    Reading the original post I realized that maybe I was unclear about that.
    However I have solved the problem now so for anybody reading this
    thread in the future I will tell you how.
    The general setting is this: You have a bunch of self-made help-classes
    organized in some classdirectory somewhere in the filesystem. You use these
    in your programming from time to time. You have set a classpath-varible
    in your OS such that Java could find your help-classes.
    Now you want to create executable jar-files so that you could publish your work.
    My problem was that while I added all needed classes in a jar-file, the main-class
    couldn't find the others because they were referenced incorrectly. The solution I
    came up with may not be elegant, but it works. I copied the main-class and all directories
    (only one in my case) just as they appear in the classpath-directory, to the same temporary
    directory. Then I created the jar-file from there, using: jar -cmfv Direkt2b.txt Direkt2b.jar Direkt2b.class extra/*.class
    I suspected that incorrect referencing was the problem and I tried several variations on the theme above
    before giving up and writing the first post. Part the problem was that I didn't get any error messages,
    just an annoying error-sound double-clicking the jar-file. I learned later that you could run jar-files from
    the command -prompt using: java -jar myjarfile.jar. This way you get a more useful response.
    I guess there is a way to include the classpath instead somehow, but I will use my method for now.
    I hope this was helpful to somebody.

  • How can I include 3rd party jar files with my custom extention?

    I'm writing an extension that is dependent upon some 3rd party jars. How can I include/package these with my extension?
    we're using the 11g tech preview
    thanks,
    Dylan

    Do you mean that you need these files to appear in JDeveloper as a library available for the developers who build a project and use your extension?
    You can create a library in JDeveloper with JAR files by simply adding the following to your extension.xml file:
    <jdeveloper-hook xmlns="http://xmlns.oracle.com/jdeveloper/1013/extension">
    - <libraries xmlns="http://xmlns.oracle.com/jdeveloper/1013/jdev-libraries">
    <library name="My Extension Library">
    <classpath> ${ide.extension.install.home}/lib/some.jar</classpath>
    </library>
    </jdeveloper-hook>

  • Can't execute a main class with jar file loaded on command line

    Hello Guys,
    I try to use the following command to execute a main metod from msword class.
    java -cp f60all.jar:jacob.jar msword
    and i receive:
    Exception in thread "main" java.lang.NoClassDefFoundError: msword
    If i extract the 2 jar files then i can execute the main metod:
    java msword
    Can anyone explainme whet is happening ?
    Thanks,
    /Marian

    Hi!
    You need to include the directory where your class is in your classpath... you can use for this the "."
    So, you should write something like
    java -cp .;f60all.jar;jacob.jar msword
    and this supposing that "f60all.jar", "jacob.jar" and "msword.class" are all in the same directory from which you are trying to run the program... in other case write the full paths for the files...
    Bye!

  • Problem with jar file & loading manifest file

    Hi Friends
    i developing one application using java Swing
    i deploying the whole application using the jar Utility.
    but while installing & deploying this application on my machine & on the another machine error occured
    which showing that failed to load manifestfile from main module Or Main class.
    why this is happen? any other Java settings OR changing in java settings of the machine required while deploying this application on the other machine
    please guide me.

    Show the contents of the manifest file,
    and what command you used to run the program,
    and what directory you ran the command from.
    and what's the current classpath setting (if any).

  • Raw files loading as custom WB not As Shot

    My CR2 and DNG files are loading into Lightroom 2.6 and CS4 with a distinct blue cast.  Previews are fine, but when loaded they come in as custom WB not As Shot.  JPEGS are all fine.  If I click on As Shot in the develop module, all is okay.  This does not happen on my laptop, only on my PC.  It also only happens with my Canon 40D, not my 7D or G10.  This is a new problem that I have not had before.
    i recently loaded ACR 5.6 to support the 7D, and I am thinking that I have a setting off somewhere, but I don't know where.

    Check that you're not applying a develop preset within the Import dialog or set a custom camera default for your 40D. To clear a custom camera default you'll need to select a 40D image in Develop module, choose Set Camera Settings... from Develop menu then click on button labelled Restore Adobe Default Settings

  • Replacing jar files within ear

    Hi
    Once you package your component as an EAR and deploy it in WebLogic/Jboss is it possible to replace one or more JARs within the EAR without actualy having to recreate the EAR?
    Sudhi

    Yes, just use the jar update command, e.g.
    jar uvf My.ear JarInEar.jar
    replaces file JarInEar.jar in My.ear.

Maybe you are looking for