I know there are many duplicates of this question, but I have looked at them all, and none of them have solved the issue.
I am trying to run a class that has a main function. I have cleaned the project, checked the classpath for '.', added the bin folder to the classpath under run configurations. I'm not sure what else to try because the class is certainly in the source folder.
Could someone please help me with this issue?
package testIt;
public class MemoryVisualizerApp extends Application{
public static void main(String[] args) {
launch(args);
}
//Setup the scene and launch with given properties
@Override
public void start(Stage primaryStage) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("/MemoryVisualizer.fxml"));
Scene scene = new Scene(root, 650, 300);
//Set whether the screen should be re-sizable (possibly best size = default)
primaryStage.setResizable(true);
primaryStage.setMinHeight(300);
primaryStage.setMinWidth(550);
primaryStage.setTitle("Memory Usage");
primaryStage.setScene(scene);
scene.getStylesheets().add("testIt/MemoryVisualizer.css");
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
public void handle(WindowEvent e){
/*Currently the threads continue running after window is closed. Looking for
a way to stop the app threads when window closed without stopping the program.*/
}
});
primaryStage.show();
primaryStage.show();
}
}
This code is located within a package, withing the src folder. It utilizes some JavaFX files that arent shown but that shouldnt be an issue.
This is the error: Error: Could not find or load main class testIt.MemoryVisualizerApp
Well the following worked for me...
workspace
)bin
folderTry to point the classpathentry output path in the .classpath
file to target/classes
The classpath file is located in the project folder. Edit it manually.
In my case, it was like this before:
classpathentry kind="output" path="bin"
I changed it to:
classpathentry kind="output" path="target/classes"
Try also renaming the package before changing the configuration or reinstalling.
I got this weird error without having changed anything else than a few lines of code. Rebuilding did not work, Eclipse would not re-create the class even though the bin folder was empty. After renaming the package from test to test1 Eclipse started rebuilding and everything was fine.
This happened to me. I noticed that someone said I have to create an entire new WORKSHOP! Why? Because I installed a newer JRE version and that won't allow other previous versions to run on it. So all those old files I have become useless in a way. Not really, Just copy and paste it to new class and change it to an unused class name.
If all the solution does not work for you, it may be caused by missing libraries:
Build Path --> Configure Build Path --> Java Build Path --> Libraries
What I ended up doing is in Eclipse, copy the entire project and paste it again (and give it a different name of course). Then run your copied project. It should ask if you want it to run as a Java Applet or as a Java Application. Click on Java Application and it should work again. Then you can remove the original project and rename your current project to its original name if you wish.
I faced the similar problem, a workaround is to open up a terminal, go to project folder and run
java cp target/<your_jar_artifact.jar> <com.your_package.YourMainClass>
I have checked all possible solutions including re-installation of JDK, eclipse as it worked for many people but unfortunately it didn't work for me. Also updated main class in pom.xml but it didn't help. It took my sleep away. I was trying different options and suddenly the magic happened when I checked this checkbox. It might be helpful for many people so thought of sharing
I had the same problem. Spent few hours and finally changed my workspace back to local folder and everything works now. Maybe saves some time for others. Jon
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:
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.
In your Eclipse Preferences -> java -> Installed JREs -> Select the Java SE the jdk which you have on your machine and add the JDK
I was getting the same message and I solve the problem moving out two dependencies from my pom.xml called "org.slf4j". After that It worked perfectly!
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 had the same issue and I could not start any project with main()
methods.
I tried almost everything mentioned above, but it turned out that I had a little mistake in my pom.xml file.
You can check this file too!
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"/>
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.
The same error I am facing in my eclipse and going through above all did not help me out, So here are the steps you can do:
Hope this will help!
I had the same problem. In my case I did the following, to resolve it.
Observation: The classes directory in my case was not created, which means that the javac failed. On command line also, javac works the same way, if you have multiple java files in the same invocation of javac, and one java file encounters a problem, then no class files are created.
Which means Eclipse puts all the java files in the project with single invocation of javac.
Now if there are no class files generated, obviously, the message now makes sense. The compile errors are also shown in the "Problems" pane.
Now this would be the case if there is a compile error. If just a jar file which is not used at all, is on the classpath, there should not be any problem.
Even if my project had problems, I ignored it, because I was just testing a single class, and was thinking that only that file would get compiled and run, because it was a standalone test, without any dependencies.
But that is not the case with Eclipse. I wish they did it that way. As you can see lot of people got burnt this way.
Thanks
I had similar problem, my standalone java program was not working. I had other compilation error in my workspace in other unrelated java files. After removing all compilation errors I could run my standalone java program.
Try to check first in the Problems window the errors you have. It is most likely it. Then check in the Configure Build Path... window under Order and Export for errors in jars and libraries (missing, duplicates, etc.).
Fixing the problems will fix the class-loading issue.
I ran into the same error today, I refactored my project name and then refactored it back to the old name and it ran just fine after that.
I too faced this issue today. Whatever new class I created with main method i faced the same issue. Then I checked the build path and saw that one of the java api jar file - nashorn was showing as missing. I put it back in the ext folder and it worked!
I had this same problem with Eclipse-Mars. I had a working project then put it in my Git repository. That broke the execution (I guess because the actual Java files got moved to the git location and no longer existed in my workspace). I did a Maven>Update and it now works.
Project files using a custom archive in the build path, to fix -> go into the class path file, find and remove the entry then collect a new copy of the archive and add it to the build path again. You'll need to rebuild the project and to check the "run configurations" after.
Check that your project has a builder by either:
There must be other ways, but what I did was:
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>
Found This while searching, and it was after I updated my Java that the problem seemed to occur.
In Eclipse from your project:
Right-click on your project
Click Properties
Java build path: Libraries; Remove the "JRE System Library[J2SE 1.4]"
Click Add Library -> JRE System Library
Select the new "Execution Environment" or Workspace default JRE
Happened to me when I changed installed JREs to JDK7 instead of JRE7. Everything compiled but I couldn't run anything from Eclipse "eclipse-error-could-not-find-or-load-main-class"
Fix; going back to previous JRE7.
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
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.
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
.
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
I had the same problem after I created new package("tables") in my project.
I went to Window -> Show View -> General -> Error Log
and Ive read error:
JavaBuilder handling ImageBuilderInternalException while building: PizzaService
org.eclipse.core.internal.resources.ResourceException: Resource '/PizzaService/bin/tables' already exists.
as it turned out I had a text file in another source folder with the same name as this new package. So I've changed text file name from "Tables" to "Tabless" and I could run my project again.
Hope this helps.
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.
This just happened to me today after updating my JRE. I cleaned the project and it started working again.
Project -> Clean
will remove any existing class files and completely rebuild the project. There's more information on Eclipse's clean function here.
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
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 hope it is useful.
I recieved this error aswell, 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.
That did the trick for me, at least.
I was having the same problem. I was having a maven project. According to my knowledge, it was because i had cleaned the project and there were no class files generated. I did mvn compile and it was started running.
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.
I spent tenths of hours trying to solve this, and what only helped me at the end was to notice that I had in my packages some files (text files) with the same name, like TODO, README and IMPORTANT. I went through every package of my project renaming those files (like TODO_packagename1, TODO_packagename2...) and when finished, I closed Eclipse and restarted it again. Then worked!
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:
Start screen will be displayed.
After this my project compiled with success.
i had same problem but in my case firewall was blocking java directory path, make sure system firewall is allowing java directory path access.
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.
that's because you guys created the class one time with the main method & after that may be you have been deleted that form or workplace & still some of the files exist ,i will suggest you to create that form or workspace again & then delete it by clicking on it completely,then after that if you created the some class like Runner class try to run it again.
I too faced same problem.
Simple solution is : delete project( not source files please). close eclipse--> go to project folder-- > remove classpath and project file .
then open Eclipse and create project in same location
if your package name is same with your class name this problem will occur.
Same Problem occur with me.I went to Project > Properties > Java BuildPath.
There In order of export , I moved up my java/main to the top priority.
I deleted a jar file from the bin directory. Right click on your project - Properties then Libraries tab. There was a red flag in there. I removed the jar file from the Libraries and it worked.
Sometimes, it might have a very simple solution
You have perhaps added a new version of the existing .jar file and forgot to remove the old version of the same .jar file.
Now try removing the old jar files (that are marked in red) in the Build Path > Configure Build Path
If you are using maven Project...
First please clean your project. then installed maven- if build success then run your project.
then project will be working fine.
thanks
Rebuilding using the command line has fixed this issue for me multiple times. For example, running ant build
(for an Ant project) or mvn build
(for a Maven project) will fix eclipse once it succeeds.
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.
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.
Follow The Steps it Works for me : 1.Remove Configure build path from eclipse(Build Path) 2.Refresh 3.add configure Build Path->Source->add Folder->check src ok.
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.
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:
hope it helps!
I found other solution in my case this problem: Eclipse->Preferences->Java->Installed JRE
then press button Search. Select folder in Linux /usr
then Eclipse found all JVM.
Select another JVM too current. It is solved for my case.
If you are facing similar problem,it has to do with the "Run Configurations" not working properly in Eclipse.
Please right clik on your java class containing the main method .Go to Run As-> Run Configurations and put the correct patrameters.Project should be the Project which contains the main class and Main class should be fuly qualified class name.See the snapshots below for clarity.It should wok fine then.Basically,you are creating a Run configuration and you can use that saved configuration for later use also.
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.
I also had the same issue and was getting "Error: Could not find or load main class ...."
I solved it as follows:
Right clicked on my Java main class > Run As > Run Configurations.
In the resultant popup box and Under Java Application your java file name would be selected mode... if not select it
Under Bootstrap Entries Click Advanced Button > Select Add Library Radio and click OK. In next "Add Library" box Select JRE System Library and provide your correct JRE location and click Finish. (If it is not visible then u have to add it by clicking Installed JRE > Add button). Now you can see JRE System Library under BootStrap Entries
Next User Entries > Click Add Projects Button > And then under Project Selection box check on your Project name > OK
The main java file should now work. Hope this may help someone who would be facing similar issues.
I was running through the similar problems in my small java snippet
Here is what I did.
I copied all the content in the Main class.
Deleted the Main class Greeter.
Right click -> Created a new Java class with name Greeter
a> checked include main method when creating class
voila !!! I was able to run as Java application by right clicking on the main class Greeter.java.
Just out of curiosity I again tried deleting the Greeter class and creating new class without checking main method, and it did not show me run as Java application option in right click.
you should add bin folder - run->run Configuration tab classpath advanced button - add folder - choose bin folder (yourproyect/bin)
I did all the things mentioned above, but none of them worked for me
My problem resolved as follows:
I came across the same problem. You might get this error in many situations like below.
mvn clean
, mvn build
and other options.Here, the list of hit and trial goes on. Believe me the tool Eclipse is not that stupid. It is quite smart to find errors even at compile time. I tried all ways mentioned above, but none worked for me.
The main problem which worked for me was JRE version which was somehow got set to default (JRE System Library [jre1.5]). I reset it to jre1.8.* which I had installed in my machine.
I don't commit that this is only the solution to this problem but yes this could be one of the solution. So keep an eye on JRE version.
I was using eclipse oxygen and the problem persists everytime. Later i found that if your Eclipse workspace is under the directory with non ascii characters then somehow javaw.exe couldn't reach the bin directory. Suppose your directory name is Work_Hello the remove underscore sign and clean the project and run configurations. This worked for me. Reference: https://www.eclipse.org/forums/index.php/t/1093209/
I faced the same error, the error was in one of the imported external jars. After removing that jar project worked for me.
Rename the class with Ctrl+Shift+R (Windows), it will change name of the class everywhere where it is referred, and then do the opposite procedure. In my case it was caused by identical class names in several projects within the environment and Eclipse had hard time to understand which is which.
Actually I found the real Root Cause for this issue, by going to add each .jar
files manually one by one, because the following answer within the same thread, suggests you to do so.
Cause:
You know as we are humans, we are really lazy creatures and that's why we have an option to "Select All" and also shortcut keys to perform that.
So when I have to add around 30+ jar files for a PDF generation required app, I did go in this way,
Right Click on Project -> Properties -> Java Build Path
So you have two options:
Select only the JAR files wherever you want and don't be too lazy to add text and other types of files. or,
Select all files within a specific folder where you wanna add JARs from, and then remove one by one, which are non-JAR files.
Note:
If you have added any of non-JAR files to the project in that way as you were trying to add "libraries", it will show you something like the following:
and keep trying to compile and at the end, it will give you the following error:
Error: Could not find or load the main class er.waterapp.some.MainFileGivenByConfigaration
So this is it!!! Hope I may have had the chance to help some one out there and also save him a lot of time. :-)
Cheers,
Randika
I just encountered the same problem as yours and I followed Kumar's directions and it worked out. One thing to add: you need to make sure that after you go to the correct main class, check the "Classpath" and your project name should be existed under the "User Entries". If not, add your project to the directory then you're good to go.
I had a very strange reason for this error. Some of the files referenced in the local Maven repository (~/.m2/...) were corrupt. After deleting these and then rebuilding the application, it worked. Using a newer version of Maven exposed the corrupt files.
It seems that the class is not compiled by Eclipse.
Few pointers could be-
tl;dr: Clean your entire Build Path and everything you ever added to it manually. This includes additional sources, Projects, Libraries.
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
I followed several of the less drastic suggestion here to no avail. Then it dawned on me that Eclipse was confused because I had made a backup copy of my src project folder. (It was compiling all these "copy of" folders' *.java files and I noticed the subfolders for them in my bin folder.) I moved the backup elsewhere, got rid of the extraneous class files, did a Project > Clean, and everything was back to normal.
A very simple fix coul’d be delete a commented error in the pom.xml.
I don’t now why, but the error coul’d persist in the pom, even if you comment it. In my case, it was an innecesary scope tag for mockit or whole dependency of mockit, all commented!!! It was sufficent delete the commented lines and save the pom.
Usually cleaning and rebuilding the project from Eclipse does the trick.
However, when using Maven I have discovered times this does not work and I've noticed the 'target' folder of my maven project does not have anything under it. Running 'mvn clean install' from the command line and refreshing the project in eclipse does the trick.
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.
Removing the JRE System Library and adding the default one worked for me.
I just ran into that problem. The cause... not sure. However, only happened after I added a new jvm. My solution:
In the Classpath tab:
I hope it helps someone out there. It took me time to figure this out.
Had the same problem, struggling with it for a while: couldn't find main class.
I did Project -> Clean -> Clean all Projects
Cleaned everything, and tada, it compiled!
Follow these steps :
1)Delete your current run configuration. 2)Go to the main class and run it by right clicking the mouse-->Run As-->Java Application.
Then you will see an auto generated new run configuration.
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.
"Run As" -> "Java Application"
Eclipse will write a new configuration for the moved class, and the error will go away.
I was also getting same message.That is due to the workspace problem.Create one new workspace and try there once. I think it will works.
These are the simple steps, which helped me to solve this problem.
Source: Stackoverflow.com