'Eclipse is executing the wrong Java file
I am using Eclipse to write and test Java. I am a beginner so i don't know anything about Eclipse.
The problem occurs when I try to run the Java file I just wrote. Instead of executing the file that is opened, it executes the file that I have successfully ran before. I have a few files in the same default package. The package explorer shows that the location of my package is: Aayush > src > default package
I want to run a file named logicaloperator.java but it runs ifstatement.java both are in the same default package and I use the 6th button on the toolbar to run it. When I hover over the run button it says : "run ifstatement" but it should be saying "run logicaloperator".
Solution 1:[1]
If you want to run a particular java file which contains the main(String[] args) method.
Right click on the file -> RunAs - > Java Application.
Solution 2:[2]
Check to see that your main() isn't capitalised.
Eclipse won't catch it, but if you run public static void Main(String[] args), it will run the wrong program.
Solution 3:[3]
The answer has been posted before, but I highly recommend to work as clean as possible. The CamelCase for classes has just been mentioned, I personally wouldn´t put all the classes in one package. Use more projects for it, or use different packages. If you ever want to do big project with Java, this is one of the most important things (not to mention, that you can work with protected stuff in different packages in one project). Also use the autogeneration for classes. You can prevent mistakes, like your did it just now, with it. Eclipse is a powerful tool, use it!
Best regards
Solution 4:[4]
I recently had this kind of problem too, however finally I could find the culprit. Since as I saw, nobody had discussed about this thing, I will tell.
I have checked things like the class name, main() method, but still the warning appear and said "no main method found", something like that.
I just overlooked that for the main() method, inside the bracket I forgot to include square brackets for the String, so my main method was
... main(String args)
where you can see that I missed that square brackets.
Therefore after that I fix that into ... main (String[] args) and that ended my problem.
Solution 5:[5]
I had this same problem in Netbeans a while back. Your code is fine. You can just right-click somewhere in the file's window and select "Run File". OR if you go to the "Run" tab in the taskbar you can "Set the project configuration", "Customize" .... once the project properties dialog box opens check your entry in the "Main Class Field" (it likely has the problematic class in there, replace it and enter the name of the class you want to run). However, you should probably just right-click so you don't have to do this everytime you create a new class in the package.
Solution 6:[6]
You can see a small triangle near the Run's button in the Eclipse. If you click on that, you can see all of your projects are opening. Now if you can seelogicaloperator.java in this list, you can click on it and then run your code, else you should check your main() method in logicaloperator.java.
Solution 7:[7]
I had the same prob. just make sure to have the main and all ure classes on public...
Solution 8:[8]
My solution : to save the file before running it. If you want the file will be saved automatically you can configure that. go to the menu: Window->Preferences-> General->Editors->Autosave , Check the 'Enable autosave...' , then put in the text-box how many second till the auto saving. if you put 3, for example , then it will be saved 3 seconds after you finished the changes in the file.
Solution 9:[9]
I also faced the same issue, my code is also fine with class file and main method.i tried right click on my project->maven->update project. Select the project you want to update. Click on Force Update of Snapshots/Releases and press ok. After update my code worked correctly and did not printed my previous output.
Solution 10:[10]
I have a similar issue and it was executing the previous class. Only hours later I found out that the problem was in the main itself. I forgot to add "String[] args" in the main.
Solution 11:[11]
I was facing the same error, I just found out that I was missing the keyword "static"
Before:
public void main(String[] args) {
// ...
}
This worked for me:
public static void main(String[] args) {
// ...
}
Solution 12:[12]
The root cause is that you are using this method signature:
public static void main()
{
// ...
}
You need to change it to this, which works:
public static void main(String[] args)
{
// ...
}
Solution 13:[13]
I also found this bug on Eclipse.
Simply if you have several classes with main methods in Eclipse and you try to run your latest application it sometimes runs the old code.
All public methods have correct signatures, it has nothing to do with missing arguments.
There are two solutions:
- Either try to put classes with main methods in different packages.
- Simply restart eclipse and rerun the app (this always works for me).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
