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

3 minute read

I just copied another file and created it, so I can’t execute it? The class file is also generated! !!

Create MyApp.java in java_lessons/com/dot/myapp. Copy MyApp.java and create Practice.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 fine, but I can’t run it.

When trying to run

In the loop of the error main class com.dot.myapp.Practice could not be found or could not be loaded.

When I looked it up, I wrote about PATH, but I was able to create a test directory separately from the java_lessons directory, move Practice.java to it, and compile and execute it.

I wondered if that wasn’t something about PATH.

But I don’t know what to do.

Hmm, what should I do when I say this? It would be nice if someone could ask questions, but not because I’m self-taught! You can listen to it online, but then you can manage it yourself!

Process to resolution

For the time being, if you can enumerate several possible causes, you can investigate each cause, and you will get caught in the search along with similar error messages! !!

Investigate the cause by comparing the file you created with the copy source file that you were able to execute

  1. main seems to be a special method, so it’s a different file, but is it strange that main exists in the same hierarchy?
  2. I deleted it because I’m not using the package, but I’m not using it. Can I still write it?
  3. After all, is it necessary to set the PATH?
  4. I’m connecting with ssh with vagrant and executing it, but I can not load the executable file because the file can not be saved?
  5. I’m using Cyberduck for FTP, but it’s not working?

The rest is trial and error

Dealing with ####1. 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 to it
  3. Go to test in the terminal
  4. Compile and execute
  5. Could be executed → Is there only one main method in the same hierarchy?
  6. Create 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 execute in the terminal and then execute
  14. It’s done! !!

harvest Just run it after moving in the terminal.

But the original MyApp could have been created deeper in the hierarchy?

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

So, Dealing with ####2. 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, I have to write the class in the directory model, which is one directory below, even though I am not using it?

Experiment by moving the file again here.

Move to java_lessons/com, declare package com; and execute.

did it

harvest If the directory is under the directory where the terminal is, you can write the directory for that file in the package declaration!

My conclusion

I think that packaging is a way to manage files such as function and define in PHP by putting them in model, and when executing it, it is necessary to write if it is under the directory where the terminal is. 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.

Sometimes searching for an error message as it is can be confusing, but it is better to think by yourself and try to solve it.

Deeper understanding makes it easier to list possible causes in similar situations, even if they are not in the same situation, resulting in faster resolution!

Anyway, think for yourself, do the number of times you solve yourself, and speed up!

Tags:

Updated: