0

I have two files in the same folder 'temp'. One file compiles fine, but for the other the compiler throws "java:13: error: cannot find symbol" for the name of a class in the first file. I don't know how to resolve this, thanks in advance for any help!

Main file, doesn't compile, can't find 'Converter':

package temp;
import temp.*;

/**
 * A simple class that uses classes in named packages.
 */
public class TempTable {

   /**
    * A program that prints out a temperature conversion table
    * @param args The command-line arguments
    */
   public static void main(String[] args) {
   
      double f = Converter.c2f(0);
      // print out headers
      System.out.println("Celsius  Fahrenheit");
      
      // print out values
      
   }
}

Secondary file, with the class that can't be found:

package temp;

/**
 * A simple supplier class that converts temperature values.
 */
public class Converter {

   /**
    * Converts Celsius to Fahrenheit.
    * @param value The Celsius temperature to be converted
    * @return      The calculated Fahrenheit temperature
    */
   public static double c2f(double value) {
   
      return 0;
      
   }
}
  • There's no package statement on `Converter` – Tim Moore Jan 29 '23 at 00:00
  • @TimMoore - apologies, looks like the code block edited it out. I have it assigned to the same package as the first file for simplicity sake at the moment. – Mark Ainsworth Jan 29 '23 at 00:33
  • Looks OK, then. How are you compiling and running? – Tim Moore Jan 29 '23 at 00:37
  • @TimMoore thanks for the response! I'm using VisualStudio's terminal, my command line is javac temptable.java and javac converter.java – Mark Ainsworth Jan 29 '23 at 00:49
  • 2
    You need to give javac the correct classpath so that it can find Converter. Try `java -cp .. converter.java`. As you're building in the package directory, `temp`, `javac` is looking for `Concverter` in `temp/Converter.class`, relative to the current working directory, which in your case is the `temp` directory which contains the source files. – tgdavies Jan 29 '23 at 00:57
  • 2
    Alternatively, work in the parent directory of `temp`, and use `javac temp/.java`. Better still, build with maven. – tgdavies Jan 29 '23 at 00:59

1 Answers1

0

I solved the problem. in the file Converter.java, compile it with a -d option:

javac -d . Converter.java

This puts Converter.class inside a subfolder 'temp', because of the "package temp;" statement.

Then, delete the "package temp;" statement from TempTable.java; it serves no useful purpose for this example.

The "import temp.*;" statement is OK, keep it.

Then, compile the TempTable.java in the normal fashion:

javac TempTable.java

The TempTable.class file should be in the same folder. You should be able to run it normally:

java TempTable