0

So right now I have two classes, one of which creates an object out of another class:

import java.io.*;


public class PostfixConverter {


    public static void main(String args[]) throws IOException, OperatorException {
        ...

        String postfixLine;

        while ((postfixLine = br.readLine()) != null) {
            // write some gaurd clauses for edge cases
            if (postfixLine.equals("")) {
                ...
                Cpu cpu = new Cpu();

and

public class Cpu {
    Cpu() {
        // this linkedListStack is for processing the postfix
        ...
    }

Currently I'm running javac PostfixConverter.java to compile the class but it cannot find the Cpu symbol. What can I do so that the compiler can discover the missing symbol? Shouldn't everything by default be packaged in the default package and therefore find each other?

Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • surprisingly exact same question was asked at https://stackoverflow.com/questions/30277632/a-java-class-cant-find-an-other-in-the-same-package?rq=1 with no answer yet on why it did not find with pure javac command. My suspect is your environment, path variables and how javac actually gets evaluated. Which java version you have and what is your global classpath variable value? – Jags Mar 02 '20 at 02:19
  • My stack overflow rep is too low to comment so I'm posting here. With regards to the comment wondering why 5 classes were compiled instead of only the two that were specified on the command line, the reason might be because one file that was specified on the command line reference another java class in the same folder, the Java compiler will automatically compile that class for you. –  Mar 02 '20 at 01:58
  • AFAIK, javac doesn't automatically compile the referenced classed unless you used any IDEs like eclipse which will build automatically. One way to make sure is to run the command and check the timestamp of the .class files. You can use "*.java" in your javac command to compile all classes in a folder. – Santosh Mar 02 '20 at 04:36

2 Answers2

0

You should compile both files, so that the Cpu class file will be available to PostfixConverter:

javac Cpu.java PostfixConverter.java

Keep in mind that in general it is not desirable to build an application where everything sits in the default package. Consider creating an appropriate package here. Also, you may want to use either an IDE and/or Maven, which would make the build process easier for you.

As the other answer mentions, Java will also automatically build all Java source files in the current directory, which might explain why other source files are also getting built.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

javac PostfixConverter.java

This command should work if both files are located on the current directory, as the default classpath (-cp option) is the current directory (.).

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417