Java - Could not find main class

1

The code below does not compile:

package app1;

public class main{
  public static void main(String args[]){
    System.out.println("Hello World!");
  }
}

The following message appears in the terminal:

  

Error: Could not find or load main class main

the main.java file is saved in the app1 folder. I am compiling the terminal in Manjaro.

I do not understand, when I compile in the IDE (either NetBeans or Eclipse) the code runs, but the terminal goes wrong. Thanks in advance for your attention.

    
asked by anonymous 11.11.2017 / 05:48

2 answers

2

The error is in this call: java main

You should use the fully qualified class name in this case: app1.main. However, it will still not work. Java will try to get into an app1 package to find the class. However, you are already inside the package. For the call to work, you need to add the parent package from app1 to the java classpath. The classpath is a sort of directory list where java will look for the classes needed to execute the code.

So your call should be: java -cp .. app1.main

To make the command easier you can exit the app1 folder and go to the parent folder of it. This way, you can execute this command: java app1.main

Just clarifying a few things. Your code does not give compilation error. What happens is a runtime error. Java can not find the class with main because you are not passing the correct name and are not telling you where to find the class.

Tip: Name classes in java should begin with a capital letter.

    
14.11.2017 / 03:43
0

You should make sure that the location of your .class file adds to your classpath. So if it's in the current folder, add it to your classpath. Note that the Windows classpath separator is a point-point, that is,

    
11.11.2017 / 06:05