Error: Could not find or load main class in intelliJ IDE

JavaCompiler ErrorsIntellij Idea

Java Problem Overview


I'm a beginner in Java and am trying to run my code using IntelliJ that I just installed as my IDE with JDK 1.7. The following piece of code keeps does not even compile and keeps giving me the error:

Error: Could not find or load main class libTest

##Code

import java.lang.Integer;
import java.lang.String;
import java.lang.System;
import java.util.*;

class book {

    private String name = "trial";
    private int bookCode=1;
    private int issued=0;

     public void Issue(){
         if(issued==0) {
             issued=1;
             System.out.println("You have succesfully issued the book");
         }
         else {
             System.out.println("The book is already issued. Please contact the librarian for further details");
         }
    }

    public int checkCode() {
        return bookCode;
    }

    String readName() {
        return name;
    }

    public void setName(String newName){
        name=newName;
    }

    public void setBookCode(int newCode){
        bookCode=newCode;
    }
}

class library {
    private ArrayList books=new ArrayList();

    public void getList(){
        for(int bk:books){
            String bName=books(bk).readName();
            System.out.println((bk+1)+")  "+bName);
        }
    }
}

public class libTest{
    public static void main(String[] args){
        library newLib= new library();
        System.out.println("code working");
   }
}

Is there any change that i have to make in the compiler settings?? Or is it the code.

Java Solutions


Solution 1 - Java

This might help:

  1. "Build" menu -> "Rebuild Project". Sometimes Intellij doesn't rewrite the classes because they already exist, this way you ask Intellij to rewrite everything.

  2. "Run" menu -> "Edit configuration" -> delete the profile -> add back the profile ("Application" if it's a Java application), choose your main class from the "Main Class" dropdown menu.

3)"Build" menu -> "Rebuild Project".

Solution 2 - Java

If none of the above answers worked for you, just close your IntelliJ IDE and remove the IntelliJ IDE file and folder from the root of your project:

rm -rf .idea *.iml 

Then open the project with IntelliJ. It must work now.

Solution 3 - Java

For me the solution was to fix the output directory under project settings. Before I was using just "target" for the Project compiler output. Instead I updated it to have a full path e.g. D:\dev\sigplusjava2_68\target enter image description here

Solution 4 - Java

I had this problem and I tried everything under the sun that I could think of and on this site.

None of my Java classes were being picked up after I pulled from a remote branch. All the classes had red Js by their names in the Project Hierarchy, not blue Cs.

In the end, I tried to follow this tutorial and a few steps in tried something not described and fixed the issue: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html

Here's what I did:

  1. Goto File | Project Structure, or press Crtl+Shift+Alt+S
  2. Select Modules under the Project Settings section.
  3. In the Sources tab click Sources on the 'Mark as:' line.
  4. Click the Apply button.

For some reason, all my classes then had blue C's.

Someone with a better understanding of how IntelliJ and/or IDE's might be able to explain the phenomenon, but all I know is now it can see all the classes and more importantly the main one, and run.

Solution 5 - Java

Invalidate cache and restart your IntelliJ, it worked for me.

File menu → Invalidate Caches / Restart... option

Solution 6 - Java

Explicitly creating an out folder and then setting the output path to C:\Users\USERNAME\IdeaProjects\PROJECTNAME\out

LIKE THIS

seemed to work for me when just out, and expecting IntelliJ to make the folder wouldn't.


Also try having IntelliJ make you a new run configuration:

Find the previous one by clicking

