DEV Community

loading...

Why won't Eclipse run my program? (Possibly the unresolved error from before).

baenencalin profile image Calin Baenen ・1 min read

Whenever I try to run a test of RuntDeale (a basic program that creates a JFrame, with a black background, that is not resizable), in the standard form that I make my programs in:

MainClass:
  constructor() {/* set up key components to initialize a program instance */}
  program-entry() {/* make an instance of the main class (program), and run it with its "run" method */}
  run() {/* program logic */}
Enter fullscreen mode Exit fullscreen mode

Here, it raises java.lang.ClassNotFoundException:

Error: Could not find or load main class RuntDeale.code.Main
Caused by: java.lang.ClassNotFoundException: RuntDeale.code.Main
Enter fullscreen mode Exit fullscreen mode

With the recurring problem (in the "Problems" tab) with "The project was not built due to "Failed to init ct.sym for C:\Users\Administrator\AppData\Local\Temp\eoiDF83.tmp\plugins\org.eclipse.justj.openjdk.hotspot.jre.minimal.stripped.win32.x86_64_15.0.1.v20201027-0507\jre\lib\jrt-fs.jar". Fix the problem, then try refreshing this project and building it since it may be inconsistent" (I don't know if this is related to the problem, but I thought I'd include it just in case it was).

Image of Eclipse IDE raising an error (ClassNotFoundException).

Could someone please explain why it won't run?

Thanks!
Cheers!

Discussion (8)

pic
Editor guide
Collapse
thorstenhirsch profile image
Thorsten Hirsch • Edited

I guess the Main() constructor has to be public.

edit: Nope. I've found your code in the other thread (here) and your main method, which can access the private Main() constructor, so my guess was wrong.

Collapse
baenencalin profile image
Calin Baenen Author

Tried it. It doesn't work.

The visibility of the class doesn't (and shouldn't) matter. It's the visibility of the entry point (Main.main).
But that's not even the error, whether it's public or not, it says it can't find the class (with the same details).

Collapse
thorstenhirsch profile image
Thorsten Hirsch • Edited

In such a case I try to run things from the command line. And it works:

$ javac -cp . RuntDeale/code/Main.java
$ java RuntDeale.code.Main
$ tree
.
└── RuntDeale
    └── code
        ├── Main.class
        └── Main.java
Enter fullscreen mode Exit fullscreen mode

So the problem is probably some wrong setting in Eclipse or a different directory structure than what Eclipse expects. My directory structure in the shell snippet is plain java with a directory for each part of the package name (separated by a dot). Java's classpath needs to be set to the current directory, so that it can find the Main class (-cp .). But your Eclipse project has at least an additional src directory as entry point. When I start new java projects I use maven or gradle and follow their directory structure, which also have your srcdirectory, but additionally a main and a java directory. Then I can use any Java IDE, because they all support maven's directory structure.

P.S.: Actually you don't need -cp, yet, as long as you've got a single .java file only.

Collapse
baenencalin profile image
Calin Baenen Author

On second thought, what exactly causes the main class not to be found.
One may be mistyping main, or not providing a main class entry.

But what things can cause this error, and what can I do to resolve it (within my Eclipse project)?

Collapse
jffrydsr profile image
Jeffrey Desir

Wish I could help more, but your prowl for Java is making want to pick up another book and play with it again 🙏🏿 My best experience with Java was with Dr. Java. Many will say eclipse and intellij is the only real way to Java, but running a project for barebones config from the editor is the best way to learn the intricacies of application architecture with Java (that I'll say you've advanced further in with your projects than I did in College).

Collapse
baenencalin profile image
Calin Baenen Author

Well hey. Ig you have the itch, you should (this is not good advice for gamedev).

But hey. The best way to learn a language is to go in having a passiin, and training.

Collapse
gfvolpin profile image
Guille Volpintesta

The correct declaration for main() method is always :

public static void main(String[] arg) {
}

Collapse
baenencalin profile image
Calin Baenen Author

I know that. That's what I did.