[JAVA] If macOS Catalina says "Error: Main class XX was not found or could not be loaded"

This time is my memorandum that MacOS was so good that I got hooked on the swamp without permission.

environment

macOS Catallina

What happened

It was when I was writing Java source code for the first time in a while and wanted to compile it from the terminal.

javac -cp "Various classpaths" -d Output directory./package name/Example.java

And hit normally. A class file will be generated. So far, everything went well.

$cd output directory
$java package name/Example
error:Main class Example was not found or could not be loaded

orz

I did it.

As a result of various investigations, I finally got nothing, so I wrote an article with common patterns. (Foreshadowing later, this time I was working under the Desktop directory.) Deliver this feeling to those who did the same thing ...

Pattern1 directory structure is different

This time ~~ I'm getting along with it ~~ I've divided it into a src directory and a bin directory. I also separated the packages, so I started with the directory structure.

If you do this first, as the error says (due to misunderstanding of the location etc.), even if the class file does not really exist, it will be discovered here.

project/
      ├src/package name/
      │            └─Example.java
      └bin/package name/
                   └─Example.class

There was no problem.

Pattern2 main method is missing

The main method is required for the class executed by java. Occasionally I see people who are not writing and are giving familiar errors.

Pattern3 environment variable CLASSPATH

I haven't used it, so I wasn't aware of it, but there seems to be a pattern that fails by setting this poorly.

$ echo $CLASSPATH
$

Well, I've never used it, so it's natural, but it was okay.

What should I do when I come here? .. ..

Pattern4 macOS is smart

When I was tired after trying various things, I remembered something like (sometimes I didn't discover this much in the first place ...?) O. (; ´Д `). So, if you think about it and look at "System Preferences" → "Security & Privacy"![Screen.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0 /230044/585b81f2-f0f8-d644-a142-4fa229319b21.png) This guy is hidden! Yes it is a solution.

Summary

I don't think everyone has such a ridiculous setting, but on the net "Error: Main class XX was not found or could not be loaded" I couldn't solve it, and the error was a permission issue, but it was hard to understand, so I wrote it for someone who makes the same mistakes as I might see later.

Recommended Posts

If macOS Catalina says "Error: Main class XX was not found or could not be loaded"
Error: Main class not found or could not be loaded
[Java] Main class XXX 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
ERROR! MySQL server PID file could not be found!
Spring-Boot main class not found