IntelliJ: running the main method on mac OS Yosemite - java

I recently got myself a mac and I'm really confused at why my IntelliJ does not seem to have an option to "Run" the main method that is sitting in my Application.java. I'm just trying to run a basic example.
On Windows, I normally I just right click on the Application.java file and select "Run Application.main()" and then it runs but on my mac version of IntelliJ there is no such option:
Then I try to set the config by clicking "Edit Configuration":
And then I tried to set my Main class as "Application.java" but it does not let me click "OK".
I'm using java 1.8 to compile:

It seems your project is not correctly imported,
and IntelliJ doesn't treat your source code as source code.
In your first screenshot, in the view of the project's directory,
I see that all these directories have the same brown color:
java
com
futureprocessing
...
When a project is correctly imported,
the java directory should look green,
and the sub-directories under should be shown in dotted notation,
so for example com.futureprocessing....
It looks like your project is a Gradle project.
Try to right-click on build.gradle and select reimport.
Or you might reimport from scratch.
This assumes that build.gradle is correctly written,
and that you are able to build your project on the command line.
Only in that case, IntelliJ should be able to import or re-import it correctly.
If importing/reimporting doesn't work reasonably (though it really should),
then you can try the tips in the answer and mark the source and resources directories manually to be treated as such.

Your java directory is not marked as a Sources root. Right click the java directory in project view and select Mark As/Sources Root.
Similarly the resources directory is not marked as Resources root. You can mark it as such the same way.
These problems are probably caused by incorrectly importing the project and so on. So other solution might be to try to import the project again if it doesn't work.

Related

main Class won't load on a different computer [duplicate]

I have a project in eclipse on my laptop that I pushed to Git https://github.com/chrisbramm/LastFM-History-Graph.git
It works fully on my laptop and runs/builds without a problem but on my desktop it doesn't Eclipse gives the error
Error: Could not find or load the main class lastfmhistoryguis.InputPanel
I've tried building the project from:
Project>Build Project
But nothing happened. I've set the PATH variables on this computer to JRE6, JRE7 and JDK 1.7.0 even though these aren't set on my laptop.
I did have Jar file (last.fm-bindings-0.1.1.jar) that was in my .classpath file that was in C:\Users\Chris\Downloads folder on my laptop hence it wasn't included in the git tree which I recently brought into the project folder and committed ,but I'm not sure whether I have done it right.
Would this also be causing a problem but there isn't a main argument in there.
I can't work out now, what I need to check/change.
If you create a java class with public static void main(String[] args), Eclipse will run that main method for you by right clicking on the file itself, or on the file in the project explorer, then choosing:
"Run As" -> "Java Application."
Once you do this, Eclipse stores information about your class, so you can easily run the class again from the Run As menu (Green Play Button on the toolbar) or from the Run Configurations dialog.
If you subsequently MOVE the java class (manually, or however), then again choose
"Run As" -> "Java Application,"
from the new location, Eclipse will run the original stored configuration, attempt to invoke this class from its original location, which causes this error.
SOLUTION:
For me, the fix was to go to the run configurations, (Green Play Button -> Run Configurations) and remove all references to the class. The next time you run
"Run As" -> "Java Application"
Eclipse will write a new configuration for the moved class, and the error will go away.
tl;dr:
Clean your entire Build Path and everything you ever added to it manually. This includes additional sources, Projects, Libraries.
Project -> Clean
Make sure Project -> Build automatically is active
Project -> Properties -> Java Build Path -> Libraries: Remove any external libs you have ever added. Don't remove standard libraries like the JRE System Library.
Try to run your main class now. The "class could not be found / load" error should be gone. Try adding your external libs/jars one after each other.
Reason behind this: The compiler had issues linking the libraries to the project. It failed and produced a wrong error message.
In my case, it should have been something like "Could not add AutoHotkey.dll to the build path" because that was what made the compiler fail.
If this is still not working, have a look at the built-in ErrorLog of Eclipse:
Window -> Show View -> General -> Error Log
In your classpath you're using an absolute path but you've moved the project onto a new machine with quite possibly a different file structure.
In your classpath you should therefore (and probably in general if you're gonna bundle JARS with your project), use relative pathing:
In your .classpath
change
<classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/><classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/>
to
<classpathentry kind="lib" path="last.fm-bindings-0.1.1.jar"/>
I did all the things mentioned above, but none of them worked for me
My problem resolved as follows:
Right click on your Project > Properties > JavaBuildPath > Libraries.
Remove the jar file, having a red flag on it.
If problem persists try the solution below. This worked for me when I faced this problem second time:
Right-Click Project > Properties > Java Build Path > Libraries
Remove Library
Add Library. (Choose the JRE System Library )
I faced similar problem in my maven webapp project after spending nearly one hour , I found a solution which worked for me .I typed the following maven command and It worked
mvn clean install -U
I dont know the exact reason behind it.
I am assuming that you had imported the project into your desktop eclipse installation? If that is the case, you should just select Project > Clean. Then rebuild your project. Worked like a charm for me.
VERY CAREFUL: This will unbind your project from the workspace. You'll have to import all your projects again
I had the same issue and solved it using:
Eclipse Mars
Egit
Github
Maven Project
The Problem was that i made my maven project available to github. It moved my project to my github folder.
Solution:
Close Eclipse
Delete the metadata folder inside your workspace
Restart Eclipse
Start screen will be displayed.
Close the start screen
Rightclick into package explorer
Chose "import maven project",
Navigate to your github folder and import the maven project.
After this my project compiled with success.
Check that your project has a builder by either:
check project properties (in the "package explorer", right click on the project, select "properties"), there the second section is "Builders", and it should hold the default "Java Builder"
or look in the ".project" file (in .../workspace/yourProjectName/.project) the section "buildSpec" should not be empty.
There must be other ways, but what I did was:
shut down eclipse
edit the ."project" file to add the "buildSpec" section
restart eclipse
A proper minimal java ".project" file should look like:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>myProjectName</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Well the following worked for me...
Went into the project folder (inside workspace)
Then, deleted the bin folder
Then, Cleaned project / projects (in Eclipse)
built/run from Eclipse.
If your code is good and you know you're having an Eclipse problem, this will solve it.
You could simply delete $yourproject/.classpath , $yourproject/.project , and $yourworkspace/.metadata. Someone else mentioned this option. It will blow up your entire workspace though. Instead:
Delete .classpath and .project from your project
Delete your project in eclipse. DO NOT check delete project
contents on disk.
Now, in a file explorer, go into $yourworkspace/.metadata.
Search for $yourprojectname
Delete everything you find. It should be safe-ish to delete anything in the .metadata directory.
In eclipse: File > Import > General > Projects from Folder or Archive > $yourproject > finish
Right click your project > properties > Java Build Path > Source tab
Select all source folders, remove.
Add folder, select src (whatever your src folder is called) and add it
Go to libraries tab
Add any jars to your build path here. There should be no more errors on your project now.
Run your project like you normally would.
Similar thing happened to me few times, the only way I knew to fix this was to remove the metadata folder. Fortunately I have found another way.
Try going to project properties > Java Build Path > Order And Export tab > select all (or try to play with check boxes there).
This should cause complete project rebuild and Eclipse to see main class.
Addition: I have noticed that this bug occurs when you have many projects in a work space and some of them is configured wrong(red exclamation mark appears). Fixing project build path and other settings(even if this project is not related to the one you have problems with) should fix an issue.
For me, the reason that this error started showing up was due to classpath getting over the limit on windows. Then I discovered the option "Use temporary JAR to specify classpath (to avoid classpath length limitations)". Selecting this option fixed the problem for me. The option resides in Run/Debug Configuration, Classpath tab, see the image below.
My Main class could not be found or loaded problem is caused by an interesting reason.
In our project, we are using Maven as build tool and my main class extends a class, which is on the class path but its scope was test, while the main class is not under the test package.
If your main class extends a class, first try to run your main class by removing extends part. If it runs, you will at least understand that the problem is not because of run configuration or eclipse but the class, your main class extends.
this could cause of jdk libraries if you had imported into jre
this happen to me , so check installed jre jars
in eclipse click on Windows > Preferences > Java > Installed Jres > click on Jre and edit after that look into jar list make sure none is of jdk or corrupted ,
I had the same problem with correct .classpath file, and soon found actually it's not the .classpath file counted (after I fixed this issue, I replace the workable .classpath file with the original one, the project still worked, which means the .classpath file was not the case)
Since it's a Maven project, all I did is:
mvn eclipse:clean
delete eclipse project
import the project
done
This problem is also caused when you have special characters in your workspace path. I had my workspace in my personal folder and its name was in Greek, so it didn't work. I changed my workspace, now contains only english characters in its path, and now the project is built without any problems.
If You are using eclipse then the following steps will solve your problem:
Go to Run -> Run Configurations -> Main Class Search -> Locate your class manually -> Apply -> Run
I had this error. It was because I had
static void main(String[] args)
instead of
public static void main(String[] args)
I spent nearly an hour trying to figure that out.
Note: The only difference is that I didn't declare main to be public
To solve this error do the following steps:
Step 1: Open the .project file.
Step 2: Check two tags...
a) <buildSpec>
b) <natures>
Step 3: If the above-mentioned tags do not include any content then surely the above error is going to occur.
Step 4: Add the following content to each tag in order to resolve the above error.
For <buildSpec> :
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
For <natures> :
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
Step 5: Save your file by hitting ctrl + s.
Step 6: For safe side just close the project once and reopen it.
Step 7: That's it. You are ready to rock!!!
Please mention in comments if the solution was helpful.
Thank you.
I run into the same problem, but in my case it was caused by missing (empty) source folder (it exists in original project, but not in GIT repository because it's empty).
After creating the missing folder everything works.
I ran into this error today because I set up a hello world program and then cut and pasted a new program into the same file. To fix the problem of not finding hello world as the last was called I clicked Run-> Run Configurations and then under Main Class I clicked search and it found my new class name and replaced it with the correct new name in the text that I pasted. This is a newbie problem I know but it is also easy to fix. I hope this helps someone!
Douglas
Mostly this happens, because Eclipse cleans the .class files, but don't build them again. Check the bin folder, it should be empty. Then you should check, is there anything else, which is causing build ti fail. You might have added some jars in classpath, which Eclipse might not be able to find.
Just go to your Package Explorer and press F5, or for some laptops fn+F5. The reason is that eclipse thinks that the files are somewhere, but the files are actually somewhere else. By refreshing it, you put them both on the same page. Don't worry, you won't lose anything, but if you want to be extra careful, just back up the files from your java projects folder to somewhere safe.
These are the simple steps, which helped me to solve this problem.
Close the eclipse
Delete ".metadata" folder in your work-space. (may be hidden folder)
Open the eclipse (it will automatically create ".metadata" folder in your work- space)
Try to run the program.
Check the workspace error log (Windows-> Show View -> Error log). If you see any of the jar's imported is corrupted, remove the corresponding repository folder and re-import again.
2 types of solutions exits for the same.
(1) Go to run configurations: - run->run configurations
In the Classpath tab:
Select Advanced
Add where Eclipse usually put the *.class for the projects, which is in bin. So I added the bin directory for the project.
(2) If first solution is not working then it means the jar you are pointing out to your project is taking the path of your local Maven repo which is not getting updated to your project so better you check the jar from that local maven repo and copy it paste it into new project simply or just download it from any site and configure it into your build path.
I hope it helps.
This problem occurred for me when I deleted a ".jar" file from Eclipse project by right clicking on it and hitting "delete" (not from "Build path").
To solve, I tried the following steps:
1- Right click on the project
2- Build Path --> Configure Build Path
3- Libraries
4- Delete the jar file from there (I see a red mark beside it).
I received this error as well, just after moving some resources. Checking the error log, I saw that Eclipse couldn't make a build since it couldn't remove a file/folder. Try manually removing the "bin" (or whatever it's called for you) folder.
Can be resolved by updating your project.
For example if you use maven and eclipse as you mentioned, do those steps:
Right click on your project
Click on Maven
Click on Update Project...
Click Ok
I just had this problem after first having the problem of Windows 8 refusing to update my path no matter what I set JAVA_HOME to - java -version reported the last JDK instead of the one I stored in JAVA_HOME. I finally got that to work by putting '%JAVA_HOME%/bin;' at the front of the path environment variable instead of at the end. Then I launched Eclipse and all the sudden it could not find my main class when it worked fine before this. What I did to fix it was went into the project properties, removed the existing JRE library from the libraries tab, added a new JRE by selecting the "Add Library" button and then followed the prompts to install JRE 7 as my default JRE. Now all is back to working.

Compile errors everywhere after workspace move

I'm making a webapp in Eclipse. I use to make it in my Desktop. And today I got a laptop and I've copied the workspace in my Desktop to laptop.
And after copying the workspace, I've opened my Eclipse pointing to the copied workspace. And when I opened my Java files, almost every line is red underlined .
In my desktop there was no problem with this app, It used to work fine.
When I started my Eclipse with this copied path, I though the errors were due to the jar files (I used to have jars in my desktop associated to this project).
Even after configuring my Build path, the red lines still appear.
How can I solve this?
A wild guess: The path to the JRE is wrong so the compiler does not find the Java standard library. When none of the standard library classes are found you get compile errors at almost every line.
Solution:
Configure the path to the JRE in the compiler settings:
Preferences → Java → Installed JREs
And/or configure the path to the standard library in the project settings:
Project settings → Java Build Path → Libraries
Edit existing entry for system library or add a new one.
You don't want to just copy the workspace over. You want to import the project into Eclipse. You will also want to make sure that you import, and add, any external .jar files. To import use:
File > Import > Existing Project into Workspace
Did you use the import wizard after setting your workspace (http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-importproject.htm)? This is the important part. Once that's done be sure to do a clean build. If you see errors then, you might still be missing external jars that you had on your desktop.

Eclipse won't compile/run java file

I am just trying to compile and run a simple java program. When I go to run my tester class it says select what to run and it gives me Ant Build which when highlighted says "Launches an Ant build with default settings" or Ant Build... that says "Launches an Ant build and allows it to be configured". When I try to select either of these it prompts Build failed. Reason: Unable to find ant file to run. I honestly don't know what these ant builds and files are. This is definitely a dumb question but have no idea what to do.
Make a project to put the files in.
File -> New -> Java Project
Make note of where that project was created (where your "workspace" is)
Move your java files into the src folder which is immediately inside the project's folder.
Find the project INSIDE Eclipse's Package Explorer (Window -> Show View -> Package Explorer)
Double-click on the project, then double-click on the 'src' folder, and finally double-click on one of the java files inside the 'src' folder (they should look familiar!)
Now you can run the files as expected.
Note the hollow 'J' in the image. That indicates that the file is not part of a project.
I was also in the same problem, check your build path in eclipse by Right Click on Project > build path > configure build path
Now check for Excluded Files, it should not have your file specified there by any means or by regex.
Cheers!
right click somewhere on the file or in project explorer and choose 'run as'->'java application'
This worked for me:
Create a new project
Create a class in it
Add erroneous code, let error come
Now go to your project
Go to Problems window
Double click on a error
It starts showing compilation errors in the code.
Your project has to have a builder set for it. If there is not one Eclipse will default to Ant. Which you can use you have to create an Ant build file, which you can Google how to do. It is rather involved though. This is not required to run locally in Eclipse though. If your class is run-able. It looks like yours is, but we can not see all of it.
If you look at your project build path do you have an output folder selected? If you check that folder have the compiled class files been put there? If not the something is not set in Eclpise for it to know to compile. You might check to see if auto build is set for your project.
What I did was I created a new project and opened the file location from my old project and new one. Then I copied everything from the old project besides the run file, and ran it on the new project and now it works fine.
the file is probably located in your project package but is not yet compiled so you are able to open it as a java file in your eclipse java project, here is what I did in my case: in eclipse, goto terminal window, cd to your project directory, then cd to your project package, then run javac yourFileName.java, finally right click your java project or project package in the project explorer and... you should see the file there, and it should now run for you as a java file... hope this helps!!!

IntelliJ - How to jump to source instead of compiled classes from failed unit tests in the "Run" view

When I run my JUnit tests in IntelliJ and one of them fails, I would like to jump to the source code of the failing test by double-clicking it (or by right-clicking it and selecting "Jump to source" or "Show source") in the results view (in the list of failed tests in the "Run" view). However, this feature doesn't always work correctly. Sometimes the source code is shown and sometimes only the compiled ".class" file of the test is shown to me in the editor window.
I would always like to see the source code (".java" file) instead of the ".class" file in the editor window after double-clicking the failed test.
One solution for it is to click "Attach Sources..." in the editor window, where the ".class" file of the test class is displayed. However, I have to do it for every test file and it takes much time to do it repeatedly for every test. In my opinion IntelliJ should find the source code automatically, because it's present in my Project (it's my own class and its source code is in the project and I run this test from the Project view, where the classes with source code are visible). How can I configure IntelliJ, so that it finds the source code automatically for every unit test which I run?
The ".class" files which are shown instead of the ".java" files are contained in a jar file which is built by Maven.
You should be able to go to the External Libraries in your Project view (File > Project Structure), find the jar that contains the .class file, and right click (or F4). You will see Open Library Settings. From that dialog, you can attach a src folder to the library.
I'm using IntelliJ IDEA 2019.3 ultimate edition and faced this issue, debugger would jump to decompiled .class files
On the top right corner once your debugger jumps to .class file
you will see an option to 'choose sources', choose the directory which has your .java file. Worked for me atleast.
I had a similar issue. In my case it was due to incorrect Module configuration. Basically you must ensure that the source folder containing the java files is added to Sources tab in corresponding module.
To do this Go to Project Structure (Ctrl + Alt + Shift + S) and select Modules. Choose corresponding module and verify that the source folder containing the java files is correctly added to the Sources Folders. If it is not then add it.
I got this issue today. The solution for me was to right click on main pom.xml, then maven, then reimport.
I Had a similar issue. The issue was still there even after pointing to the correct source folder but I got rid of it by setting the classes folder to the source code's root folder.
Add a solution to JAVA Maven project.
Open 'Libraries' from 'Project settings', search and select the Java jar you want to attach source file. Then you can add source directory by click the '+' button.
I Had a similar issue. The solution for me was to close project, delete .idea folder than re open the project.

Eclipse "Error: Could not find or load main class"

I have a project in eclipse on my laptop that I pushed to Git https://github.com/chrisbramm/LastFM-History-Graph.git
It works fully on my laptop and runs/builds without a problem but on my desktop it doesn't Eclipse gives the error
Error: Could not find or load the main class lastfmhistoryguis.InputPanel
I've tried building the project from:
Project>Build Project
But nothing happened. I've set the PATH variables on this computer to JRE6, JRE7 and JDK 1.7.0 even though these aren't set on my laptop.
I did have Jar file (last.fm-bindings-0.1.1.jar) that was in my .classpath file that was in C:\Users\Chris\Downloads folder on my laptop hence it wasn't included in the git tree which I recently brought into the project folder and committed ,but I'm not sure whether I have done it right.
Would this also be causing a problem but there isn't a main argument in there.
I can't work out now, what I need to check/change.
If you create a java class with public static void main(String[] args), Eclipse will run that main method for you by right clicking on the file itself, or on the file in the project explorer, then choosing:
"Run As" -> "Java Application."
Once you do this, Eclipse stores information about your class, so you can easily run the class again from the Run As menu (Green Play Button on the toolbar) or from the Run Configurations dialog.
If you subsequently MOVE the java class (manually, or however), then again choose
"Run As" -> "Java Application,"
from the new location, Eclipse will run the original stored configuration, attempt to invoke this class from its original location, which causes this error.
SOLUTION:
For me, the fix was to go to the run configurations, (Green Play Button -> Run Configurations) and remove all references to the class. The next time you run
"Run As" -> "Java Application"
Eclipse will write a new configuration for the moved class, and the error will go away.
tl;dr:
Clean your entire Build Path and everything you ever added to it manually. This includes additional sources, Projects, Libraries.
Project -> Clean
Make sure Project -> Build automatically is active
Project -> Properties -> Java Build Path -> Libraries: Remove any external libs you have ever added. Don't remove standard libraries like the JRE System Library.
Try to run your main class now. The "class could not be found / load" error should be gone. Try adding your external libs/jars one after each other.
Reason behind this: The compiler had issues linking the libraries to the project. It failed and produced a wrong error message.
In my case, it should have been something like "Could not add AutoHotkey.dll to the build path" because that was what made the compiler fail.
If this is still not working, have a look at the built-in ErrorLog of Eclipse:
Window -> Show View -> General -> Error Log
In your classpath you're using an absolute path but you've moved the project onto a new machine with quite possibly a different file structure.
In your classpath you should therefore (and probably in general if you're gonna bundle JARS with your project), use relative pathing:
In your .classpath
change
<classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/><classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/>
to
<classpathentry kind="lib" path="last.fm-bindings-0.1.1.jar"/>
I did all the things mentioned above, but none of them worked for me
My problem resolved as follows:
Right click on your Project > Properties > JavaBuildPath > Libraries.
Remove the jar file, having a red flag on it.
If problem persists try the solution below. This worked for me when I faced this problem second time:
Right-Click Project > Properties > Java Build Path > Libraries
Remove Library
Add Library. (Choose the JRE System Library )
I faced similar problem in my maven webapp project after spending nearly one hour , I found a solution which worked for me .I typed the following maven command and It worked
mvn clean install -U
I dont know the exact reason behind it.
I am assuming that you had imported the project into your desktop eclipse installation? If that is the case, you should just select Project > Clean. Then rebuild your project. Worked like a charm for me.
VERY CAREFUL: This will unbind your project from the workspace. You'll have to import all your projects again
I had the same issue and solved it using:
Eclipse Mars
Egit
Github
Maven Project
The Problem was that i made my maven project available to github. It moved my project to my github folder.
Solution:
Close Eclipse
Delete the metadata folder inside your workspace
Restart Eclipse
Start screen will be displayed.
Close the start screen
Rightclick into package explorer
Chose "import maven project",
Navigate to your github folder and import the maven project.
After this my project compiled with success.
Check that your project has a builder by either:
check project properties (in the "package explorer", right click on the project, select "properties"), there the second section is "Builders", and it should hold the default "Java Builder"
or look in the ".project" file (in .../workspace/yourProjectName/.project) the section "buildSpec" should not be empty.
There must be other ways, but what I did was:
shut down eclipse
edit the ."project" file to add the "buildSpec" section
restart eclipse
A proper minimal java ".project" file should look like:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>myProjectName</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Well the following worked for me...
Went into the project folder (inside workspace)
Then, deleted the bin folder
Then, Cleaned project / projects (in Eclipse)
built/run from Eclipse.
If your code is good and you know you're having an Eclipse problem, this will solve it.
You could simply delete $yourproject/.classpath , $yourproject/.project , and $yourworkspace/.metadata. Someone else mentioned this option. It will blow up your entire workspace though. Instead:
Delete .classpath and .project from your project
Delete your project in eclipse. DO NOT check delete project
contents on disk.
Now, in a file explorer, go into $yourworkspace/.metadata.
Search for $yourprojectname
Delete everything you find. It should be safe-ish to delete anything in the .metadata directory.
In eclipse: File > Import > General > Projects from Folder or Archive > $yourproject > finish
Right click your project > properties > Java Build Path > Source tab
Select all source folders, remove.
Add folder, select src (whatever your src folder is called) and add it
Go to libraries tab
Add any jars to your build path here. There should be no more errors on your project now.
Run your project like you normally would.
Similar thing happened to me few times, the only way I knew to fix this was to remove the metadata folder. Fortunately I have found another way.
Try going to project properties > Java Build Path > Order And Export tab > select all (or try to play with check boxes there).
This should cause complete project rebuild and Eclipse to see main class.
Addition: I have noticed that this bug occurs when you have many projects in a work space and some of them is configured wrong(red exclamation mark appears). Fixing project build path and other settings(even if this project is not related to the one you have problems with) should fix an issue.
For me, the reason that this error started showing up was due to classpath getting over the limit on windows. Then I discovered the option "Use temporary JAR to specify classpath (to avoid classpath length limitations)". Selecting this option fixed the problem for me. The option resides in Run/Debug Configuration, Classpath tab, see the image below.
My Main class could not be found or loaded problem is caused by an interesting reason.
In our project, we are using Maven as build tool and my main class extends a class, which is on the class path but its scope was test, while the main class is not under the test package.
If your main class extends a class, first try to run your main class by removing extends part. If it runs, you will at least understand that the problem is not because of run configuration or eclipse but the class, your main class extends.
this could cause of jdk libraries if you had imported into jre
this happen to me , so check installed jre jars
in eclipse click on Windows > Preferences > Java > Installed Jres > click on Jre and edit after that look into jar list make sure none is of jdk or corrupted ,
I had the same problem with correct .classpath file, and soon found actually it's not the .classpath file counted (after I fixed this issue, I replace the workable .classpath file with the original one, the project still worked, which means the .classpath file was not the case)
Since it's a Maven project, all I did is:
mvn eclipse:clean
delete eclipse project
import the project
done
This problem is also caused when you have special characters in your workspace path. I had my workspace in my personal folder and its name was in Greek, so it didn't work. I changed my workspace, now contains only english characters in its path, and now the project is built without any problems.
If You are using eclipse then the following steps will solve your problem:
Go to Run -> Run Configurations -> Main Class Search -> Locate your class manually -> Apply -> Run
I had this error. It was because I had
static void main(String[] args)
instead of
public static void main(String[] args)
I spent nearly an hour trying to figure that out.
Note: The only difference is that I didn't declare main to be public
To solve this error do the following steps:
Step 1: Open the .project file.
Step 2: Check two tags...
a) <buildSpec>
b) <natures>
Step 3: If the above-mentioned tags do not include any content then surely the above error is going to occur.
Step 4: Add the following content to each tag in order to resolve the above error.
For <buildSpec> :
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
For <natures> :
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
Step 5: Save your file by hitting ctrl + s.
Step 6: For safe side just close the project once and reopen it.
Step 7: That's it. You are ready to rock!!!
Please mention in comments if the solution was helpful.
Thank you.
I run into the same problem, but in my case it was caused by missing (empty) source folder (it exists in original project, but not in GIT repository because it's empty).
After creating the missing folder everything works.
I ran into this error today because I set up a hello world program and then cut and pasted a new program into the same file. To fix the problem of not finding hello world as the last was called I clicked Run-> Run Configurations and then under Main Class I clicked search and it found my new class name and replaced it with the correct new name in the text that I pasted. This is a newbie problem I know but it is also easy to fix. I hope this helps someone!
Douglas
Mostly this happens, because Eclipse cleans the .class files, but don't build them again. Check the bin folder, it should be empty. Then you should check, is there anything else, which is causing build ti fail. You might have added some jars in classpath, which Eclipse might not be able to find.
Just go to your Package Explorer and press F5, or for some laptops fn+F5. The reason is that eclipse thinks that the files are somewhere, but the files are actually somewhere else. By refreshing it, you put them both on the same page. Don't worry, you won't lose anything, but if you want to be extra careful, just back up the files from your java projects folder to somewhere safe.
These are the simple steps, which helped me to solve this problem.
Close the eclipse
Delete ".metadata" folder in your work-space. (may be hidden folder)
Open the eclipse (it will automatically create ".metadata" folder in your work- space)
Try to run the program.
Check the workspace error log (Windows-> Show View -> Error log). If you see any of the jar's imported is corrupted, remove the corresponding repository folder and re-import again.
2 types of solutions exits for the same.
(1) Go to run configurations: - run->run configurations
In the Classpath tab:
Select Advanced
Add where Eclipse usually put the *.class for the projects, which is in bin. So I added the bin directory for the project.
(2) If first solution is not working then it means the jar you are pointing out to your project is taking the path of your local Maven repo which is not getting updated to your project so better you check the jar from that local maven repo and copy it paste it into new project simply or just download it from any site and configure it into your build path.
I hope it helps.
This problem occurred for me when I deleted a ".jar" file from Eclipse project by right clicking on it and hitting "delete" (not from "Build path").
To solve, I tried the following steps:
1- Right click on the project
2- Build Path --> Configure Build Path
3- Libraries
4- Delete the jar file from there (I see a red mark beside it).
I received this error as well, just after moving some resources. Checking the error log, I saw that Eclipse couldn't make a build since it couldn't remove a file/folder. Try manually removing the "bin" (or whatever it's called for you) folder.
Can be resolved by updating your project.
For example if you use maven and eclipse as you mentioned, do those steps:
Right click on your project
Click on Maven
Click on Update Project...
Click Ok
I just had this problem after first having the problem of Windows 8 refusing to update my path no matter what I set JAVA_HOME to - java -version reported the last JDK instead of the one I stored in JAVA_HOME. I finally got that to work by putting '%JAVA_HOME%/bin;' at the front of the path environment variable instead of at the end. Then I launched Eclipse and all the sudden it could not find my main class when it worked fine before this. What I did to fix it was went into the project properties, removed the existing JRE library from the libraries tab, added a new JRE by selecting the "Add Library" button and then followed the prompts to install JRE 7 as my default JRE. Now all is back to working.

Categories