Solving Error: Could not find or load main class HelloWorld It's Java class loader who looked into CLASSPATH for loading classes.Ĭoming 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. While, CLASSPATH is used to locate the class file or JAR files. ![]() It is also used to locate native libraries used by your Java program. This way you always know which JARs are included in your classpath.įor beginners, another important thing to understand is the difference between PATH and CLASSPATH, you must know that PATH is used locate system executable, commands or. Instead, I use -cp or -classpath option with java command to run my program. ![]() Personally, I don't like this environment variable and doesn't define in my environment variable, because its confusing and source of so many classpath related issue. In Eclipse, it's easy to compile and run the program because Eclipse takes care of all Classpath setup, but when you run your Java program from command line, CLASSPATH environment variable comes in picture. Since many Java programmer is now started programming using Eclipse they face this issue when they first try to run their Java program from command line. For now, you just remember that there is an environment variable called CLASSPATH which includes directories where Java looks for all class files and if it doesn't find your main class there then it throws "Error: Could not find or load main class XXX", where XXX is the name of your main class. For the curious reader, I would suggest reading my post How Classpath works in Java, a must read for a beginner. In order to solve this error, you must know how Java find and loads the classes, that's a little bit complex topic for beginners, but we will touch the same base here. The only solution I found so far is to manually edit this file and remove duplicate lines (in my project zip, these dulicate lines are removed).Error: Could not find or load main class HelloWorld comes when you are trying to run your Java program using java command with the main class as HelloWorld but Java is not able to find the class. idea\libraries\sbt_org_scala_lang_scala_library_2_13_0_jar : each of the line below is duplicated, which causes a complie error : SCALA Plugin v 2019.1.9, vendor : JetBrainsĪnother problem which I ma having are duplicate lines in SBT configuration file. JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o I uploaded my project to jetbrains, zip file name is BorisAshmanSolutions.zip And you should see the build icon (Hammer). You should now see the execute arrow at that top right of Intellij. Now click on the Ellipsis (three dots) in “Main Class”. Then, click on “Add Configuration” (top right corner of Intellij)ĭ. ![]() This adds src to the “Source folders” on the right. Then, Click on the “Sources” folder icon on the top. Click on src (this is where my source code sits).ī. Go to Project Structure and Select ModulesĪ. Project Language Level: I chose “SDK Default”Ģ. (I think this was what was the main step missing) Set the project compiler output to point to where I want the compiled output to sit: C:\Users\kbala\OneDrive - MatrixCare\SoftWLearnings\Javamyfun\NewHelloWorld\outĬ. Set the SDK to 10 (“Java Version 10.0.2”) or whatever you are using.ī. ![]() Go to Project Structure and Select ProjectĪ. Here are the steps that finally worked on my localhost environment:ġ. My Problem: Whenever I exit Intellij and try to open up an old project, I have trouble getting the builds and execute to work. This is finally what worked for me! Phew! Serge, Thanks for staying with me on this.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |