File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Not able to load Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not able to load Class" Watch "Not able to load Class" New topic
Author

Not able to load Class

Sandeep Advani
Ranch Hand

Joined: Mar 11, 2004
Posts: 78
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

Joined: Mar 11, 2004
Posts: 78
Apologies for bad indentation ! It didnt work.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You need to use the UBB [CODE]...[/CODE] tags for preformatted text such as code snippets. See What is UBB Code for details.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Mar 11, 2004
Posts: 78
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
 
 
subject: Not able to load Class