thanks a lot,

I followed your suggestion and found this class on the net:

import java.lang.reflect.*;
import java.io.*;
import java.net.*;

// many thanks to antony_miguel
public class ClassPathHacker {
   private static final Class[] parameters = new Class[]{URL.class};
     public static void addFile(String s) throws IOException {
       File f = new File(s);
       addFile(f);
   }//end method
     public static void addFile(File f) throws IOException {
       addURL(f.toURL());
   }//end method

   public static void addURL(URL u) throws IOException {
       URLClassLoader sysloader =
                (URLClassLoader)ClassLoader.getSystemClassLoader();
       Class sysclass = URLClassLoader.class;
             try {
           Method method =
                sysclass.getDeclaredMethod("addURL",parameters);
           method.setAccessible(true);
           method.invoke(sysloader,new Object[]{ u });
       } catch (Throwable t) {
           t.printStackTrace();
           throw new IOException("Error, could not add URL to " +
                   "system classloader");
       }//end try catch
   }
}

It works really fine and in two line of code I'm able to add a jarfile or a classfolder to CLASSPATH

    File actorLibFile=new
        File("pathToJar"+File.separatorChar+"fooActorLib.jar");
    ClassPathHacker.addFile(actorLibFile.getAbsolutePath());

or

    File classDir= new File("pathToClassDir" + File.separatorChar);
    ClassPathHacker.addURL(classDir.toURL());


Edward A. Lee wrote:

I think you could also customized the classpath, no?
It won't be able to include a remote URL, however...

Edward

At 07:51 AM 9/8/2006, Christopher Brooks wrote:

I'm fairly certain you would need to have the actor in
the classpath.  It might be possible to define a custom ClassLoader
that would have the actor in question in the classpath.  However,
things get tricky.

We thought about using a custom URLClassLoader to help with reloading
classes, see  the "removing and readding actors to a model"
thread from Nov 21, 2003 in ptolemy hackers

This URL might work
http://groups.google.com/group/comp.soft-sys.ptolemy/browse_thread/thread/a5e43aa5c4366c03/92699b333c66c9cd?lnk=st&q=ptolemy+classloader&rnum=6&hl=en#92699b333c66c9cd

or
http://www.mail-archive.com/ptolemy-hackers@eecs.berkeley.edu/msg01101.html

> Hi all,
> I have a problem. I need to instantiate actors not located in Ptolemy
> classPath.
> I tried using vergil menu' Graph->Instantiate Entity, setting the field
> "Location (URL)" with the actor's path.
> It seems to work only with Xml actors.
> It doesn't work with Java actors (*.class) and with any actor
> incapsulated in a JAR file.
> Any suggestion? No matter if I have to write some code.
>
> I'm using Ptolemy version 5.0.1 and java version "1.5.0_04".
>
> Thanks,
> Adriana

----------------------------------------------------------------------------
Posted to the ptolemy-hackers mailing list.  Please send administrative
mail for this list to: [EMAIL PROTECTED]


------------
Edward A. Lee
Professor, Chair of EECS
231 Cory Hall, UC Berkeley, Berkeley, CA 94720-1770
phone: 510-642-0253 or 510-642-0455, fax: 510-642-2845
[EMAIL PROTECTED], http://ptolemy.eecs.berkeley.edu/~eal

Adriana

----------------------------------------------------------------------------
Posted to the ptolemy-hackers mailing list.  Please send administrative
mail for this list to: [EMAIL PROTECTED]

Reply via email to