Error: "Cannot find Symbol" - Class Instance within Main Class, using Linux terminal

Asked

Viewed 729 times

1

I have an exercise to solve, which consists of creating a simple Java language calculator.

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

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

Only it gives me the following mistake:

    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 down the MAIN class:

 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);

}

CALCULATOR CLASS:

    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 MISTAKE - when I try to use the command java MainEX1

Error: Could not find or load main class MainEX1
  • And the class code Calculatormethodos ?? Has not ?

  • @Wéllingthonm..

  • @Wéllingthonm.Souza can check

  • You imported him into the class Main ?

  • he’s inside the same package :)

  • You copied the class code Calculatormethodos and glued here ? Because if that was still close }

Show 1 more comment

1 answer

2


This is 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 compile. The .. will pass the directory Parent of the directory you are running the command, in case the: zeluis@zeluis-HP-EliteBook-8460p ~/NetBeansProjects/SOCP1/src

From this directory it will be possible to find the classes socp1.MainEX1 and socp1.CalculatorMethodos

  • worked, but I still have error, when I try to run the program. I will put in the original post as editing

  • 1

    @bullprog It’s the same problem. Only in this case, in addition to putting the directory in the classpath, you need to call the class by its full name. The command is: java -cp .. socp1.MainEx1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.