Error: "Can not find symbol" - Class Instance within the Main Class, using Linux terminal

1

I have an exercise to solve, which is to create a simple Java language calculator.

I have to through the Terminal, send the two numbers as arguments to my program. I am using the following build command:

 zeluis@zeluis-HP-EliteBook-8460p ~/NetBeansProjects/SOCP1/src/socp1 $ javac MainEX1.java

It just gives me the following error:

    EX1.java
MainEX1.java:62: error: cannot find symbol
        CalculatorMethodos cM = new CalculatorMethodos();
        ^
  symbol:   class CalculatorMethodos
  location: class MainEX1
MainEX1.java:62: error: cannot find symbol
        CalculatorMethodos cM = new CalculatorMethodos();
                                    ^
  symbol:   class CalculatorMethodos
  location: class MainEX1
2 errors

I leave the MAIN class below:

 int num1, num2, total = 0, opcCalc, vef = -1;
    CalculatorMethodos cM = new CalculatorMethodos();
  //read from keyboard
    Scanner lerDataKeyBoard = new Scanner(System.in);
    // BufferedReader lerDataKeyBoard = new BufferedReader(new InputStreamReader(System.in));
/*    System.out.println("First number:\n");
    num1 = lerDataKeyBoard.nextInt();
    System.out.println("Second number:\n");
    num2 = lerDataKeyBoard.nextInt(); */


    System.out.println("Introduza a operação:\n");
    System.out.println("'1' - SUM\n");
    System.out.println("'2' - SUBTRACT\n");
    System.out.println("'3' - MULTIPLY\n");
    System.out.println("'4' - DIVIDE\n");

    opcCalc = lerDataKeyBoard.nextInt();

    switch (opcCalc) {
        case 1:
           total = cM.add(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
            break;
        case 2:
          // total = cM.sub(args[0], args[1]);
            break;
        case 3:
         //   total = cM.sub(args[0], args[1]);
            break;
        case 4:
           // total = cM.div(args[0], args[1]);
            break;
    }
    System.out.println("Resultado:" + total);

}

CLASS CALCULATOR:

    public class CalculatorMethodos {

    private int total;

    public int add (int num1, int num2) {
        return total = num1 + num2;

    }
     public int sub(int num1, int num2) {
        return total = num1 - num2;

    }
      public int mult (int num1, int num2) {
        return total = num1 * num2;

    }
       public int div (int num1, int num2) {
        return total = num1 / num2;

    }
}

SOLUTION The solution to this error was to use the javac command with -cp and "..":

javac -cp .. MainEX1.java

NEW ERROR - when I try to use the command java MainEX1

Error: Could not find or load main class MainEX1
    
asked by anonymous 17.11.2017 / 23:34

1 answer

2

This is a classpath problem. If all classes are in the same package, compile with this command: javac -cp .. MainEX1.java

You need to tell the compiler where to find the classes needed to perform the compilation. The .. will pass the parent directory of the directory you are running the command, in the case of: zeluis@zeluis-HP-EliteBook-8460p ~/NetBeansProjects/SOCP1/src

From this directory you can find the socp1.MainEX1 and socp1.CalculatorMethodos

    
18.11.2017 / 03:17