Error: Could not find or load main class Integers Caused by: java.lang.Noclassdeffounderror: javaLoiane/aula11/Integers (Wrong name: Integers)

Asked

Viewed 1,257 times

0

When I compile with javac, it works normally but when I try to execute the compiled code ex:"java Integers" this appears: Error: Could not find or load main class Integers Caused by: java.lang.Noclassdeffounderror: javaLoiane/aula11/Integers (Wrong name: Integers) NOTE: When I try to run directly without compiling ex:"java Integers.java" the code also works normally, and this error is also happening in Vscode’s Runner code but it does not happen if it runs with Vscode’s own Java extension

package javaLoiane.aula11;

public class Inteiros {

    public static void main(String[] args) {

        final byte idade01 = 20;

        final short idade02 = 21;

        final int idade03 = 22;

        final long idade04 = 23;
        
        System.out.println("Valor variavel idade01 = " + idade01);
    }
}
  • Could you show the commands you’re using on the command line? It may be that you are running the command in the wrong folder and this causes a problem to find the right package.

1 answer

1

Good afternoon! There is no problem with the code itself. However, since the Integers class is in the javaLoiane.aula11 package, the execution command must be performed in the base directory where the package is located (folder) boar, and refer to the class by its full name javaLoiane.aula11.Integers.

I performed a test creating this package on the desktop and compiled it as follows:

javac -cp C:\Users\mprado\Desktop javaLoiane\aula11\Inteiros.java

And ran the compiled class:

java javaLoiane.aula11.Inteiros

Follow image with the commands above executed and the output: execução do código

I hope you solve the problem!

Browser other questions tagged

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