Classes in jar can't load - why?

J

Jack Andersson

I've just created a jar-file containing some classes that I've made. I have
made this jar file in Idea IntelliJ's AntBuild. However, when I try to use
the jar file in a project I get the following message:

cannot access Teleadress.DMP2004.DTDClasses.CompanyResult
bad class file
D:\IdeaProjects\DTDFileTest\lib\DMP2004_DTDClasses.jar(Teleadress/DMP2004/DT
DClasses/CompanyResult.class)
class file contains wrong Class: CompanyResult
Please remove or make sure it appears in the correct subdirectory of the
classpath

The thing is that it works perfectly when I open the jar-file with WinZip
and copy all the class-files to the classes folder in my project. Moreover
when I write Teleadress.DMP2004.DTDClasses. the autocomplete function shows
a list of all my classes including CompanyResult. However, as soon as I
chose CompanyResult it turns red meaning it can't be found. This is also the
case with all the other classes in my jar.

Why is this? I've checked other jars and the only thing that is different is
the manifest-file which in my case is more or less empty.
 
V

VK

You can't use WinZip to create/update jars, it messes up: just a little bit
here and there, but enough to spoil the jar structure (I guess it's made to
do not upset Sun lawyers with their patents).

Use jar.exe only

Use WinZip ONLY to view your jars. (By simply taking one file from jar thru
WinZip will possibly break your jar).
 
J

Jack Andersson

I didn't make the jar with winzip, I made it with AntBuild which in turn
uses jar.exe. What I did was to use WinZip to open the jar and then copy the
files to the classes folder.
 
J

Jack Andersson

Did you declare the class in the correct package?

I'm not sure I understand what you mean by this. Could you explain in detail
what you mean by declaring in the correct package?
 
J

Jack Andersson

That is, when it didn't work I copied all the .class-files to my classes
folder. Then it worked.
 
A

Andrea Desole

I mean that, when you are building a jar file, and the class MyClass is
in the file MyClass.java in the subdirectory a/b/c, yoou should put a
package declaration in your MyClass.java:

packacge a.b.c;

The package structure has to match the directory structure
 
A

Andrew Thompson

On Wed, 15 Dec 2004 14:51:50 +0100, Jack Andersson wrote:

Please refrain from top-posting Jack, I find it most confusing.
<http://www.physci.org/codes/javafaq.jsp#netiquette>

See further comments below..

I regularly make applet jars using WinZip. No problems
whatsoever. Applications are a different matter. I suspect
it has something to do with the exact naming of the Manifest
file, but I have never managed to pin it down.

I do not feel any available evidence supports that, it is
purely a technical matter.
I didn't make the jar with winzip, I made it with AntBuild which in turn
uses jar.exe. What I did was to use WinZip to open the jar and then copy the
files to the classes folder. ...

Yes! You mention AntBuild and earlier, IntelliJ.

Bring this problem back to the basics and use jar.exe from the
command line. If the problem is still there it is one of..
- a problem in your code (package statements)
- a problem in your directory structure
- a problem in how jar.exe is invoked
- a bug in jar.exe (very unlikely)

In that case, bring the relevant details back (including the
exact jar command and output) and we will know more.

OTOH, if that works fine, go back to the makers of AntBuilder
or IntelliJ respectively.
 
J

Jack Andersson

I mean that, when you are building a jar file, and the class MyClass is
in the file MyClass.java in the subdirectory a/b/c, yoou should put a
package declaration in your MyClass.java:

packacge a.b.c;

The package structure has to match the directory structure

This I had forgot, but it didn't solve the problem, I'm afraid.
 
S

Sudsy

Jack said:
That is, when it didn't work I copied all the .class-files to my classes
folder. Then it worked.

No need to go to that extreme!
It's simply that the directory structure has to match the package
specification. Let's create a really simple example:

Class A is in package com.mine. Class B is in package com.mine.ext.
They have to reside in a directory structure which matches those
package names. Here's an ASCII line-art drawing:

$HOME
|
+-com
|
+-mine
|
+-A.java
|
+-ext
|
+-B.java

So to the path to the file A.java from the $HOME directory is
com/mine/A.java. Similary, the path to B is com/mine/ext/B.java.
Notice that "com/mine" maps to package com.mine and "com/mine/ext"
maps to package com.mine.ext.
That's all there is to it!
You just have to make sure that you're in the correct directory
(the root of the file structure) when you run jar.
In the previous example, I'd do the following (under Linux):

$ cd $HOME
$ jar cf example.jar com

Here's an example of listing the contents (after also compiling
the source files):

$ jar tf example.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mine/
com/mine/ext/
com/mine/ext/B.class
com/mine/ext/B.java
com/mine/A.class
com/mine/A.java

Also, to run the class (from the $HOME directory again), I'd do
this:

$ java com.mine.A
$ java com.mine.ext.B

See how it all maps nicely?

Bottom line: Ensure that your package declarations match your
directory structure.
 
