Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: java.lang.NoClassDefFoundError exception

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java.lang.NoClassDefFoundError exception

    Hello. I'm having some issues with my code. When I try to execute it, it gives this exception:

    Exception in thread "main" java.lang.NoClassDefFoundError: Employee
    Caused by: java.lang.ClassNotFoundException: Employee
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)

    Because there is no line number, I cannot determine which line of code is giving the error. Is it because of something wrong in my code or my java environment? If so, how would I fix it. Thanks, my code is pasted below.
    import java.util.Scanner;
     
     
     
    public class Employee {
    	//Varibales
    	private static double salary, rate, hours, weeklyPay = 0;
    	private static String name;
    	private static String type;
    	static boolean bonusPay = false;
    	//Constructor creates Employee object
    	public Employee(String name, String status, double hours, double rate, double salary, boolean bonusPay) {
    		if (status == "salaried") {
    			this.salary = salary;
    			if(bonusPay == true) {
    				salary = salary * 1.1;
    			}
    		} else {
    			this.rate = rate;
    			this.hours = hours;
    			if(hours > 40) { //calculates and adds overtime pay
    				weeklyPay = (hours - 40) * (2*(salary)) + 40 * salary;
    			} else {
    				weeklyPay = rate * hours;
    			}
     
    		}
    	}
    	//Methods to obtain information about a specific employee
    	private double getWeeklyPay() {
    		return weeklyPay;
    	}private String getName() {
    		return name;
    	}private double getSalary() {
    		return salary;
    	}private String getType() {
    		return type;
    	}private double getHours() {
    		return hours;
    	}private double getRate() {
    		return rate;
    	}
     
    	private void printData(Employee x) {
     
    	}
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	System.out.println("Hello! Welcome to Employee Database");
    	System.out.println("If Employee is salaried enter S. Otherwise enter H for hourly");
    	if(input.next().toLowerCase().contains("s")) {
    		type = "salaried";
    	} else {
    		type = "hourly";
    	}
    	System.out.println("Enter Employee's full name");
    	name = input.next();
    	if(type == "salary") {
    		System.out.println("Enter weekly salary");
    		salary = input.nextDouble();
    		System.out.println("Enter Y if employee receives a bonus of 10%, otherwise enter N");
    		if(input.next().toLowerCase().contains("y")) {
    			bonusPay = true;
    		} else {
    			bonusPay = false;
    		}
    	} else {
    		System.out.println("Enter hourly pay");
    		rate = input.nextDouble();
    		System.out.println("Enter # of hours");
    		hours = input.nextDouble();
     
    	}
    	Employee Employee1 = new Employee(name, type, rate, hours, salary, bonusPay);
     
    	 System.out.println();
       	 System.out.println("\t  Name          Status              Hours             Rate             Weekly Pay Amount");
       	 System.out.println("\t======================================================================");
       	 System.out.println(Employee1.getName() +              Employee1.getType() +          Employee1.getHours()             + Employee1.getRate()         + Employee1.getWeeklyPay());
       	 System.out.println();
        }
     
    }


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: java.lang.NoClassDefFoundError exception

    :/ runs for me. Make sure you name the file correctly? Cap sensitive.
    paste from my console
    C:\sandbox>javac Employee.java
     
    C:\sandbox>java Employee
    Hello! Welcome to Employee Database
    If Employee is salaried enter S. Otherwise enter H for hourly
    S
    Enter Employee's full name
    Jeff
    Enter hourly pay
    20
    Enter # of hours
    40
     
              Name          Status              Hours             Rate             W
    eekly Pay Amount
            ======================================================================
    Jeffsalaried40.020.00.0
     
     
    C:\sandbox>
    Last edited by JJeng; July 23rd, 2012 at 01:25 PM. Reason: Fixing engrish

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NoClassDefFoundError exception

    Still have problem. USing Eclipse IDE

  4. #4
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb Re: java.lang.NoClassDefFoundError exception

    I wish I could say there is nothing wrong with your code, the formatting was horrendous .... anyways try copying your code into notepad then paste it back into your IDE (eclipse). You might have a blank space character that is throwing off the compiler. Or Simply copy and paste the formatted version of your code... I'm assuming you aren't finished with the code anyways.

    import java.util.Scanner;
     
    public class Employee {
    	//Varibales
    	private static double salary, rate, hours, weeklyPay = 0;
    	private static String name;
    	private static String type;
    	static boolean bonusPay = false;
     
    	//Constructor creates Employee object
    	public Employee(String name, String status, double hours, double rate, double salary, boolean bonusPay) 
    	{
    		if (status == "salaried") 
    		{
    			this.salary = salary;
     
    			if(bonusPay == true) 
    			{
    				salary = salary * 1.1;
    			}
    		} 
    		else 
    		{
    			this.rate = rate;
    			this.hours = hours;
    			if(hours > 40) 
    			{ 
    				//calculates and adds overtime pay
    				weeklyPay = (hours - 40) * (2*(salary)) + 40 * salary;
    			} 
    			else 
    			{
    				weeklyPay = rate * hours;
    			}
     
    		}
    	}
    	//Methods to obtain information about a specific employee
    	private double getWeeklyPay() 
    	{
    		return weeklyPay;
    	}
    	private String getName()
    	{
    		return name;
    	}
    	private double getSalary() 
    	{
    		return salary;
    	}
    	private String getType() 
    	{
    		return type;
    	}
    	private double getHours() 
    	{
    		return hours;
    	}
    	private double getRate() 
    	{
    		return rate;
    	}
     
    	private void printData(Employee x) 
    	{
     
    	}
     
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	System.out.println("Hello! Welcome to Employee Database");
    	System.out.println("If Employee is salaried enter S. Otherwise enter H for hourly");
    	if(input.next().toLowerCase().contains("s")) 
    	{
    		type = "salaried";
    	} 
    	else 
    	{
    		type = "hourly";
    	}
    	System.out.println("Enter Employee's full name");
    	name = input.next();
     
    	if(type == "salary") 
    	{
    		System.out.println("Enter weekly salary");
    		salary = input.nextDouble();
    		System.out.println("Enter Y if employee receives a bonus of 10%, otherwise enter N");
     
    		if(input.next().toLowerCase().contains("y")) 
    		{
    			bonusPay = true;
    		}
    		else 
    		{
    			bonusPay = false;
    		}
    	} 
    	else 
    	{
    		System.out.println("Enter hourly pay");
    		rate = input.nextDouble();
    		System.out.println("Enter # of hours");
    		hours = input.nextDouble();
     
    	}
     
    	Employee Employee1 = new Employee(name, type, rate, hours, salary, bonusPay);
     
    	 System.out.println();
       	 System.out.println("\t  Name          Status              Hours             Rate             Weekly Pay Amount");
       	 System.out.println("\t======================================================================");
       	 System.out.println(Employee1.getName() +              Employee1.getType() +          Employee1.getHours()             + Employee1.getRate()         + Employee1.getWeeklyPay());
       	 System.out.println();
        }
     
    }
    f34r th3 kut3 1z

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: java.lang.NoClassDefFoundError exception

    Not directly related to your problem, as it sounds like an IDE issue that is oftentimes hard to remotely diagnose, but never use == to compare the content of a String...use the equals() method.

Similar Threads

  1. [SOLVED] Exception in thread "main" java.lang.NoClassDefFoundError
    By cppcppcpp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2012, 11:59 AM
  2. java.lang.NoClassDefFoundError help?
    By softballfrk13608 in forum Threads
    Replies: 2
    Last Post: March 6th, 2012, 03:22 PM
  3. Exception in thread "main" java.lang.NoClassDefFoundError
    By Scarice in forum Java Theory & Questions
    Replies: 25
    Last Post: July 4th, 2011, 10:02 PM
  4. Replies: 13
    Last Post: October 13th, 2010, 11:20 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM