Mashu
Mashu

Reputation: 71

Java "Exception in Thread "main" java.lang.NoClassDefFoundError"

I've run into this error before, but it's confusing to me as it's obviously for different reasons now, as I'm doing what I did last time to avoid it. I'm really stuck on this and not at all sure what to do. This is simply a tester class for the main class that I have, but this is where the error takes place.

  // test class for "CelciusAndFahrenheit"

import java.io.*;

class CelCiusAndFahrenhetTester
{
    public static void main(String[] args) throws IOException
    {

    //Making Input and reading to variable 
    String inInput;
    InputStreamReader inStream = new InputStreamReader (System.in);
    BufferedReader reader = new BufferedReader (inStream);
    System.out.println ("Please state which temperature type you are converting to");
    inInput = reader.readLine();

    if (inInput != "Celcius")
        {
            System.out.println ("What is the temperature amount you wish to convert?");
            inInput = reader.readLine();
            CelciusAndFahrenheit temperatureF = new CelciusAndFahrenheit();
            double answer = Double.parseDouble(inInput);
            temperatureF.setFahrenheit(answer);
            temperatureF.tFah();
        } 
    else
        {
            System.out.println ("What is the temperature amount you wish to convert?");
            inInput = reader.readLine();
            CelciusAndFahrenheit temperatureC = new CelciusAndFahrenheit();
            double answer = Double.parseDouble(inInput);
            temperatureC.setCelcius(answer);
            temperatureC.tCel();
        }
    }
}

EDIT; Won't lie. I'm a noob at this. The guy teaching me Java is making his class use Notepad and CMD to run our programs and frankly I'm just lost. In an attempt to answer some of these question, here's the other program.

import java.io.*;

// blueprint for "CelciusAndFahrenheit" class
class CelciusAndFahrenheit
{
    // declare instance variable as private
    private double fahrenheit;
    private double celcius;

    // declare getter method public
    public double getFahrenheit()
    {
        return fahrenheit;
    }

    // declare setter method public
    public void setFahrenheit(double tempF)
    {
        fahrenheit = tempF;
    tempF = (9.0 / 5.0) * celcius + 32;
    }

    // declare getter method public
    public double getCelcius()
    {
        return celcius;
    }

    // declare setter method public
    public void setCelcius(double tempC)
    {
        celcius = tempC;
    tempC = (5.0 / 9.0) * (fahrenheit - 32);
    }

    public void tFah()
    {
     System.out.println ("The temperature you've inputted in Fahrenheit is " + fahrenheit + ".");
    }

    public void tCel()
    {
     System.out.println ("The temperature you've inputted in Celcius is "+ celcius + ".");
    }
}

Upvotes: 0

Views: 1034

Answers (2)

chubbsondubs
chubbsondubs

Reputation: 38842

Since you are learning I'll see if I can help you out. We can't really help you a lot since we don't have the full error. But, a NoClassDefFoundError means something very specific about what is happening. But we'd need to know what specific class it can't find. NoClassDefFoundError will tell you the class it can't find.

When you compile a Java source file (ie files ending in .java) the java compiler (ie javac) creates the compiled version as a class file (ie files ending in .class). The JVM (ie java) executes code in your .class files. When you compile a Java file you need either source code (ie .java file) or a (.class file) in order for the compiler to resolve all of the dependencies.

For example in your program CelciusAndFarenhetTester class depends upon the CelciusAndFahrenheit class because CelciusAndFahrenheit is used in the source of CelciusAndFarenhetTester. When javac compiles CelciusAndFarenhetTester it needs the class files for CelciusAndFahrenheit or else it will complain. In your case javac isn't complaining which means it can find both CelciusAndFarenhetTester and CelciusAndFahrenheit. It also means that javac should've produced two class files. One for CelciusAndFahrenheit and one for CelciusAndFarenhetTester. Typically those are written out to a different directory than your source files. To control where those files are written you set the build path on javac. For example:

javac -d bin src\*.java

That would compile every java source file under the src directory and write the class files to bin directory. If you looked inside bin directory you'll see .class files.

When you get a NoClassDefFoundError it means that when it compiled that class it found all of the dependencies. However, when you went to run it java couldn't find one or more of the class files needed. Java looks for class files on the classpath. If your program can't find a class it's because it's not on the classpath. So if we wanted to run the class files I created above I'd do the following:

java -cp bin CelciusAndFarenhetTester

The -cp option sets the classpath. You can specify any number of directories seperating them with whatever your platform uses (; windows, : */unix platforms). You only need the top level directory that contains the packages or class files.

Upvotes: 0

user4413257
user4413257

Reputation:

Try to clean and build project again.

Upvotes: 0

Related Questions