How to solve the problem "The main method was not found" in a Java class?

Asked

Viewed 3,518 times

5

I created this Arraylist and at the moment of execution the following message appears:

Error: Main method not found in class Declaracaoarray.Declaracao_array; set the main method to:
public Static void main(String[] args)

Reading the message, and with the help of colleagues here at SOPT, it is noticed that in my code the main method is missing, my doubt is Where do I put this method? I have tried in many ways, but without success.

What would my code look like with this new method?

package DeclaracaoArray;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public String sorteia (){

            List<String> lista = new ArrayList<String>();

            lista.add ( "Alice" );
            lista.add ( "Bruno" );
            lista.add ( "Carlos");
            lista.add ( "Daniel");

           Collections.shuffle ( lista );   

           return lista.get(0);
        }
    }
  • Is this your main class? If so, as the error message already says, the void main in it,

  • Are you trying to run this class only? Without the main method it won’t run at all. Add the <code>'public Static void main(String args[]){}'</code> method and run again.

3 answers

4


This code can go anywhere within your class, for example like this:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args) {
        Declaracao_Array d = new Declaracao_Array();
        System.out.println(d.sorteia());
    }

    public String sorteia (){
        List<String> lista = new ArrayList<String>();

        lista.add ( "Alice" );
        lista.add ( "Bruno" );
        lista.add ( "Carlos");
        lista.add ( "Daniel");

        Collections.shuffle ( lista );   

        return lista.get(0);
    }
}

For the above case I created an object of class type so that it was possible to call the method sorteia() otherwise it would not be accessible, but you still have the option to do otherwise by defining the method as static and accessing it directly from the main method without the need to instantiate an object. Thus:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args) {
        System.out.println(sorteia());
    }

    public static String sorteia (){
        List<String> lista = new ArrayList<String>();

        lista.add ( "Alice" );
        lista.add ( "Bruno" );
        lista.add ( "Carlos");
        lista.add ( "Daniel");

        Collections.shuffle ( lista );   

        return lista.get(0);
    }
}

3

You need to have a method with the following signature

public static void main(String[] args)

this will be the entry point of your application. It will be the first method to be executed and is therefore required.

Applying to your code, it would look something like:

package DeclaracaoArray;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args){
        sorteia();
    }       

    public static String sorteia (){

        List<String> lista = new ArrayList<String>();

        lista.add ( "Alice" );
        lista.add ( "Bruno" );
        lista.add ( "Carlos");
        lista.add ( "Daniel");
        Collections.shuffle ( lista );   

        return lista.get(0);
    }
}

3

Example:

public class Declaracao_Array {

     public static void main(String[] args) {

     }

     public static void metodo(){

     }

} 

Browser other questions tagged

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