Problem with multiple classes

W

wapo

Hi all,

I am rather new to Java and I have two questions to ask:

a) I have a project where I import a jar file in order to use some
classes. For one of the classes in the jar I have written my own
version and I want to force the project to use my class instead of the
one provided by the jar. I could simply rename my class to something
else but I want this class to be used by the classes in the jar as
well. Is there a way to suppress the class provided by the jar.
b) This particular class in the jar creates another problem to me
(even if I decide to keep using it). My project compiles but when I
try to run it I get an error of the form
java.lang.NoClassDefFoundError. I know that this has to do with the
classes not being found. I read on the net that I need to fix the
classpath but how/why. The jar (where the class reside) has been
imported to the project. The compiler finds it during compiling. The
answer I found on the net had to do with the addition of ./; in the
classpath but that didnt help. Any other ideas?

I am using Eclipse/WinXP/J2ME

Thanks in advance
 
J

John B. Matthews

[...]
a) I have a project where I import a jar file in order to use some
classes. For one of the classes in the jar I have written my own
version and I want to force the project to use my class instead of
the one provided by the jar. I could simply rename my class to
something else but I want this class to be used by the classes in the
jar as well. Is there a way to suppress the class provided by the
jar.

If your class replaces the original, why keep the original class in the
JAR at all?
b) This particular class in the jar creates another problem to me
(even if I decide to keep using it). My project compiles but when I
try to run it I get an error of the form
java.lang.NoClassDefFoundError. I know that this has to do with the
classes not being found. I read on the net that I need to fix the
classpath but how/why. The jar (where the class reside) has been
imported to the project. The compiler finds it during compiling. The
answer I found on the net had to do with the addition of ./; in the
classpath but that didnt help. Any other ideas?

If you "need to reference classes in other JAR files from within a JAR
file," you can specify the Class-Path in the JAR's manifest file:

<http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html>

See "Packaging Programs in JAR Files," here:

<http://java.sun.com/docs/books/tutorial/deployment/jar/index.html>
 
W

wapo

If your class replaces the original, why keep the original > class in the JAR at all?

Do you mean to just open the jar and delete the class? Cause I cannot
recreate it (or can I? Remember I am new to this stuff). It was given
to me.
 
S

steen

Do you mean to just open the jar and delete the class? Cause I cannot
recreate it (or can I? Remember I am new to this stuff). It was given
to me.

A jar file is just a zipfile, so just open it with your favorite tool
(winzip, winrar, etc) and delete the offending class.

/Steen
 
P

Pushkaraj

A jar file is just a zipfile, so just open it with your favorite tool
(winzip, winrar, etc) and delete the offending class.

/Steen

I suggest to replace classes in jar with your classes instead deleting
them.
 
J

John B. Matthews

wapo said:
I want to force the project to use my class [...]
Is there a way to suppress the class provided by the jar.

To which I responded:

And you replied:
Do you mean to just open the jar and delete the class?

That's one approach.
Cause I cannot recreate it (or can I? Remember I am new to this stuff).

Which is why I recommended the tutorial:

It was given to me.

Can you elaborate?
 
L

Lew

Pushkaraj said:
I suggest to replace classes in jar with your classes instead deleting
them.

All of the above is essentially trouble. Name your class something different
and don't try to replace the JAR class.
 
W

wapo

Can you elaborate?


Ok, here it goes. Its this library jjil. Its for image processing for
J2ME. They have this jar file that contains many different classes. I
need to change some segments of the source code in some of the classes
in the jar. Following your advice I opened the jar, deleted the
classes that I want to edit and imported in my project the java files
they provide. If I didn't delete the classes from the jar then I get
the error java.lang.reflect.invocationtargetexception. I modify the
java code, build and then import the generated .class in the jar.
After this step I end up with the modified jar and the Main.java that
uses the jar. When I try to run from inside Eclipse I get an
java.lang.NoClassDefFoundError: Main
 
W

wapo

Yes,

but I need to change the code for some classes. If I create new
classes the dependences inside the jar wont be updated. Lets say for
example that there is an Error.class (error handling). If I go like
MyError extends Error{} then everything inside the jar continues to
use the Error.class while I want them to use my MyError.

Is that correct or am I missing something?
 
J

John B. Matthews

wapo said:
Ok, here it goes. Its this library jjil. Its for image processing for
J2ME. They have this jar file that contains many different classes. I
need to change some segments of the source code in some of the classes
in the jar. Following your advice I opened the jar, deleted the
classes that I want to edit and imported in my project the java files
they provide. If I didn't delete the classes from the jar then I get
the error java.lang.reflect.invocationtargetexception. I modify the
java code, build and then import the generated .class in the jar.
After this step I end up with the modified jar and the Main.java that
uses the jar. When I try to run from inside Eclipse I get an
java.lang.NoClassDefFoundError: Main

Why not just checkout the source tree, edit the relevant file(s) and
build a new jar using Eclipse?

<http://code.google.com/p/jjil/source/checkout>

You may want to examine the license terms (LGPL) before you distribute a
modified version.

IANAL.
 
W

wapo

This is what I decide to do. It turns out that when importing the jar
in the project (without doing anything else; so I didnt use anything
from the jar, just had it imported) Eclipse wouldnt preverify. The
errors that I got during the package creation would be:

Errors during build.
Errors running builder 'Preverification' on project 'FaceDetection'.
org.eclipse.jdt.internal.core.JavaModel.getTarget(Lorg/eclipse/core/
resources/IContainer;Lorg/eclipse/core/runtime/IPath;Z)Ljava/lang/
Object;
org.eclipse.jdt.internal.core.JavaModel.getTarget(Lorg/eclipse/core/
resources/IContainer;Lorg/eclipse/core/runtime/IPath;Z)Ljava/lang/
Object;

So, now I am looking at that plus trying to make up the lost time by
using the sources directly.

Thanks everybody for your help
 
R

Roger Lindsjö

wapo said:
This is what I decide to do. It turns out that when importing the jar
in the project (without doing anything else; so I didnt use anything
from the jar, just had it imported) Eclipse wouldnt preverify. The
errors that I got during the package creation would be:

Errors during build.
Errors running builder 'Preverification' on project 'FaceDetection'.
org.eclipse.jdt.internal.core.JavaModel.getTarget(Lorg/eclipse/core/
resources/IContainer;Lorg/eclipse/core/runtime/IPath;Z)Ljava/lang/
Object;
org.eclipse.jdt.internal.core.JavaModel.getTarget(Lorg/eclipse/core/
resources/IContainer;Lorg/eclipse/core/runtime/IPath;Z)Ljava/lang/
Object;

So, now I am looking at that plus trying to make up the lost time by
using the sources directly.

Thanks everybody for your help

I think you get the error because the original classes are already
preverified for J2ME. That process makes the classfiles invalid for
regular classloaders.
 
A

Arved Sandstrom

Yes,

but I need to change the code for some classes. If I create new classes
the dependences inside the jar wont be updated. Lets say for example
that there is an Error.class (error handling). If I go like MyError
extends Error{} then everything inside the jar continues to use the
Error.class while I want them to use my MyError.

Is that correct or am I missing something?

That's right - the other classes know nothing about MyError.

I wouldn't recommend this for production, but since the entries in the
user classpath are processed in order, if you have your own custom Error
class (retaining directory structure corresponding to the package) in
your classpath before the JAR, it will be used vice the Error class in
the JAR. This will at least suffice for development.

Since you are in a situation where other classes *in* the supplied JAR
must be pointed to something else, and I'm guessing you don't have
anything but the class files, in production you don't have much choice
but to replace the old Error with a new Error. Rename the modified JAR,
keep the old JAR around, and modify your classpath to use the renamed
JAR. Document all of this.

Out of curiosity, exactly what scenario is requiring you to do this? It's
not unheard of but it's not common either.

AHS
 
M

Mark Space

wapo said:
Yes,

but I need to change the code for some classes. If I create new
classes the dependences inside the jar wont be updated. Lets say for
example that there is an Error.class (error handling). If I go like
MyError extends Error{} then everything inside the jar continues to
use the Error.class while I want them to use my MyError.

Is that correct or am I missing something?

This is correct, however you are probably missing something anyway.

If the jar you call throws Error, then catch that and re-throw it as
MyError.

Likewise, you should consider encapsulation to change any given class X
into some other class Y. Pay attention when people are saying "this is
not for production." That means you can't really use code like you are
proposing anywhere. No one (customers, bosses, other IT folks) just
won't let you do it.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top