Help!
I have a server application which tries to load a class from the current directory and execute it. This server application has a helper file called Helper.java which extends ClassLoader. My server applications calls the loadclass method passing the name of the .class file to load. Code snippet is as follows.
File Helper.java:
...
public synchronized Class loadClass(
String name, boolean resolve ) throws ClassNotFoundException
{
Class classRef = null;
byte [] classData = null;
System.out.println( " Loading..." );
try
{
classData = loadClassData( name );
}
catch( FileNotFoundException e )
{
System.out.println("Exception:" + e.getMessage() +" not found." );
}
catch( IOException e )
{
System.out.println("Problem reading file " + e.getMessage() );
}
try
{
classRef = defineClass( name, classData, 0, classData.length );
}
catch( Exception e )
{
System.out.println( e.getMessage() );
}
....
} //end of loadclass() method
public String getCurrentPath()
{
return sCurrentPath;
}
public void setCurrentPath( String s )
{
sCurrentPath = new String( s );
}
private byte loadClassData( String filename )[]
throws IOException, FileNotFoundException
{
// Open the .class file to read.
File file = new File( getCurrentPath(), filename + ".class" );
byte[] classData=null;
try
{
// print out the path and filename.
System.out.println( " Path: " + file.getPath() );
// Allocate an array to hold the class data based on the amount
// of data in the .class file.
classData = new byte[ (int)file.length() ];
// create a stream to read from the file.
FileInputStream fileStream = new FileInputStream( file );
// read data from the file into the array.
int result = fileStream.read( classData );
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Returning class data : "+file.length());
return classData;
}
...
} //end of class Helper.java
My file length is returning ZERO. I tried running this .class file through command line which runs perfect.
Code snippet from Server.java is:
...
Helper loader = new Helper();
loader.setCurrentPath("../Server");
Class classRef = loader.loadClass( "Test" );
Object objectRef = classRef.newInstance();
if(objectRef instanceof ITest)
{
ITest testClass = (ITest) objectRef;
testClass.test();
}
....
Any suggestions will be helpful. Thanks.
Sandeep