Error: "Cannot find symbol" - class instance inside Main class, using Linux terminal
I have an exercise to solve, which consists of creating a simple Java language calculator.
I have to through the Terminal, send the two numbers as arguments to my program. I'm using the following build command:
zeluis@zeluis-HP-EliteBook-8460p ~/NetBeansProjects/SOCP1/src/socp1 $ javac MainEX1.java
Only it 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 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 e"..":
javac -cp .. MainEX1.java
New error - when I try to use command java MainEX1
Error: Could not find or load main class MainEX1
1 answers
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 necessary classes to perform the compilation. The ..
will pass the parent directory of the directory you are running the command, in case the: zeluis@zeluis-HP-EliteBook-8460p ~/NetBeansProjects/SOCP1/src
From this directory you can find the classes socp1.MainEX1
and socp1.CalculatorMethodos