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.
Code :
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();
}
}
Re: java.lang.NoClassDefFoundError exception
:/ runs for me. Make sure you name the file correctly? Cap sensitive.
paste from my console
Code :
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>
Re: java.lang.NoClassDefFoundError exception
Still have problem. USing Eclipse IDE
Re: java.lang.NoClassDefFoundError exception
I wish I could say there is nothing wrong with your code, the formatting was horrendous :-s .... 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.
Code java:
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();
}
}
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.