Eclipse is executing the wrong Java file

Viewed 51213

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".

14 Answers

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.

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.

I had the same prob. just make sure to have the main and all ure classes on public...

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.

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.

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.

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) {
// ...
}

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)
{
    // ...
}

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:

  1. Either try to put classes with main methods in different packages.
  2. Simply restart eclipse and rerun the app (this always works for me).
Related