John B. Matthews wrote:
Novice wrote:
I'm getting an unexpected runtime error when I execute my applet on a
webpage from a jar. I should note that I do NOT get this same error
when I run this same applet in Eclipse, either as an applet or as
application using Roedy Green's "Hybrid/switchhitter" technique.
This is the line that it doesn't like:
switch(FooConstants.DESIRED_FORMAT) {
The relevant line of FooConstants is:
public static final Formats DESIRED_FORMAT =
Formats.PARAGRAPH_FORMAT;
And Format is defined as follows:
public enum Formats {
/** Bulleted list. */
LIST_FORMAT,
/** Paragraph. */
PARAGRAPH_FORMAT
}
Can anyone tell me why I'm getting this error? Also, I'm curious to
know why this error doesn't occur when I run the applet in Eclipse.
As suggested here, you may have re-factored FooConstants or Formats
since the last full build:
<https://groups.google.com/forum/#!msg/comp.lang.java.programmer/NrVbkO
kDBsg/BtW0CQeuhDUJ>
Thanks for the suggestion, John, but it didn't work. I did a clean and
rebuild of the project, then ran my Ant script to compile the code, jar
it, and send the jar to the server but I'm still getting the same error
in the same place when I execute the applet on a web page. The applet
continues to run just fine in Eclipse, both as an applet and an
application.
I did some googling looking for other reports of this error and found a
suggestion to put a try/catch around the problem code so I did that:
try {
switch(FooConstants.DESIRED_FORMAT) {
case PARAGRAPH_FORMAT:
dataPanel.add(createScrollableBorderedTextAreaForParagraph
(this.doc.getDescriptionParagraph(entryNumber),
this.doc.getDescriptionText()));
break;
case LIST_FORMAT:
dataPanel.add(createScrollableBorderedTextAreaForList
(this.doc.getDescriptionSentences(entryNumber),
this.doc.getDescriptionText(), LIST_BULLET_CENTER_DOT));
break;
}
}
catch (Throwable t) {
t.printStackTrace();
}
Then I ran the Ant script again. The behaviour changed fairly
dramatically after this change. When I try to run the applet in Firefox
7, I get:
java.lang.NoClassDefFoundError: Could not initialize class
com.foo.FooConstants
The applet will not display. However, when I do the same thing in Opera
11.52, the applet DOES display, although the console shows the same
error. Ditto for Google Chrome: the applet displays but I get the error
in the console.
FooConstants is definitely in the jar - I have a little jarViewer class
that I use to list the contents - so I'm not sure whether Java just isn't
seeing it or is having some kind of problem initializing it.
Java just isn't seeing it. You have the ".class" file in the wrong subdirectory.
The underlying error is that 'FooConstants' is not in the correct place in the classpath. It is not Java's fault.
Instead of providing us with a partial clue (that never contains the whole answer and sometimes contains none of it), put together a Simple, Self-Contained Compilable Example (SSCCE):
What you will find is that the class 'FooConstants' is not in the directory location in the classpath that corresponds to its package.
Show us the source for 'FooConstants', please (no more snippets!) and in what directory it resides within the JAR ("jar tf jarfile | grep FooConstants" being a convenient way to figure that out).