[Java] Main class XXX was not found or could not be loaded

Why can't I just copy and create another file? Even though the class file has been generated! !!

Create MyApp.java in java_lessons / com / dot / myapp. Create Practice.java by copying MyApp.java. Write what you want to try in Practice.java.

I put the outer class of MyApp.java in java_lessons / com / dot / myapp / model and loaded it, but I want to try a simple code without loading the outer class, so I removed package and import I created Practice.java.

And the compilation went well, but I can't run it.

When trying to run

In a loop with the error Main class com.dot.myapp.Practice was not found or could not be loaded.

When I looked it up, I managed to write the PATH, but I created a test directory separately from the java_lessons directory, moved Practice.java to it, and compiled and executed it.

I thought that it might be something about PATH.

But I don't know what to do.

Hmm, what should I do when I say this? I wish there was someone who could ask questions, but I'm not self-taught! You can ask on the net, but then I want to deal with it myself!

Process to resolution

If you can list some possible causes for the time being, if you look into each cause, you should be caught in the search with similar error messages! !!

Investigate the cause by comparing the file you created with the copy source file that could be executed

  1. main seems to be a special method, so it's a separate file, but is it strange that main exists in the same hierarchy?
  2. I deleted it because I don't use the package, but I don't use it, but can I write it?
  3. Is it necessary to set the PATH after all?
  4. I connect with vagrant with ssh and execute it, but I can't save the file and can't read the executable file?
  5. I'm using Cyberduck for FTP, but it's not working?

The rest is trial and error

1. Action

It was deep in the hierarchy of java_lessons, but first create another directory and move it.

  1. Create a test folder next to java_lessons
  2. Move Practice.java there
  3. Go to test in the terminal
  4. Compile and execute
  5. I was able to execute → Is there only one main method in the same hierarchy?
  6. Create a test / practice folder
  7. Move files
  8. Compile and execute
  9. I can't → There is only one main ...
  10. Move to test again
  11. Compile execution
  12. Done → Directory problem, not the number of main
  13. Move to the directory containing the file you want to run in the terminal and then run
  14. It's done! !!

harvest Just move to the terminal and then run.

However, the original MyApp could be done deep in the hierarchy, right?

So there is a way to do it without having to go to the terminal and then run it.

So

2. Action

I don't use the package, but let's declare the package for the time being!

package com.dot.myapp;

With this at the beginning, the first directory, that is, Return Practice.java to java_lessons / com / dot / myapp and compile.

I got it.

harvest If you declare a package, you can execute it even if you are deep in the hierarchy. But do I have to write the class in the directory model one level below even though I'm not using it?

Experiment by moving the file again here.

Move to java_lessons / com, declarepackage com;and execute.

did it

harvest If it's below the directory where the terminal is located, write the directory for that file in the package declaration!

Your own conclusion

I think that packaging is a method to manage files with only function and define in model in PHP, but when executing it, it is necessary to write it when it is under the directory where the terminal is located. thing.

Your own harvest

After listing some causes, I happened to find a solution at the beginning, but I wonder if it's quick to actually try various things anyway.

Even if you search for the error message as it is, it may get stuck, but it is better to think about it yourself, try it, and solve it.

Deepening your understanding makes it easier to list possible causes, if not in the same situation, in similar situations, resulting in faster resolution!

Anyway, let's think for ourselves and solve it by ourselves to speed up!

Recommended Posts

[Java] Main class XXX was not found or could not be loaded
Error: Main class not found or could not be loaded
If macOS Catalina says "Error: Main class XX was not found or could not be loaded"
"Error: Main class XXX not found or could not be loaded" occurs even though the main class exists
Today's java error: Error: Could not find or load main class
Spring-Boot main class not found
The story that the Servlet could not be loaded in the Java Web application
ERROR! MySQL server PID file could not be found!
HttpServletRequest class file not found
Symbol not found error when new class in another Java file