Reputation: 2739
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
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.
Upvotes: 263
Views: 600909
Reputation: 11
In my case, I name the file as "First Last" with space between. The project can be run after refactoring the name.
Upvotes: 0
Reputation: 2693
This worked for me:
Right-click the folder where your class is in >> down you'll see an option "Mark Directory As..." Then you mark it as Source root.
Upvotes: 1
Reputation: 556
remove any README.md files or files with .md extensions from your project
Upvotes: 0
Reputation: 1174
No-brainer straightforward answer for Windows
Delete the C:\Users\username\AppData\Local\JetBrains\IdeaIC2023.3
folder. The folder is present inside AppData
folder, which can be navigated from C:\
and then Users
, current user and there you can see the AppData
folder.
Close all IDEA windows
Delete C:\Users\username\AppData\Local\JetBrains\IdeaIC2023.3
Reopen IDEA
After deleting the IdeaIC2023.3
folder, and re-opening IntelliJ
it starts to re-structure the project, and the error gone.
Upvotes: 0
Reputation: 6869
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 should work now.
Upvotes: 280
Reputation: 5668
Upvotes: 0
Reputation: 323
After trying out almost every suggestion in this tread I was still out off luck. Tried them on two different machines. Consulted an experienced colleague.
And finally: A solution to my problem, and probably the cause of the isssue:
One of the folder names contained a forward slash on my Macs that's it
(We used a (deeply) nested folder structure on a shared drive; and my colleague works on Windows and doesn't have an issue with the forward slash in the path)
NOTE: while I wanted to supply my answer here on stackoverflow in this thread, I got a 'warning' from stackoverflow that there where already more than 30 answers. But he, to no avail for me. And because it's a thread that dates from 2012 and is still active in 2023 with so many answers (and cause's of the same problem). I decided to add my fifty cents here as well. Hope it helps and saves somebody's time!!
Upvotes: 0
Reputation: 995
You can run the maven command on the pom.xml file in your project directory:
mvn clean install
Upvotes: 4
Reputation: 203
for me deleting .idea file in your project directory worked fine! close the ide , delete the .idea in your project , open the project build and load !
Upvotes: 2
Reputation: 444
Tried everything, no luck. Finally, the simple solution that worked for me (on a Mac) was:
Upvotes: 1
Reputation: 1756
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.
Upvotes: 0
Reputation: 127
Goto File-> Invalidate Caches and Restart . Else delete rm -rf .idea *.iml and restart InteliJ
Upvotes: 2
Reputation: 313
Upvotes: 1
Reputation: 4088
Invalidating cache didn't work.
I edited the main class java file with a dummy change and ran it. It worked.
Upvotes: 3
Reputation: 131
If you are using gradle try rebuilding the project by the command from the command line:
gradle clean build
Upvotes: 0
Reputation: 495
After creating your project in intelliJ, try running the following command:
mvn package
Upvotes: 5
Reputation: 1577
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.
Upvotes: 1
Reputation: 385
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.
Upvotes: 3
Reputation: 4497
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.
Upvotes: 7
Reputation: 2191
I meet same problem and solved it by:
C:/Userz/xx/.IdeaIC2019.3
folderUpvotes: 0
Reputation: 69
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.
Upvotes: -1
Reputation: 767
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.
Upvotes: 13
Reputation: 141
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"
Upvotes: 1
Reputation: 167
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.
Upvotes: 1
Reputation: 31
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
Upvotes: 0
Reputation: 3275
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).
Upvotes: 6
Reputation: 970
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
Upvotes: 2