A

Andrea Desole

Jack said:
This I had forgot, but it didn't solve the problem, I'm afraid.

Strange. The error message you posted, specially the part that says
"Please remove or make sure it appears in the correct subdirectory of
the classpath" makes me think this is the problem. In this case I would
agree with Andrew's posting (I would actually add another option to the
list: a bug in the compiler, which is *very* unlikely): call jar from
the command line, and see what happens. Just to be sure, check that
there is no META-INF directory present.
If it doesn't work, please post the following:

- the jar command line
- information about the directory structure, mainly where you called jar
from and where your class file is
- the first lines of your code, including the class name
 
J

Jack Andersson

Strange. The error message you posted, specially the part that says
"Please remove or make sure it appears in the correct subdirectory of
the classpath" makes me think this is the problem. In this case I would
agree with Andrew's posting (I would actually add another option to the
list: a bug in the compiler, which is *very* unlikely): call jar from
the command line, and see what happens. Just to be sure, check that
there is no META-INF directory present.
If it doesn't work, please post the following:

Actually, after a thorough cleaning and a complete rebuild and then jaring
the package it started working! As I said, I'm using the AntBuild so I
woudn't be able to post the jar command.

Also I got some useful information from this:

http://forum.java.sun.com/thread.jspa?threadID=551821

Thanks!
 
A

Andrew Thompson

...I'm using the AntBuild so I
woudn't be able to post the jar command.

Unless you learn how to do these things from the command line,
your next ten(+) questions will have you blaming this tool or that,
for errors that are your own.

Please look over Sudsy's clear explanation of how it all goes together,
then figure how to translate that to your project and environment.
Consider it an 'all else fails'* option.

* Things -will- fail, take that as a given.
 
J

Jack Andersson

...I'm using the AntBuild so I
Unless you learn how to do these things from the command line,
your next ten(+) questions will have you blaming this tool or that,
for errors that are your own.

I haven't said that the errors aren't mine.

Believe me, I tried using the command line version of jar. What happened?
The jar crashes and spits out 7589342573489 strange characters including the
bell one, which made my computer sound like a fire alarm. That in turn made
my collegues rather upset. I won't use the command line jar again unless I'm
completely alone.
 
T

Tilman Bohn

In message <[email protected]>,
Jack Andersson wrote on Thu, 16 Dec 2004 10:08:49 +0100:

[...]
Believe me, I tried using the command line version of jar. What
happened? The jar crashes and spits out 7589342573489 strange
characters including the bell one, which made my computer sound like a
fire alarm.

Which just means you didn't specify the f option so it created the
archive and wrote it to stdout -- your terminal. It didn't crash at
all. It did exactly what you told it to.
That in turn made my collegues rather upset. I won't use the command
line jar again unless I'm completely alone.

A much better approach would be to try and understand why it printed
to stdout and how to make it write the output somewhere else.

Cheers, Tilman
 
J

Jack Andersson

A much better approach would be to try and understand why it printed
to stdout and how to make it write the output somewhere else.

Or use a nice GUI called AntBuild. And try to understand it.
 
M

Michael Borgwardt

Jack said:
Or use a nice GUI called AntBuild. And try to understand it.

No. You need to understand the tool, not the GUI. The GUI may
be easier to use, but will only hinder understanding.
 
J

Jack Andersson

No. You need to understand the tool, not the GUI. The GUI may
be easier to use, but will only hinder understanding.

Oh sorry, I forgot that we have the year 1975 soon, and of course I must
learn to use the very modern command line tool. I will also stop using
IntelliJ and use notepad as editor.

I rather read the AntBuild manual than use a hopeless cmd window. I will
claim that using cmd windows will hinder understanding and fast development.
 
T

Tilman Bohn

In message <[email protected]>,
Jack Andersson wrote on Fri, 17 Dec 2004 11:05:22 +0100:

[...]
learn to use the very modern command line tool. I will also stop using
IntelliJ and use notepad as editor.

That would be a wise decision for learning the ropes, as it would
force you to think in terms of what's going on, instead of in terms of
auto-completion and something magically and inexplicably turning
red in your IDE.
I rather read the AntBuild manual than use a hopeless cmd window. I will
claim that using cmd windows will hinder understanding and fast
development.

Claim what you will. It's your private problem.
 
J

Jack Andersson

That would be a wise decision for learning the ropes, as it would
force you to think in terms of what's going on, instead of in terms of
auto-completion and something magically and inexplicably turning
red in your IDE.

Auto completion is gods gift to programmers. I can't believe that someone
with serious intentions of developing java code quickly and efficiently
would think differently. It stunning how programmers tends to love stone age
methods and how they tend bo be very reactionary.

Somehow this discussion reminds me of when some jerks back in the 90s
claimed that internet only should contain text and no picture.
Claim what you will. It's your private problem.

Thank you. I happy to announce that my "private problem" has speeded up my
programming with 100% or more.
 

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,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top