![Edit Configurations

then remove it

enter image description here

and hit okay.

Now, (IMPORTANT STEP) open the class containing your main method. This is probably easiest done by clicking on the class name in the left-hand side Project Pane.

Give 'er a Alt + Shift + F10 and you should get a

THIS

Now hit Enter!!

Tadah?? (Did it work?)

Solution 7 - Java

File > Project Structure > Modules > Mark "src" folder as sources. This should fix the problem. Also check latest language is selected so that you don't have to change code or do any config changes. enter image description here

Solution 8 - Java

I know this was asked a while ago, but I was just stumbling over this issue and thought my findings might help others. As pointed out, the error message is basically a result of the out folder. That's because, when you're trying to run the program, it compiles the code first, and puts the compiled result to the out location, and then it tries to load the compiled code from the out location. If the compiled code is not in the location expected, you'll get the error.

The point I'm particularly wanting to share is that some times, the code is not compiled (built), even though your run configuration specifies "Build" in the "Before launch" section of the configuration panel.

When can this happen? One situation that can cause this to happen is if you're using modules and you manually delete the module out directory. For example, if I have a module named "foo", there should be a directory named foo under out/production. If you manually delete it, the build system may not know that it needs to be rebuilt.

Even worse, if you select Build | Build module 'foo', it still may not rebuild the module. If that's the case, you should select a file in the module, for example 'bar.java' and then select Build | Recompile 'bar.java'. Now the out directory out/production/foo should be restored.

Since IntelliJ typically knows about any changes going on, this surprised me, and took me a little time to figure out, so I thought I'd share.

Solution 9 - Java

Check your class module : I have encountered this problem with intellij : I have a maven multi-module project, the problem is that i runing a class which not exist the module within the configuration, so my problem is fixed by setting the right module ("edit configuration" -> "use class of module")

may this help you

Solution 10 - Java

I had to mark the "src" folder as "Sources". After restarting IntelliJ and rebuilding the project I could run the project without further issues (see screenshot). Edit: You can access the "Project Structure" tab via File->Project Structure or by pressing Ctrl+Shift+Alt+S.

Solution 11 - Java

Open Modules Tab (Press Ctrl+Shift+Alt+S). I had two modules under one project. I've solved the problem after removing the second redundant module (see screenshot).enter image description here

Solution 12 - Java

I have faced such problems when the class is in the default folder, i.e. when the class does not declare a package.

So I guess using a package statement (eg. package org.me.mypackage;) on top of the class should fix it.

Solution 13 - Java

After creating your project in intelliJ, try running the following command:

mvn package

Solution 14 - Java

I ran into this problem when my Java class was under src/main/kotlin. After I moved it to src/main/java, the problem was gone.

Solution 15 - Java

I have tried all the hacks suggested here - to no avail. At the end I have simply created a new Maven application and manually copied into it - one by one - the pom.xml and the java files and resources. It all works now. I am new to IntelliJ and totally unimpressed but how easy it is to get it into an unstable state.

Solution 16 - Java

In my case the problem seemed to be related to upgrading IntelliJ. When I did this I overwrote the files from the old IntelliJ with the files from the new IntelliJ (2017 community to 2018 community). After that all of my projects were broken. I tried everything in this thread and none of them worked. I tried upgrading gradle to the latest version (4 to 4.8) and that didn't work. The only thing that worked for me was deleting the entire IntelliJ folder and reinstalling it. All of my projects worked after that.

Solution 17 - Java

Invalidating cache didn't work.

I edited the main class java file with a dummy change and ran it. It worked.

Solution 18 - Java

For me - i tried few of the options above, did not work. Then i just renamed my Application class and that probably forced intelliJ to build a fresh jar and error message started to change. Then i renamed it back and it worked.

Solution 19 - Java

Mark the directory as a source directory. Opened via Ctrl+Shift+Alt+S

enter image description here

Solution 20 - Java

enter image description here

modules.xml with wrong content, I don't know what's matter with my IDEA.

Solution 21 - Java

I inherited a bunch of .JAVA files from elsewhere and couldn't figure out how to get them to work in any IDE. Ultimately I had to go to the command line where the Main.JAVA file was and run javac Main.java. This created a bunch of .CLASS files. The IDE's were then able to figure out what to do.

Solution 22 - Java

I got this error when using Scala/SBT. IntelliJ could not find the main class, even though everything was set up correctly. My solution: delete the <user>/.sbt/<version>/plugins/target folder, then restart IntelliJ.

Solution 23 - Java

You probably would have specified a wrong package and the package hierarchy would not be right. Look below

enter image description here

The ide would highlight the wrong path in that case.

Solution 24 - Java

Another thing you can check here is the actual command that is being passed to the JVM and make sure it looks OK. Scroll to the top of your Run console, it should be the first line.

Spaces in your Run Configuration VM Options field will malform the app startup command and can result in this error message

-DsomeArgument="arg with space must be quoted"

Solution 25 - Java

I'm using IntelliJ with Spring and my main class is wrapped in a JAR. I had to mark the 'Include dependencies with "Provided" scope' in the Run/Debug configuration dialog

Solution 26 - Java

We are at File/Project Structure.. Answer might be:

Folder indicated as "content root" needs a child folder where the code is. Plus find the button that marks code as excluded and not. Not to be confused with tickbox that states excluded without telling in what phase and what** Is it compiler exclude or runtime exclude? You are doomed to test and lot. So no that tickbox but icons and colors.

As an idea we need to crack how it was originally thought to work. They never got it to work in first place and started add things in premature codaculation style. It has been so many years and you cannot expect any improvement. But as cure we can hack out some way to get it right every time.

Solution 27 - Java

I have tried almost everything suggested in the answers here, but nothing worked for me.

After an hour of just trying to run my application, I noticed that my project's path included non-ASCII characters (Arabic characters). After I moved my project to a path with no non-ASCII characters, it executed just fine.

Solution 28 - Java

I am working with Kotlin but am guessing the problem is the same. I would start a project, create a single file and add main to it and the IDE couldn't find the main.

I tried the things in this list and none worked. I finally mentioned my frustration on one of the IntelliJ pages and was contacted. Of course, it worked fine for IntelliJ. After a couple of days back and forth, I noticed that the highlight function wasn't working and mentioned that. It turned out something was wrong with the IDE settings. I still don't know specifically what was wrong but the fix in my case was to reset the IDE settings. File->Manage IDE Settings->Restore Default settings.

After this, the green triangle start icon became visible to the left of my main function and things continued to work normally for subsequent projects.
Thanks to Konstantin at JetBrain's support for his patience.

Solution 29 - Java

  1. choose maven as the type while creating project (do not check create from archetype checkbox)
  2. Inside Project explorer, right click on java under main under src
  3. choose new class
  4. complete the class with public static void main
  5. choose run from run menu

intellij option to create a new java class

Solution 30 - Java

Elaborating on Brad Turek's solution... One of the default IntelliJ Java project templates expects a file called Main defining the class Main and main() method entry point. If the method is contained in another file (and class), change the Run configuration:

Run Configuration Window

  1. With the project open in IntelliJ, use the Run:Edit Configurations... menu to open the build configuration window.
  2. If the entry for Main class doesn't list the name of your file containing the class exposing the main() entry method, enter the correct file name. (The configuration in the image is wrong, which is why it's red in the configuration panel.)
  3. My main() entry method is in the class (and file) ScrabbleTest. So changing Main class: to ScrabbleTest fixes the runtime error.

