my dog learned polymorphism*
The moose likes Beginning Java and the fly likes NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NoClassDefFoundError" Watch "NoClassDefFoundError" New topic
Author

NoClassDefFoundError

Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
I have been working on trying to get a project to run for 2 days but i receive the following error.Let me backup a lil. This is what my code looks like. I had no errors until my assignment called for me to Move implemenation out of main(String[ ] args). Any help or suggestions will be much appreciated.



package payroll;

import java.util.Scanner;


public class Payroll {



{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );




// prompt for and input employee name
System.out.println( "Please enter the employee name:" );
String employeeName = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line

// prompt for and input employee hourly wage
System.out.println( "Please enter the employee hourly wage:" );
double hourlyWage = input.nextDouble(); // read a line of text
System.out.println(); // outputs a blank line

// prompt for and input employee hours worked
System.out.println( "Please enter the employee hours worked:" );
double hoursWorked = input.nextDouble(); // read a line of text
System.out.println(); // outputs a blank line


System.out.println(); // outputs a blank line

System.out.println("Employee Name: " + employeeName);
System.out.println("Hourly Wage: " + hourlyWage);
System.out.println("Hours Worked: " + hourlyWage);

// main method begins execution of Java Application
}
public static void main(String args[]) {


} // end method main


} // end class Payroll

Error

C:\jdk1.5.0_14\bin>java Payroll
Exception in thread "main" java.lang.NoClassDefFoundError: Payroll (wrong name:
payroll/Payroll)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:1
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Aretha,

Hi. First thing, you should try and wrap your code in the code tags. It makes it a little bit easier to read.

As for your issue. The reason you are getting this error message is because you are invoking Java from the jdk bin directory and the application does not know where to find your class Payroll. There are several ways to tell the JVM where to find your classes, and this article sums it up nicely: http://www.javaservice.net/~java/docs/jdk1.1.8/tooldocs/win32/classpath.html. But, the easiest way to start out would be to invoke java from wherever your class files are located and specify a dot (.) classpath.

For example, assume your class file is in C:\mydir, you would invoke the following:


This is saying to look for your Payroll.class file in the current directory. Hope this helps!

Jeff


Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
I guess im a lil confused because i have the class files in the same directory which is the bin folder. Is this a bad idea?
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Typically you'd like to keep your application files separate from the JDK directories for a number of reasons. A couple would be for packaging your applications or if you want to change JDKs. Also, in real deployment environments, you run into the issue where the JDK bin directory is probably not writeable, so it would be impossible to install there.

But, my guess is your classpath does not include the bin directory (if it is explicitly set as an environment variable and doesn't include that bin directory, I don't think the JDK will find those classes). Assuming your class files are still in the bin directory, try
from the bin directory. It should work just fine.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Exception in thread "main" java.lang.NoClassDefFoundError: Payroll (wrong name: payroll/Payroll)

Look at the error message: the "wrong name" part indicates what's wrong. The fully-qualified name of your class is payroll.Payroll, because it's in a package named 'payroll'.

First of all, your directory structure must match the package structure. So your Payroll.class file must be in a directory named 'payroll'. Then, you must run the application from the base directory of the package, or you must put the base directory in the classpath, and then you run the program by specifying the fully-qualified name of your class:

C:\Project\payroll> cd ..
C:\Project> java payroll.Payroll

For more info on working with packages, see The Java Tutorial: Packages.
[ June 10, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
Thank you all for the much needed help. I read more on how packages from both sites that was listed by you guys.So now my error is gone Yay but my program fails to run even though it gives no errors.Any suggestions on why this might be happening?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Aretha Clark:
...my program fails to run even though it gives no errors.Any suggestions on why this might be happening?

You have nothing in your main method, so when it runs, it doesn't do anything (that you can see).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
my assignment called for me to Move implemenation out of main(String[ ] args). I thought this was what i was doing but it isnt i guess. Do you know where i might could look for information or ideas on how to accomplish this and my code still runs.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Aretha,

In your main method, you're not actually calling your code ... think about trying to create a new payroll object. Since all of your behavior is in the constructor, you need to construct that object for that code to be called.

Jeff
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Aretha Clark:
my assignment called for me to Move implemenation out of main...

But do you understand why you were told to move the code out of main?

Ideally, your Payroll class should be a "blueprint" for a type of object. But if all your code is in main, then you are never creating any objects -- you are just running a procedure in a static method.

So what you really want to do is create a new Payroll object. The idea is that you then have an instance of Payroll that you can use. You can call methods on it and have it "do" things.

But when you moved your code out of main, you did something that's kind of defeating the whole purpose. You may not realize it, but you enclosed your code inside an initializer block...

Initializer blocks will run when an instance is created. So as soon as you create a new instance of Payroll, this code will run. That might seem like what you want, but you're still not treating your object like an object. You can't really "communicate" with it.

You communicate with an object by calling its methods. So instead of one big initializer block, you probably want to divide this code up into a few separate methods. Maybe things like promptForInput() and displayOutput().

You still need some code in main to make it go. This is where you create a new instance of Payroll, and probably call a method on it to get it going. Maybe something like...

Payroll myPayroll = new Payroll();
myPayroll.promptForInput();
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might want to look at The Java Tutorial: Object-Oriented Programming Concepts for background. After reading the first page on objects, notice the example of the Bicycle class, and the way it defines state (fields) and behavior (methods). Below that, you will see how a main method is used to create Bicycle objects and call methods on them.
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
Thanks for the help i will give it another shot ad also take a look at the tutorial also
 
wood burning stoves
 
subject: NoClassDefFoundError
 
Similar Threads
illegal start of expression
Nested while loop inside do...while loop?
Code problems
Code Help
condition to leave all your loops