using actors not in CLASSPATH

using actors not in CLASSPATH

Post by aricchiut » Sat, 09 Sep 2006 23:02:11


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: XXXX@XXXXX.COM
 
 
 

using actors not in CLASSPATH

Post by cxh » Sat, 09 Sep 2006 23:51:30

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://www.yqcomputer.com/ +classloader&rnum=6&hl=en#92699b333c66c9cd

or
http://www.yqcomputer.com/ @XXXXX.COM /msg01101.html


----------------------------------------------------------------------------
Posted to the ptolemy-hackers mailing list. Please send administrative
mail for this list to: XXXX@XXXXX.COM

 
 
 

using actors not in CLASSPATH

Post by aricchiut » Sat, 16 Sep 2006 22:27:03

hanks 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:

Adriana

----------------------------------------------------------------------------
Posted to the ptolemy-hackers mailing list. Please send administrative
mail for this list to: XXXX@XXXXX.COM