Naresh Agarwal a écrit:
Hi
I have class MyClass, built in jar file MyClassJar.jar
This class uses a class ABC, which is built in ABCJar.jar
I've MyClassJar.jar in jre/lib/ext and ABCJar.jar in system classpath.
But while running the MyClass Java program, I get following exception
-
java.lang.NoClassDefFoundError: ABC at MyClass.main (MyClass.java:9)
However, if I put ABCJar.jar also in jre/lib/ext, it works fine
Does this mean that if I put a Jar file in jre/lib/ext, all its
dependent jar files must be in jre/lib/ext only.
thanks & regards,
Naresh Agarwal
Hi
I had a related problem for running classes edited inside the GUI of my
image processing program. The method I want to fire is "main" but the
generalisation is obvious
The context is a bit different, but I adapted a solution found on the
web (I do not remember where) which consists of using a home-made
classloader which looks for the new classes in a special directory.
Here is the code ; perhaps it can help you (the comments are in french)
Sincerely yours
**********************************************
// exécution
private void doExec()
{ try
{ ScrClassLoader scl = new ScrClassLoader(classDir);
Class clas = scl.loadClass(sNam,true);
Method main = clas.getMethod("main",null);
main.invoke(null,null);
}
catch (Exception ex)
{ mesEdt.writeln(ex);
StackTraceElement[] ste = ex.getStackTrace();
for (int i = 0; i < ste.length; i++)
mesEdt.writeln(ste
.toString());
}
}
*************************************************
// charge une classe à partir d'un fichier
class ScrClassLoader extends ClassLoader implements Const
{ String dir; // directory où chercher les classes
public ScrClassLoader(String dir)
{ this.dir = dir; }
public Class loadClass( String name, boolean resolve )
throws ClassNotFoundException
{ Class clas;
try
{ if ((clas = findLoadedClass(name)) != null)
return clas;
}
catch(Exception e) { }
try
{ if ((clas = findSystemClass(name)) != null)
return clas;
}
catch(ClassNotFoundException e) { }
byte[] clasBytes;
try
{ FileInputStream inStream = new FileInputStream(dir+fSep+name+".class");
clasBytes = new byte[inStream.available()];
inStream.read(clasBytes);
inStream.close();
if (clasBytes == null) throw new
ClassNotFoundException("ScrClassLoader "+name);
}
catch (IOException e)
{ throw new ClassNotFoundException("ScrClassLoader IOException "+name); }
clas = defineClass(name,clasBytes,0,clasBytes.length);
if (clas == null) throw new ClassNotFoundException("ScrClassLoader
"+name);
if (resolve) resolveClass(clas);
return clas;
}
}
*************************************************