The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

loading class from different path

Posted by Pavan Kumar Keely on January 21, 2002 at 3:56 AM

> hi, i hope somebody help me...
> how i could load a class that is in a directory diferent to the main....
> for example:
> if the main is in c:\main
> and the class is in c:\another

> thanks a lot...


Hi,

You can load the classes dynamically from any path, if you can read the file (class file). The procedure is as follows...

I am giving a simple program which will do this...


public class ScriptExecutionManager extends ClassLoader
{
public void executeScript(String fileWithPath)
{
try{
File file = new File(fileWithPath);
FileInputStream fis = new FileInputStream(file);
int available = fis.available();
byte bytes[] = new byte[available];
fis.read(bytes);
Class scriptClass = this.defineClass("Script",bytes,0,bytes.length);
netpod.liveupdate.ScriptInter script = (netpod.liveupdate.ScriptInter)scriptClass.newInstance();
script.start();
script = null;
scriptClass = null;
file.delete();

}catch(Exception e){e.printStackTrace();}

}

// for debugging purpose
public static void main(String s[])
{
new ScriptExecutionManager().executeScript("d:/install/Script.class");

}
}


If you can't understand this...please do mail me to keelypavan@hotmail.com...

bye



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us