Ug. what is wrong with this classloader???

T

Troy Makaro

Hi, I keep getting the following error:



java.lang.NoClassDefFoundError: com/mydomain/tst/TestClass (wrong name:
com/mydomain/tst/TestClass)

at java.lang.ClassLoader.defineClass0(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:502)

at java.lang.ClassLoader.defineClass(ClassLoader.java:431)

at com.mydomain.loader.TNTClassLoader.findClass(TNTClassLoader.java:194)

at com.mydomain.loader.TNTClassLoader.loadClass(TNTClassLoader.java:115)

at com.mydomain.loader.TNTClassLoader.loadClass(TNTClassLoader.java:104)

at java.lang.ClassLoader.loadClass(ClassLoader.java:255)

at com.mydomain.prm.bo.Test.redirectCheck(Unknown Source)

... 40 more



What causes this? Here is my code: (Search for the 'Chokes here') Note: I
am running under java 1.4



======================================================

package com.mydomain.loader;



import com.mydomain.rmi.*;

import com.mydomain.util.*;

import com.mydomain.exceptions.*;

import java.util.*;

import java.io.*;

import java.net.*;



/**

* Used to load TNT classes. TNT classes start with com.mydomain. There will
be

* one class loader for each environment file.

*

* @author Troy

* @version $Revision$

*/

