2

Below my Test.java which is one .java file from a student project. I don't see why eclipse gives me " Selection does not contain any Java files " error when I tried to "run" it from the menu Run-->Run ? Could you explain why?

This post Java launch error selection does not contain a main type does not explain my problem. In my case "main" is well defined.

class Test {
   public static void main(String[] args) {
      test1();
   }

   static void test1() {
      Font f = new Font("SansSerif", Font.PLAIN, 70);
      Glyph g = new Glyph(f, 'g');
      System.out.println(g);
   }
}
Community
  • 1
  • 1
zell
  • 9,830
  • 10
  • 62
  • 115
  • Can you right click the class and select `Run As` -> `Java Application`? – Kai Sep 09 '11 at 15:32
  • Have you tried cleaning and recompiling? It's possible that Eclipse has a stale class file which does not have a main() method defined in it. – mcfinnigan Sep 09 '11 at 15:33
  • Have you tried right-clicking on the project in the project explorer window on the left, and selecting 'run' from that menu? – Sam Dufel Sep 09 '11 at 15:33
  • 2
    Uhhm, is that file inside a Java source Folder? Just checking... – Sean Patrick Floyd Sep 09 '11 at 15:37
  • possible duplicate of [Error: Selection does not contain a main type](http://stackoverflow.com/questions/16225177/error-selection-does-not-contain-a-main-type) – jww Sep 30 '14 at 05:57

3 Answers3

3

The problem is that the file isn't in a source folder. So for Eclipse, it's just a text file which (by coincidence) contains some Java code. But since the compiler never saw it, there is no .class file -> Eclipse can't run it.

Create a source folder (New... -> Source Folder) or move the file into an existing source folder in your project (they contain a little "package" symbol in the icon) and try again.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thanks a got. The reason is as you said, .java should be in \src folder. This is not done autmatically: creating a new project and create .java files". One should then MANUALLY move .java file to \src folder. – zell Sep 09 '11 at 16:07
  • It happens automatically if you use the "Create Java Class" from the toolbar (green "+" button) – Aaron Digulla Sep 11 '11 at 09:41
1

The name of the file and the name of the class should match. That is the reason, it's not able to recognize it. Please check the name again.

Varun Achar
  • 14,781
  • 7
  • 57
  • 74
0

Haven't used eclipse in some time now as I use InteliJ but make sure you edit configurations and select your main file so Run can then execute your app.

vitez
  • 73
  • 1
  • 2
  • 7