• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Error  RSS feed

 
Anthony Stillwell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to get homework completed. Would appreciate any help with understanding the problem.

1. Without changing anything in this package, create a java program that imports the package and extends the PsalmC23V1 class to print out the contents of the protected verse variable to the screen.

encouragement is a file the has PsalmC23V1 in it and is provided.




I created a new file called MoreEncouragement




The code compiles fine but when I type java MoreEncouragement I get error stating Exception in thread "main" java.lang.NoClassDefFoundError: [ClassName]

I have tried different things but have not been able to overcome the issue.
 
Nam Ha Minh
Ranch Hand
Posts: 515
Eclipse IDE Firefox Browser Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...when I type java MoreEncouragement I get error ...


The class is declared inside a package, so you need to type the package name before the class name, for example:



And remember your current directory should be the parent directory of the encouragements directory.
 
Anthony Stillwell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the input. I am still having trouble. Even when I do your recommendation error "could not find or load main class. It must be how I have it structured.

My code in my directory -



CODE GIVEN STORED IN my directory in a FILE called encouragements with PsalmsC23V1 java and class file in it

package encouragements;

public class PsalmC23V1{
protected String verse = "The Lord is my Shepherd, I shall not want.";
}
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Anthony Stillwell wrote: . . . package MoreEncouragement; // This might be a problem, I have package called MoreEncouragement and class also called More Encouragement?
Not to the compiler, but you can get yourself into no end of confusion with two things named the same. There are good reasons for the Java naming conventions: classes start with UpperCaseLetters and packages are lowercasethroughout. We also have conventions: please avoid ALL UPPERCASE, and use the code button; since you are new I have added code tags and you can see how much better your post looks
Anthony Stillwell wrote:. . . protected String verse = "The Lord is my Shepherd, I shall not want.";
Shame you can’t change that; protected fields are often suggestive of dubious design.

You need to check the directory structure; it should look something like thisYou need a class with a main method somewhere; you have to get the JVM to invoke that method to start anything off. I know HelloWorld applications are not really useful, but they give an example of the main method: look here. There are several links about compiling in this posting.
 
Anthony Stillwell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help. I figured it out, I was making this way two hard. I did not need a seperate package. The MoreEncouragement Class need to be in my main directory

import encouragements.PsalmC23V1;
class MoreEncouragement extends PsalmC23V1 {
public static void main(String[] args){
MoreEncouragement x = new MoreEncouragement();
System.out.println(x.verse);
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!