public class TNTClassLoader extends ClassLoader {



private Context _context;

private ClassLoader _common;

private String _product;

private String _version;

private String _classKey;



private TNTClassLoader(Context context, ClassLoader common) {

super(common);

_context = context;

_common = common;



InsensitiveProperties p = _context.getEnvironment();

_product = p.getProperty("product");

_version = p.getProperty("version");

_classKey = _product + _version;

_classKey = _classKey.toLowerCase().trim();

} // constructor



protected synchronized Class loadClass(String name, boolean resolve)
throws java.lang.ClassNotFoundException {



boolean useTNTLoader = true;

String product = null;

if (name.startsWith("com.mydomain.")) {

int lastDot = name.lastIndexOf(".");

// should always be greater than 12 unless the package name is
'com.mydomain'

if (lastDot > 12) {

// match the product name to the product field in the
environment field

product = name.substring(13, lastDot);

} // end if

// ignore core packages

if (product == null ||
StrU.inStr(1,"/util/data/rmi/loader/","/"+product+"/") != 0) {

useTNTLoader = false;

} // end if

} else {

useTNTLoader = false;

} // end if



// use the regular classloader

if (!useTNTLoader) {

System.out.println("using parent classloader");

Class c = _common.loadClass(name);

if (resolve) {

resolveClass(c);

} // end if

return c;

} // end if



// match the product name to the product field in the environment
field

if (_product == null) {

throw new ClassNotFoundException("environment: " +
_context.getEnvironmentName() +

" does not define the product
field.");

} // end if



// make sure we are using the correct classloader

if (!product.equalsIgnoreCase(_product)) {

// the product doesn't match. we need a different classloader

// find the name of the actuall environment

InsensitiveProperties p = _context.getEnvironment();

String actualEnvironment = p.getProperty(product +
"Environment");

if (actualEnvironment == null) {

throw new java.lang.ClassNotFoundException(

"Actual environment could not be found, environment: " +

_context.getEnvironmentName() + ", product: " +
product);

} // end if

InsensitiveProperties pp =
_context.getEnvironment(actualEnvironment);

String checkProduct = pp.getProperty("product");

if (checkProduct == null) {

throw new ClassNotFoundException("environment: " +
actualEnvironment +

" does not define the
product field.");

} // end if

if (!checkProduct.equalsIgnoreCase(product)) {

throw new ClassNotFoundException("unable to find product -
environment: " +


_context.getEnvironmentName() +

", product: " + product);

} // end if

Context context;

context = (Context)_context.clone();

context.setEnvironmentName(actualEnvironment);

TNTClassLoader loader = getClassLoader(context, _common);

return loader.loadClass(name, resolve);

} // end if



/** @todo get class here */

System.out.println("using TNT classloader");



// First, check if the class has already been loaded

Class c = findLoadedClass0(name);

if (c == null) {

// If still not found, then call findClass in order

// to find the class.

c = findClass(name);

} // end if



if (resolve) {

resolveClass(c);

} // end if

return c;



} // loadClass



private Class findLoadedClass0(String name) {

// StaticClassCache is a synchronized cache

return StaticClassCache.getClass(_classKey,name);

} // findLoadedClass0



public static synchronized TNTClassLoader getClassLoader(Context
context, ClassLoader parent) {

String key = "System.classloader."+context.getEnvironmentName();

TNTClassLoader loader = (TNTClassLoader)StaticLoaderCache.get(key);

if (loader == null) {

loader = new TNTClassLoader(context,parent);

StaticLoaderCache.put(key,loader);

} // end if

return loader;

} // getClassLoader



protected Class findClass(String name) throws
java.lang.ClassNotFoundException {

String fileName = name.replace('.','/');

File file = new
File(_context.getLocation(),_product+_version+"/busobjs/"+fileName+".class")
;

System.out.println(file);

if (!file.exists()) {

throw new ClassNotFoundException(name);

} // end if





// Looking up the package

String packageName = null;

int pos = name.lastIndexOf('.');

if (pos != -1)

packageName = name.substring(0, pos);



Package pkg = null;



if (packageName != null) {

System.out.println("the package name is: "+packageName);

pkg = getPackage(packageName);

if (pkg == null) {

System.out.println("the package does not exist");

} else {

System.out.println("the package exists");

System.out.println(pkg.getImplementationTitle());

System.out.println(pkg.getImplementationVendor());

System.out.println(pkg.getImplementationVersion());

System.out.println(pkg.getName());

}



// Define the package (if null)

if (pkg == null) {

definePackage(packageName, "spectitle", "specversion",
"specvendor",

"impltitle", "implversion",

"implVendor", null);



} // end if

} // end if



byte[] buffer = new byte[8000];

try {

FileInputStream fis = new FileInputStream(file);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

BufferedOutputStream bos = new BufferedOutputStream(baos);

int result = 0;

while ((result = fis.read(buffer)) != -1) {

bos.write(buffer,0,result);

} //wend

fis.close();

bos.flush();

byte[] bytes = baos.toByteArray();

bos.close();

System.out.println("number of bytes in class is "+bytes.length);

System.out.println("trying to define class: "+name+"|");

Class c = defineClass(name, bytes, 0, bytes.length); //
<----Chokes here!!!!

System.out.println("class is defined!");

return c;

} catch (IOException ex) {

throw new ClassNotFoundException(ex.toString());

} // try

} // findClass



} // class
 
J

John C. Bollinger

Troy said:
Hi, I keep getting the following error:



java.lang.NoClassDefFoundError: com/mydomain/tst/TestClass (wrong name:
com/mydomain/tst/TestClass)

at java.lang.ClassLoader.defineClass0(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:502)

at java.lang.ClassLoader.defineClass(ClassLoader.java:431)

at com.mydomain.loader.TNTClassLoader.findClass(TNTClassLoader.java:194)

at com.mydomain.loader.TNTClassLoader.loadClass(TNTClassLoader.java:115)

at com.mydomain.loader.TNTClassLoader.loadClass(TNTClassLoader.java:104)

at java.lang.ClassLoader.loadClass(ClassLoader.java:255)

at com.mydomain.prm.bo.Test.redirectCheck(Unknown Source)

... 40 more



What causes this? Here is my code: (Search for the 'Chokes here') Note: I
am running under java 1.4

That happens when the class name passed to ClassLoader.defineClass
differs from the class name specified by the bytes from which the class
definition is to be constructed. You may be able to figure out what is
going on by passing null as the class name to defineClass, and then
comparing the name of the returned Class to the name you expected.

Overall, however, you seem to be doing quite a bit more work than you
need to do. Is there a reason why you can't just use one or more
properly configured URLClassLoaders for your task?


John Bollinger
(e-mail address removed)
 
T

Troy Makaro

I apologize for wasting your time. As it turns out there was a case
sensitive spelling mistake. I looked it over a dozen times looking for just
that and missed it. You would have never found it because I copied the error
incorrectly.

java.lang.NoClassDefFoundError: com/mydomain/tst/TestClassloader (wrong
name:
com/mydomain/tst/TestClassLoader)
Thank you for your help
Troy
 
L

Lee Fesperman

Troy said:
I apologize for wasting your time. As it turns out there was a case
sensitive spelling mistake. I looked it over a dozen times looking for just
that and missed it. You would have never found it because I copied the error
incorrectly.

Thanks for posting. My intuition was not to dig through the mass of code you posted.
java.lang.NoClassDefFoundError: com/mydomain/tst/TestClassloader (wrong
name: com/mydomain/tst/TestClassLoader)

I did look at that message and, being appropriately confused by it, decided not to go
further.

Cut&paste is your friend ;^)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top