程序有main函数,在Eclipse中运行后出现selection does not contain a main type错误,环境配置是正确的

解决方案 »

  1.   

    有问题,在导航树中选中类,鼠标右键然后选择run as java application
      

  2.   

    import java.awt.*;
    import javax.swing.*;public class Welcome extends JFrame
    {
       private JLabel textJLabel;    // JLabel that displays text
       private JLabel pictureJLabel; // JLabel that displays an image   // no-argument constructor
       public Welcome()
       {
          createUserInterface();
       }   // create and position GUI components; register event handlers
       private void createUserInterface()
       {
          // get content pane and set layout to null
          Container contentPane = getContentPane();
          contentPane.setBackground( Color.YELLOW );
          contentPane.setLayout( null );
          
          // set up textJLabel
          textJLabel = new JLabel();
          textJLabel.setText( "Welcome to Java Programming!" );
          textJLabel.setLocation( 35, 0 );
          textJLabel.setSize( 550, 88 );
          textJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 ) );
          textJLabel.setHorizontalAlignment( JLabel.CENTER );
          contentPane.add( textJLabel );
          
          // set up pictureJLabel
          pictureJLabel = new JLabel();
          pictureJLabel.setIcon( new ImageIcon( "bug.png" ) );
          pictureJLabel.setBounds( 54, 120, 500, 250 );
          pictureJLabel.setHorizontalAlignment( JLabel.CENTER );
          contentPane.add( pictureJLabel );      // set properties of application's window
          setTitle( "Welcome" ); // set JFrame's title bar string
          setSize( 608, 413 );   // set width and height of JFrame
          setVisible( true );    // display JFrame on screen   } // end method createUserInterface   // main method
       public static void main( String[] args )
       {
          Welcome application = new Welcome();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   } // end method main} // end class Welcome
    代码是书上的例子应该是没问题的,
    1L的方法试过了还是同样的错误
      

  3.   

    我试了,在Eclipse里面能够运行.没有错误.你按Alt+Shifl+x,再按j就可以了.这个是快截键.
      

  4.   

    唯一解释,楼主直接按的顶部的运行按钮,而那个默认又不是当前的类。请参照1楼的做法,现在左侧树状目录中选中你的这个类。千万别选错了!!鼠标右键然后选择run   as   java   application
      

  5.   

    出现这个问题,是因为.java文件不在项目的src路径内,也就是说源代码未被eclipse编译,字节码不存在无法运行了
    在项目名上右键 -> Builder Path -> Configure Build Path -> 选择Source面板 再点Add Folder, 把源代码所在的包路径的上层目录加进来,而且如果你是把两个类写在一个文件里的话,你在右键选择Run As JavaAppication 的时候,要把光标至于包括main方法的类上
      

  6.   

    看看这样行不行:重新建立一个Java Project工程,弹出一个对话框,这里注意:选择:Create new project in workspace不要选别的选项,然后再建立一个含有main方法的类,再测试。希望对您有所帮助!
      

  7.   

    zzzkkk666 
    顶你一下!!!!!
    哈哈
      

  8.   

    我的错误原因是:在新建java project时勾选了Create separate folders as root for sources and class files.
    只要勾选Use project folder as root for sources and class files就行了