• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to load Class

 
Sandeep Advani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandeep Advani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for bad indentation ! It didnt work.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use the UBB [CODE]...[/CODE] tags for preformatted text such as code snippets. See What is UBB Code for details.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your problem isn't with class loading but with locating the .class file you've uploaded? If you do a File#exists(), does it return true or false?
 
Sandeep Advani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error was due to failure for checking if the file was a system class. I had to add a line -

Class classRef = findSystemClass(filename);

at the very beginning before loading the class data.

Now I get the idea clear; I have a web service which accepted byte codes from clients and load it on the fly. Once bytecodes arrive, they are already inside the JVM and does not require loading classdata explicitly. Just a check if its a system class (which was handled by JVM) was appropriate.

Thanks for indentation tip though :-)

Sandeep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic