Java - Could not find main class

Asked

Viewed 1,066 times

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 file main.java is saved in folder app1. I’m compiling through the terminal at Manjaro.

I don’t understand, when I compile in the IDE (either Netbeans or Eclipse) the code runs, but the terminal goes wrong. I appreciate the attention already.

  • Command line to run which you are testing?

  • javac main.java [enter] java main [enter]

  • The way you are running, Java would expect you to be without a package in the class. You said it was inside the folder app1, right? Try to java -classpath ../ main. The -classpath is teaching Java which way it should go after the classes. How you are inside the folder app1 (you said above), then the root is above that, so the root is in ..

2 answers

2

The error is in that call: java main

You must use the full name of the class that in this case is: app1.main. However, it still won’t work. Java will try to enter an app1 package to find the class. However, you are already inside the package. For the call to work, you need to add the app1 parent package to the java classpath. Classpath is a kind 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 its parent folder. This way, you can run this command: java app1.main

Just clarifying a few things. Your code does not give build error. What happens is an execution error. Java can’t find the class with main because you’re not giving the correct name and you’re not telling us where to find the class.

Tip: Java class name must start with uppercase letter.

  • Thanks for the tip! I’m studying C and Java at the same time and end up confusing when using a capital letter or when not using. As for the classpath I do not know how to change. I will do a search on the internet and if I can solve I close the topic.

  • In fact, you will call the command exactly as I put it there which will work. You are calling the command in which folder?

  • In the folder where exactly is the file .java . /home/username/code/Filename.java

  • The correct one would be to have a folder called app1 and within the folder a file called main.java. The class name has to be equal to the class file name. The class.

  • I checked that if the class name does not match the file name, it gives build error. Then your file name is probably correct. Try to rename the code folder to app1 and then run one of the commands described in the reply.

0

You should ensure that the location of your . classfile adds to your classpath. Then, if it is in the current folder, add . to your classpath. Note that the Windows classpath separator is a dot-point, i.e.,;

  • I won’t lie, I don’t understand anything. I’m starting in the Java language now. I don’t use Windows, I use Manjaro Linux.

Browser other questions tagged

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