Lỗi could not find or load main class trong jcreator năm 2024
Coming back to the problem in hand, if you are a beginner in Java, who are able to run the program from Eclipse but getting "Error: Could not find or load main class HelloWorld" when trying to run the same program from the command line then follow the steps given here to solve it. Solving Error: Could not find or load main class HelloWorldUnfortunately beginner's book like Head First Java, which many developers used to learn Java, doesn't teach you how to deal with this kind of errors. You need to build this skill by doing active development. In order to understand the problem little better, let's reproduce it. This is one of the most important troubleshooting skill which will help you a long way in your career. Half of the problem is solved when you are able to reproduce it. For our purpose we will use following HelloWorld program for our testing, interestingly I have named it HelloHP and it resides in a package called "dto". I have purposefully chosen a class with a package instead of HelloWorld in the default package because many programmers get "Could not find or load main class" error when they try to run a class which is inside a package. package dto; /
When you run this from Eclipse, by Right click on the source file and Choosing "Run as Java Program", it will run fine and print following line: My first program in Java, HelloWorld !! Everything as expected, Now we will try to run same Java program from command line. Since I am using Maven with Eclipse, its build process creates class files in project_directory\target\classes directory. If you are not using Maven with Eclipse, then you can see the class file created by Eclipse's Java compiler in project_directory\bin. It doesn't matter how those class files are created, but, what is important is the location of the class file. If your class is inside a non-default package e.g. "dto" in our case then compiler the will put the HelloHP.class file, which contains Java bytecode in a directory named "dto". In our case the full name of class dto.HelloHP and it is present in C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto. So in the first try, I go there and execute java command to launch my program, as seen below: C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto>java HelloHP Error: Could not find or load main class HelloHP Do you see the error? It's coming because the full name of the class should be dto.HelloHP and not HelloHP. So let's correct this error and try to run the same command from the same location but this time with fully qualified name: C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto>java dto.HelloHP Error: Could not find or load main class dto.HelloHP Still same error, right. Why? because I don't have any CLASSPATH environment variable, neither I am using -classpath or -cp option to suggest the path, So by default Java is only searching in the current directory. It is looking for dto/HelloHP.class but since we are already inside dto, it is not able to find the class. So, what should we do now? let's go to the parent directory "C:\Users\WINDOWS 8\workspace\Demo\target\classes" and execute the same command, this time, it should work: C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto>cd .. C:\Users\WINDOWS 8\workspace\Demo\target\classes>java dto.HelloHP My first program in Java, HelloWorld !! Bingo!!, our program ran successfully because, without any hint about where to find class files, Java is by default looking into the current directory, denoted by . (dot) and able to locate ./dto/HelloHP.class. Now, what if you want to run this program from any other directory? Well, for that purpose whether we need to define CLASSPATH or just use -classpath or -cp option. I like the second option because it's easier to control and change. Also, remember, it overrides any CLASSPATH environment variable. If you like to set CLASSPATH environment variable in Windows, see that tutorial. Now let's run the program target directory first without using -classpath option: C:\Users\WINDOWS 8\workspace\Demo\target\classes>cd .. C:\Users\WINDOWS 8\workspace\Demo\target>java dto.HelloHP Error: Could not find or load main class dto.HelloHP You can see we are again started getting the same error, Why? because Java is still looking into the current directory and there is no .\target\dto\HelloHP.class there, as it's one level down e.g. .\target\classes\dto\HelloHP.class Now let's run the same command using -classpath option from target directory itself: C:\Users\WINDOWS 8\workspace\Demo\target>java -cp ./classes;. dto.HelloHP My first program in Java, HelloWorld !! Bingo!!, our program ran successfully again because now Java is also looking at ./classes directory and there it is able to find dto\HelloHP.class file. There are many ways Error: Could not find or load main class HelloWorld manifests itself, but if you know the basics of Java Classpath, you can easily sort out the problem. Most of the time you just need to either correct your CLASSPATH environment variable or run your program with java -cp or -classpath option. By the way, there are more to it e.g. Main class defined in the manifest.mf file and that's why I suggest reading about How Classpath works in Java (see the link in the first paragraph). SummaryIf you are getting "Error: Could not find or load main class XXX", where XXX is the name of your main class while running Java program then do this to solve that error:
java com.abc.HelloWorld without any classpath hints, Java will look into the current directory and search for com\abc\HelloWorld.class in Windows, so if com directory exists in your current directory, your program will run otherwise you will get "Error: Could not find or load main class dto.HelloHP".
java -cp C:\test\;. com.abc.HelloWorld If you still facing any issue just check whether you have accidentally using CLASSPATH environment variable, you can check this in Windows by running echo %CLASSPATH% command and in Linux by running echo $CLASSPATH. If CLASSPATH is nonempty then it will print its value otherwise just echo the same command.
Here is the screenshot of how I tried to reproduce and solve the error as discussed in the previous paragraph: That's all about how to solve "Error: Could not find or load main class HelloWorld" in Java. Classpath is little confusing topic to master, but you will understand it once you started writing and running some Java program. If you are still not able to fix your problem then post a comment there with what you have tried and we will try to troubleshoot together. My goal is not just to give you solution but also make you able to explain why the solution is working and CLASSPATH basics are very important for a Java developer. I have seen many programmers getting frustrated, losing interest in Java due to various PATH and CLASSPATH issues e.g. NoClassDefFoundError and ClassNotFoundException and this is my humble effort to bring them back and empower with practical knowledge. Hope you understand. |