• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in main  RSS feed

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,


I have the following program . I like to call the MessageGenerator print and other methods in the TestMessage class.
I am getting an error as : java.lang.NoClassDefFoundError: message/TestMessage.Exception in thread "main" .

Can any one explain me where am I wrong.







 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, your import statement is invalid. You may want to fix that, then make sure you've set up a correct directory structure to reflect your package structure, then recompile and let us know if you need more help. I'd suggest simplifying the problem by eliminating the import and package statements to make the classes part of the default package, unless using packaging is part of the goal of the exercise.
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I changed the package to default, but I am getting an error as cannot access MessageGenerator. at MessageGenerator m = new MessageGenerator();
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two questions:

1. Did you also remove your import statement?

2. Is your classpath set to the folder where your compiled classes are?

It may help if you post the new error message.

Good luck!
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed the import statement.

I am getting the error as :
[[cannot access MessageGenerator
bad class file: .\MessageGenerator.class
class file contains wrong class: message.MessageGenerator
Please remove or make sure it appears in the correct subdirectory of the classpath.
MessageGenerator mg = new MessageGenerator("yellow");]]

This error is displayed in TEXTPAD.

In Eclipse it is shown as : java.lang.NoClassDefFoundError: TestMessage
Exception in thread "main"



 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TestMessage and MessageGenerator are both public classes, so they have to be in their own files, and the file must match the name, case included, of the public class with a .java extension.

If both of these conditions are true, then you most likely have a classpath problem. Try setting the classpath to the directory containing your classes. If you're running in Eclipse's debug perspective, then it should be doing that for you.
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question can i write a method as


As the question implies : Define a method writePhrase that has one String
parameter .The method should write the parameter on System.out
followed by HALF_STOP.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as long as HALF_STOP is visible to the method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!