Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » java.lang.ClassFormatError: (Illegal UTF8 string in constant pool)
java.lang.ClassFormatError: (Illegal UTF8 string in constant pool) [message #282975] Thu, 24 March 2005 15:26
Eclipse UserFriend
Originally posted by: denis.roudet.rd.francetelecom.com

Hi everyone,

I am developing a plug-in in Eclipse which made automatic tests on Java
classes. So I have to load dynamically these classes. Then I write a
specific ClassLoader and it works with simple classes. But when I want to
load a class from a jar file I have got this error :

java.lang.ClassFormatError: core/ChetEnvir (Illegal UTF8 string in
constant pool)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.lang.ClassLoader.defineClass(ClassLoader.java:431)
at
fr.francetelecom.rd.chetBaker.actions.ChetBakerClassLoader.f indClass(ChetBakerClassLoader.java:147)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315 )
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:15 90)
at java.lang.Class.getConstructors(Class.java:848)
...



This a copy of my class in which I make my ClassLoader

public class ChetBakerClassLoader extends ClassLoader
{
private IJavaProject project;
private IPath rootLocation;
private Set/*[IClasspathEntry]*/ entries = new HashSet();;
private static int BUFSIZE = 2048;

/**Makes a new classloader in order to load classes in a specified
project.
* @param _cl The parent ClassLoader.
* @param _prj The specified project.
* @throws IllegalArgumentException
*/
public ChetBakerClassLoader(ClassLoader _cl, IJavaProject _prj)
throws IllegalArgumentException
{
super(_cl);
if (_prj == null)
new IllegalArgumentException("invalid null project ");
if (!_prj.exists() || !_prj.isOpen())
new IllegalArgumentException("invalid project " + _prj);
project = _prj;
rootLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation();
}

/**Makes a new classloader in order to load classes in a specified
project.
* @param _prj The specified project.
* @throws IllegalArgumentException
*/
public ChetBakerClassLoader(IJavaProject _prj) throws
IllegalArgumentException
{
this(null, _prj);
}

/**Stores the .classpath of the project in a set.
* @throws JavaModelException
*/
public void initPath() throws JavaModelException
{
IClasspathEntry[] ents = project.getResolvedClasspath(true);
for (int i = 0; i < ents.length; i++)
{
entries.add(ents[i]);
}

}

/* (non-Javadoc)
* @see java.lang.ClassLoader#findClass(java.lang.String)
*/
protected Class findClass(String _nm)
{
byte[] buf = null;
try {
buf = readBytes(_nm);
}
catch (Exception e) {
e.printStackTrace();
throw new Error("pb wheb reading bytes[]");
}
if (buf == null)
{
throw new Error("Empty buffer ");
}
return defineClass(_nm, buf, 0, buf.length);
}

private byte[] readBytes(String _nm) throws Exception
{
if (entries == null)
throw new Error("No path to load the class");

byte[] resultat = null;
Path relP2CFile = new Path(_nm.replace('.', '/') + ".class");
System.out.println("String pour jar : "+ relP2CFile.toString());
for (int i = 0; i < entries.size(); i++) {
IClasspathEntry entriesElement = (IClasspathEntry)
entries.toArray()[i];

if (entriesElement.getEntryKind() == IClasspathEntry.CPE_SOURCE)
{
IPath pth = entriesElement.getOutputLocation();

if (pth == null)
{
pth = project.getOutputLocation();
}
if (pth != null)
pth = rootLocation.append(pth);
else
pth = rootLocation;

if (pth.append(relP2CFile).toFile().exists())
{
return getBytesFromFile(pth.append(relP2CFile).toFile());
}
}
else
{
if (entriesElement.getEntryKind() == IClasspathEntry.CPE_PROJECT)
{
}
else
{
if (entriesElement.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
{
if (entriesElement.getContentKind() ==
IPackageFragmentRoot.K_BINARY)
{
IPath jarPath = entriesElement.getPath();
try
{
//Cas ou le .jar est un fichier externe au projet
if (jarPath.toFile().exists())
{
return getBytesFromJar(jarPath.toFile(),
relP2CFile.toString());
}
else
{
IPath jarPathInternal = rootLocation.append(jarPath);
if (jarPathInternal.toFile().exists())
{
return getBytesFromJar(jarPathInternal.toFile(),
relP2CFile.toString());
}
}
}
catch (ClassNotInThisJarException e)
{
//do nothing
}
}
}
}
}

}
throw new Error("Problem with the spot of the class");
}



private static byte[] getBytesFromFile(File _fil)
{
if (_fil == null || !_fil.exists())
throw new Error("Empty file or does not exist");
InputStream stm = null;
try {
stm = new BufferedInputStream(new FileInputStream(_fil));
byte[] buf = new byte[BUFSIZE];
int sz = 0;

while (true) {
int cnt = stm.read(buf, sz, buf.length - sz);
if (cnt < 0)
throw new Error ("Problème when reading file");
sz += cnt;
if (sz < buf.length)
break;

byte[] newBuf = new byte[buf.length + BUFSIZE];
System.arraycopy(buf, 0, newBuf, 0, sz);
buf = newBuf;
}

byte[] res = new byte[sz];
System.arraycopy(buf, 0, res, 0, sz);
return res;
}
catch (Exception ex) {
throw new Error("Problem with the transformation File->Bytes");

}
finally {
try {
if (stm != null) {
stm.close();
}
}
catch (IOException ex) {
throw new Error("Problem with closing InputStream");
}
}
}

// I THINK THIS
private static byte[] getBytesFromJar(File _jar, String _fil) throws
Exception
{
if (_jar == null || !_jar.exists())
throw new Error("Ce jar n'existe pas");
if (_fil == null)
throw new Error("La classe recherchée est vide");
// String fName = _fil.getName();
ZipFile zip = null;
InputStream stm = null;
byte[] res = null;
try {
zip = new ZipFile(_jar);
ZipEntry ent = zip.getEntry(_fil);
if (ent == null)
throw new ClassNotInThisJarException();
stm = zip.getInputStream(ent);
int sz = (int) ent.getSize();
res = new byte[sz];
int nbBytesRead = stm.read(res, 0, sz);



System.out.println("Taille de la classe dans le .jar : " + sz);
System.out.println("Nombres de bytes lues : " + nbBytesRead);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}

}
finally {
if (stm != null)
stm.close();
if (zip != null)
zip.close();
}
return res;
}
}
Re: java.lang.ClassFormatError: (Illegal UTF8 string in constant pool) [message #282979 is a reply to message #282975] Thu, 24 March 2005 14:15 Go to previous message
Eclipse UserFriend
Originally posted by: olivier_thomannNOSPAM.ca.ibm.com

Denis a écrit :
> Hi everyone,
>
> I am developing a plug-in in Eclipse which made automatic tests on Java
> classes. So I have to load dynamically these classes. Then I write a
> specific ClassLoader and it works with simple classes. But when I want
> to load a class from a jar file I have got this error :
>
> java.lang.ClassFormatError: core/ChetEnvir (Illegal UTF8 string in
> constant pool)
> at java.lang.ClassLoader.defineClass0(Native Method)
> at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
> at java.lang.ClassLoader.defineClass(ClassLoader.java:431)
> at
> fr.francetelecom.rd.chetBaker.actions.ChetBakerClassLoader.f indClass(ChetBakerClassLoader.java:147)
Does it work using the default classloader?
If not, then could you please open a bug report against JDT/Core with
the file ChetEnvir.class in the folder core as an attachment.
--
Olivier
Previous Topic:icons in table cells
Next Topic:How to remove a preference entry?
Goto Forum:
  


Current Time: Thu Apr 25 09:25:39 GMT 2024

Powered by FUDForum. Page generated in 0.03404 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top