As others have noted you have to ReBuild using the new configuration. I am using a package, but that doesn't seem to make a difference IME. Hope this helps.

Solution 31 - Java

In my case in the module's build.gradle the mainClassName assignment must state the fully qualified class name, ie. as specified as the package name in the main class' source code file.

Solution 32 - Java

Just re-build the project (CTRL+F9) and double check (Edit Configurations) if you have a properly set Main class, and module (if any)

Solution 33 - Java

Simple solution is go to the backed folder in command line, where the root folder of the project. You should have SBT installed in your machine.

If you have sbt installed, please run the command "sbt package", this will recompile the project if it success, come back to the intelliJ it will work.

Solution 34 - Java

In my case, I had (relatively) long vm arguments configured. Converting these arguments (whenever possible) to environment variables fixed the issue for me

Solution 35 - Java

In my case, the application runs well when code initially was under /src directory under the project. Later I added /main/java directory after the /src, then "Could not find or load main class ..." error happened. At that time you couldn't add /src/main/java/YourMain.java to the run configuration. What I did was to remove the run configuration, then right click the project->Open Module Settings (or F4 after click the Project panel)->Project Settings->Modules delete (x) the old Content Root and + Add Content Root, choose /src/main/java. Rebuild the project and it runs.

Solution 36 - Java

In my case, it is a maven project, I Reimported maven (right click on pom.xml file and click Reimport) that's it worked immediately.

Solution 37 - Java

Try to check your project name and full directory name, see if contain any "un-English" words(like Asian words), sometimes this could be the fault. Rename the project name or project path contains such words and reopen it might work.

Solution 38 - Java

in my case it helped to remove the lines build.gradle

compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '8.2.0.jre11'

I don't know why this happens, but after adding the specified library, the compiled jar stops working

Solution 39 - Java

I meet same problem and solved it by:

  • Closing all IDEA windows
  • Delete C:/Userz/xx/.IdeaIC2019.3 folder
  • Reopen IDEA

Solution 40 - Java

If you are using gradle try rebuilding the project by the command from the command line:

gradle clean build 

Solution 41 - Java

please make sure your file name extension is ".kt"

Solution 42 - Java

Goto File-> Invalidate Caches and Restart . Else delete rm -rf .idea *.iml and restart InteliJ

Solution 43 - Java

