user1927242
user1927242

Reputation: 27

Trouble compiling CMD Java program getting error

New to java and compiling through CMD. My class DataAnalyzer.java is compiling fine but the class containing the main program DataAnalyzerTester.java is giving me an error.

DataAnalyzer:

package h1.q3;
import java.util.*;
public class DataAnalyzer 
{    
    public DataAnalyzer(LinkedList<Integer> numList) 
    {
        for (int num : numList)
        {
            numbers.add(num);
        }
    }
    public int min()
    {
        smallest = numbers.getFirst();
        for(int num : numbers)
        {
            if(num < smallest) smallest = num;
        }
        return smallest;
    }

    public int max()
    {
        largest = numbers.getFirst();
        for(int num : numbers)
        {
            if(num < largest) largest = num;
        }
        return largest;
    }

    public int average()
    {
        sum = 0;
        listSize = 0;
        for(int num : numbers)
        {
            sum += num;
            listSize++;
        }
        return sum/listSize;
    }



    int largest, smallest, sum, listSize;
    LinkedList<Integer> numbers = new LinkedList<Integer>(); 
}

TESTER:

package h1.q3; import java.util.*; /**  *  * @author pini  */
public class DataAnalyzerTester  {

   public static void main(String[] args)
   {
       LinkedList<Integer> numbers = new LinkedList();
       Scanner sc = new Scanner(System.in);
       while(sc.hasNext())
       {
           numbers.add(sc.nextInt());
       }

       DataAnalyzer da = new DataAnalyzer(numbers); 

       System.out.println("Minimum: " + da.min());
       System.out.println("Maximum: " + da.max());
       System.out.println("Average: " + da.average());        
   }

The error I get is as follows:

C:\Users\pini\Documents\NetBeansProjects\Homework1\src\homework1\q3>javac DataAn
alyzer.java

C:\Users\pini\Documents\NetBeansProjects\Homework1\src\homework1\q3>javac DataAn
alyzerTester.java
DataAnalyzerTester.java:24: error: cannot find symbol
        DataAnalyzer da = new DataAnalyzer(numbers);
        ^
  symbol:   class DataAnalyzer
  location: class DataAnalyzerTester
DataAnalyzerTester.java:24: error: cannot find symbol
        DataAnalyzer da = new DataAnalyzer(numbers);
                              ^
  symbol:   class DataAnalyzer
  location: class DataAnalyzerTester
Note: DataAnalyzerTester.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

I have tried everything and still can't get it to compile the TESTER. I am pretty sure my code is good but it doesn't seem to be recognizing the DataAnalyzer object.

Thanks

EDIT

After compiling both and running tester I get the following errors:

    C:\Users\pini\Documents\NetBeansProjects\Homework1\src\homework1\q3>java DataAna
lyzerTester
Exception in thread "main" java.lang.NoClassDefFoundError: DataAnalyzerTester (w
rong name: homework1/q3/DataAnalyzerTester)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
        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:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

Upvotes: 0

Views: 111

Answers (2)

dsh
dsh

Reputation: 12214

You need to include DataAnalyzer on the CLASSPATH. Usually it is easiest to compile all source files at once. (This is Java, not C or C++ :) )

If they are all in the same directory:

javac *.java


C:\Users\pini\Documents\NetBeansProjects\Homework1\src\homework1\q3>java DataAnalyzerTester
Exception in thread "main" java.lang.NoClassDefFoundError: DataAnalyzerTester (wrong name: homework1/q3/DataAnalyzerTester)

To run the program, from where you were there:

cd ..\..
java homework1.q3.DataAnalyzerTester

The java command takes a fully-qualified class name of the "main" class. Since you declared your class to have a package of homework1.q3, the fully-qualified name is homework1.q3.DataAnalyzerTester. When the .class files are in a directory tree (as opposed to a jar file), the packages correspond to directories. Also, the top-level directory that contains the packages must be in the CLASSPATH. The CLASSPATH is critically important for Java. The CLASSPATH used by javac when you compile and the CLASSPATH used by java when you run; and note that each may be different (though usually that is a mistake that leads to errors).

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

Java expects the source files to be in a directory called h1\q3 relative to where youre compiling

Move the files to a directory named

C:\Users\pini\Documents\NetBeansProjects\Homework1\src\homework1\h1\q3

and compile the code

javac h1\q3\*.java

from the homework1 directory.

Upvotes: 1

Related Questions