Reputation: 4033
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".
Upvotes: 8
Views: 63016
Reputation: 1
I had a similar problem. Turns out it was a typo. I had written
public static void main(String args)
when I should have written
public static void main(String[] args)
The lack of [ ]
braces meant Eclipse automatically sought out a class with a complete main method.
Upvotes: 0
Reputation: 30
Make sure the file is saved. Make sure the main method is written correctly: public static void main (String [] args)
.
Upvotes: 0
Reputation: 947
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) {
// ...
}
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 21
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.
Upvotes: 0
Reputation:
I had the same prob. just make sure to have the main and all ure classes on public...
Upvotes: 0
Reputation: 1
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
.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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:
Upvotes: -2
Reputation: 1
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)
{
// ...
}
Upvotes: -2
Reputation: 93
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.
Upvotes: 2
Reputation: 21
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.
Upvotes: 2
Reputation: 33544
1. Please see that you have included the main()
method in this class file.
2. If you still have the probs then right click on this java file in the package explorer and select Run
3. Keep the class name which contains your main() method, and the File name with which you save your file in Eclipse as SAME.
Its logicaloperators.java
NOT logicaloperator.java
......You have missed the "s"
Eg:
class logicaloperators{
public static void main(String[] args){
}
}
Save it as logicaloperators.java
Upvotes: 10
Reputation: 420
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
Upvotes: 0
Reputation: 2242
If you want to run a particular java file which contains the main(String[] args) method.
Right click on the file -> RunAs - > Java Application.
Upvotes: 2