I got the issue when I went to "Edit configurations" -> "Modify options" and checked the "Do not build before run" option, as I use IntelliJ for short programming problems and I don't want it to run the build every time.

What I instead did is to assign a shortcut that runs the build process (only for the first time after a class is created), which fixes the error and then another convenient shortcut to "Run context configuration" that successfully runs the main method in whichever class is currently in focus.

Solution 44 - Java

Tried everything, no luck. Finally, the simple solution that worked for me (on a Mac) was:

  • while the project is open, go to the finder and move the src directory out of the project folder. IntelliJ realizes that the src is not there any more, and updates itself.
  • with the project still open, move the src directory back in place. IntelliJ updates its project configuration, and at this point everything works.

Solution 45 - Java

Follow these steps

  1. Go to run
  2. Go to edit configurations
  3. Click the green colored + sign in top left corner
  4. Select the type you are working, for instance "Applications"
  5. Now enter the name of the class which you want to run instead of unnamed
  6. Do the same where it is written Main class,just below it.

Yippee....your code will run:)

Solution 46 - Java

If the class which has the main method is extending/implementing another class/interface then it will not find the main class. Change the signature and it should work.

I had the same problem tried almost every answer from this thread non of them worked. Then this worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionConfused MailmanView Question on Stackoverflow
Solution 1 - JavaEhsanView Answer on Stackoverflow
Solution 2 - JavayouhansView Answer on Stackoverflow
Solution 3 - JavaUncle IrohView Answer on Stackoverflow
Solution 4 - JavaSarah CohenView Answer on Stackoverflow
Solution 5 - JavaS_KView Answer on Stackoverflow
Solution 6 - JavaBrad TurekView Answer on Stackoverflow
Solution 7 - JavaSumit JadiyaView Answer on Stackoverflow
Solution 8 - JavamsoltysView Answer on Stackoverflow
Solution 9 - JavaAbdessattar NOISSIView Answer on Stackoverflow
Solution 10 - JavaNo-Name NemesisView Answer on Stackoverflow
Solution 11 - JavaTaras MelnykView Answer on Stackoverflow
Solution 12 - JavaHakan SerceView Answer on Stackoverflow
Solution 13 - JavalleiouView Answer on Stackoverflow
Solution 14 - JavaBig PumpkinView Answer on Stackoverflow
Solution 15 - JavaPierluigi VernettoView Answer on Stackoverflow
Solution 16 - JavablindcantView Answer on Stackoverflow
Solution 17 - JavaSmileView Answer on Stackoverflow
Solution 18 - JavaJavaTecView Answer on Stackoverflow
Solution 19 - JavakguiView Answer on Stackoverflow
Solution 20 - JavaibambooView Answer on Stackoverflow
Solution 21 - JavacodeMonkeyView Answer on Stackoverflow
Solution 22 - JavaBeni MurzaView Answer on Stackoverflow
Solution 23 - JavaAkash YellappaView Answer on Stackoverflow
Solution 24 - JavaYepView Answer on Stackoverflow
Solution 25 - JavaOHYView Answer on Stackoverflow
Solution 26 - JavaKurskinenView Answer on Stackoverflow
Solution 27 - JavalostpersonView Answer on Stackoverflow
Solution 28 - Javasteven smithView Answer on Stackoverflow
Solution 29 - JavaVijayView Answer on Stackoverflow
Solution 30 - JavaJHakeView Answer on Stackoverflow
Solution 31 - JavaMatthewView Answer on Stackoverflow
Solution 32 - JavaAlexPesView Answer on Stackoverflow
Solution 33 - JavaRatheeshView Answer on Stackoverflow
Solution 34 - JavaAhmed SayedView Answer on Stackoverflow
Solution 35 - JavaTGUView Answer on Stackoverflow
Solution 36 - JavaddcView Answer on Stackoverflow
Solution 37 - Javaghudt yiView Answer on Stackoverflow
Solution 38 - Java mark99View Answer on Stackoverflow
Solution 39 - JavaChrisZZView Answer on Stackoverflow
Solution 40 - JavaRubusView Answer on Stackoverflow
Solution 41 - JavaEmre MemilView Answer on Stackoverflow
Solution 42 - JavaShyamView Answer on Stackoverflow
Solution 43 - JavaHarshaView Answer on Stackoverflow
Solution 44 - JavaMarcoView Answer on Stackoverflow
Solution 45 - JavaNikhil GoyalView Answer on Stackoverflow
Solution 46 - JavaxXTurnerView Answer on Stackoverflow