problem with loading dependencies of a jar file in jre/lib/ext directory

N

Naresh Agarwal

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
 
A

aurengo

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

*************************************************
 
D

David Segall

Naresh Agarwal said:
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
Thanks to a response in this group and extensive googling I have
discovered two well kept secrets. The first is, if you launch your
application from a jar file, the system classpath is ignored. The
second is that you can include a space separated classpath in the jar
manifest file on a line beginning "Class-Path:".

No doubt Sun would deny they are secrets because they are documented
in the -jar option of the java command and in the Jar Manifest
Specification respectively but a tutorial on deploying java
applications using jars would have been a big help.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top