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
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