user2094257 user2094257 - 1 year ago 91
Java Question

Eclipse can't find my main class

I am trying to run my first Eclipse Java project but for some reason Eclipse can't find


I created a new Java run configuration and added my project's source file directory to the user entries under the classpath tab. Then I added my main class
to the
class field under the main tab. When I run my project I get the following error.

Error: could not find or load main class DisplayDeck.

Here is my code:

package cards;

public class DisplayDeck {
static void main(String[] args) {
Deck cardDeck = new Deck();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
card tempCard = cardDeck.getCard(i, j);
System.out.format("%s of %s", tempCard.rankToString(tempCard.getRank()), tempCard.suitToString(tempCard.getSuit()));

I've read a few similar questions on here but none of the solutions to those questions seems to be working... Something else I noticed is when I choose the search button next to the
class field under main in the build configuration there's no classes to choose from in the

Answer Source

In code snippet you have static void main(String[] args) and not public static void main(String[] args). This is why main is not found.

From :

The method signature for the main() method contains three modifiers: public indicates that the main() method can be called by any object. Missing Page covers the ins and outs of access modifiers supported by the Java language: public, private, protected, and the implicit, friendly. static indicates that the main() method is a class method. Class Members vs. Instance Members later in this lesson talks in more detail about class methods and variables. void indicates that the main() method has no return value. The main() method in the Java language is similar to the main() function in C and C++. When you execute a C or C++ program, the runtime system starts your program by calling its main() function first. The main() function then calls all the other functions required to run your program. Similarly, in the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application. If you try to run a class with the Java interpreter that does not have a main() method, the interpreter prints an error message. For more information see Troubleshooting Interpreter Problems .

Arguments to the main() Method

As you can see from the code snippet above, the main() method accepts a single argument: an array of Strings.

public static void main(String args[])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download