Fixing a NoClassDefFoundError

Posted by Chris Okyen on Programmers See other posts from Programmers or by Chris Okyen
Published on 2012-09-24T20:38:02Z Indexed on 2012/09/24 21:48 UTC
Read the original article Hit count: 245

Filed under:
|
|

I have some code:

package ftc;
import java.util.Scanner;


public class Fer_To_Cel {

public static void main(String[] argv)
{


    // Scanner object to get the temp in degrees Farenheit 
    Scanner keyboard = new Scanner(System.in);
    boolean isInt = true; // temporarily put as true in case the user enters a valid int the first time

    int degreesF = 0; // initialy set to 0

    do
    {
        try 
        {
            // Input the temperature text.
            System.out.print("\nPlease enter a temperature (integer number, no fractional part) in degrees Farenheit: ");
            degreesF = Integer.parseInt(keyboard.next()); // Get user input and Assign the far. temperature variable, which is casted from String to int.
        }

        // Let the user know in a user friendly notice that the value entered wasnt an int ( give int value range ) , and then give error log
        catch(java.lang.Exception e)
        {
            System.out.println("Sorry but you entered a non-int value ( needs to be between ( including ) -2,147,483,648 and 2,147,483,647 )..  \n");
            e.printStackTrace();
            isInt = false;
        }
    }

    while(!isInt);

    System.out.println(""); // print a new line.

    final int degreesC = (5*(degreesF-32)/9); // convert the degrees from F to C and store the resulting expression in degreesC

    // Print out a newline, then print what X degrees F is in Celcius.
    System.out.println("\n" + degreesF + " degrees Farenheit is " + degreesC + " degrees Celcius");
}
}

And The following error:

C:\Program Files\Java\jdk1.7.0_06\bin>java Fer_To_Cel
Exception in thread "main" java.lang.NoClassDefFoundError: Fer_To_Cel (wrong name: ftc/Fer_To_Cel)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14

    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

The code compiled without compile errors, but presented errors during execution.

Which leads me to two questions.

  1. I know Errors can be termed Compiler, Runtime and Logic Errors, but the NoClassDefFoundError inherits java.lang.LinkageError. Does that make it a Linker error, being niether of the three types of errors I listed,
  2. If I am right this is the answer. For someone else who obtains the singular .java file and compiles it, would this be the only way to solve this problem? Or can I (should I ) do/have done something to fix this problem? Basically, based on a basis of programming, is this a fault of me as the writer? Could this be done once on, my half and be distributed and not needed be done again?

© Programmers or respective owner

Related posts about java

Related posts about errors