Why does it return the following error?
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main Command execution failed.
Main:
package Calculando2;
public class Main {
public static void main(String[] args) {
int n1 = 10;
int n2 = 2;
Operacion op1 = new Operacion();
op1.sumar(n1,n2);
op1.restar(n1,n2);
op1.multiplicar(n1,n2);
op1.dividir(n1,n2);
op1.mostrar();
}
}
Operation:
package Calculando2;
public class Operacion {
//Atributos
int suma;
int resta;
int multiplicacion;
int division;
//Métodos
public void sumar(int numero1, int numero2){
suma = numero1 + numero2;
}
public void restar(int numero1, int numero2){
resta = numero1 - numero2;
}
public void multiplicar(int numero1, int numero2){
multiplicacion = numero1 * numero2;
}
public void dividir(int numero1, int numero2){
division = numero1 / numero2;
}
public void mostrar(){
System.out.println("suma = " + suma);
System.out.println("resta = " + resta);
System.out.println("multiplicacion = " + multiplicacion);
System.out.println("division = " + division);
}
}
Hi, your code is fine. What program do you use to program I have taken your code to netbeans and everything works here you have the image.
Possible solutions: https://www.campusmvp.es/recursos/post/como-solucionar-el-error-java-lang-noclassdeffounderror-en-java.aspx
Summary :
1.- The package is not available in the Classpath If you have doubts about where the Classpath is located, you can show the path on the screen using:
System.getproperty("java.classpath")
2.- Sufficient permissions are missing Some permission problem may be preventing the classes from a .jar file from being loaded and the NoClassDefFoundError exception is produced.
You must ensure that the current user has sufficient permissions to access the files. This is especially important in the event that a package is shared and therefore may have permissions established by another user, without having created permissions that ensure access by all users.
3.- Some dependency on third parties is missing
Some program dependency, such as a native library, is not available on the current machine, but was available on the machine it was compiled on.
Determine which one it is by looking at the error log and install it on the current machine.
4.- Lack of visibility between levels of the ClassLoader hierarchy
In J2EE, as we have explained before, the lack of visibility of the class between the different levels of the hierarchy in the loading process can cause this situation.
5.- An initialization script overrides ClassPath
A less common, but still possible possibility is when a program's initialization script is overwriting the Classpath environment variable, causing the rest of the program to be unable to find the appropriate .jar files.
Try what was mentioned in point 1 to see if the correct route is being used.
6.- Errors in a static initialization block
If the program uses a static initialization block (for example, it is typical with classes of type Singleton, which do not allow more than one instance), it is possible that some reference that is used from there will not be found.
7.- Misconfigured or incorrectly installed JDK
If any of the ClassPath, JAVA_HOME, or PATH variables are set wrong due to some Java installation problem, we're going to get NoClassDefFoundError errors, of course. In this case, the Java runtime would have to be reinstalled to fix it.
I have not found any errors. Make sure your two classes are inside a folder called
Calculando2
. Since when you use a package the classes must be found in a folder that has the same name as the package.Hello Another thing that can be done if you use Eclipse and Maven: Right Click on the project: Run AS > Maven Install
and then
Right Click on the project: Run AS > Java Application