0

I tired to run the following java codes via eclipse but I get the errors. So, what should I do? I am a beginer.

I tried the second answer of the page but it did not work for me. Eclipse error ... cannot be resolved to a type

ERROR

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    JTable cannot be resolved to a type
    JTable cannot be resolved to a type

    at ppp/ppp.table.main(table.java:11)

CODES

package ppp;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class table {

    public static void main(String[] args) {
        JTable table = new JTable();

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hausdorff
  • 47
  • 5
  • I am not sure JTable is installed or not. – Hausdorff Jan 05 '22 at 01:47
  • 1
    JTable is part of Java SE. So you should be able to compile and run this on any release of Java (TM) since about Java 1.3.x. Indeed it works for me. (Modulo ... the code does nothing visible ... 'cos ... bugs.) – Stephen C Jan 05 '22 at 02:10
  • 1
    The most likely explanations are: 1) You are trying to compile this for Android: it won't work! 2) Your IDE is misconfigured. – Stephen C Jan 05 '22 at 02:12
  • Thank you. I am not sure what the Android means. I understand my configuration of eclipse may be wrong. Why it does not work for Android? How do I verify that I do not try for Android? – Hausdorff Jan 05 '22 at 02:25
  • It doesn't work on the Android platform because Java on Android doesn't support `Swing`. You would **know** if you were trying to write an Android app. (It is the platform for > 75% of the world's mobile phones. I'm surprised you have never heard of it. ) – Stephen C Jan 05 '22 at 02:49
  • When I build a project, eclipse asked me what kink do I make? I guess , there is a project for Android.... Now, I understand Android means phone. I am confused what should I select when I build a project. – Hausdorff Jan 05 '22 at 03:04
  • If your IDE is Eclipse, I don't think it supports Android. And if it did, you would explicitly need to tell it that the project is an Android project. This won't be the problem in your case. – Stephen C Jan 05 '22 at 03:13
  • Try reading the "Obscure reasons" section of https://stackoverflow.com/questions/25706216. Check Eclipse's configured Java compiler and class library; see https://stackoverflow.com/questions/2573836. Try force rebuilding the project. Try restarting Eclipse. Try building / running your code from the command line. (Works for me.) – Stephen C Jan 05 '22 at 03:17
  • See your code [run successfully at IdeOne.com](https://ideone.com/vNwpdH). So something very unusual must be going on. Swing is built into Java, and your `import` statements are correct. Targeting Android rather than Java, as others said, is the only reason I can think of. Or your project is very screwed up: Try clearing caches and rebuilding your project, however that is done in Eclipse. Or your copy of Eclipse IDE or Java JDK are corrupt: Reinstall. – Basil Bourque Jan 05 '22 at 03:44
  • I am not sure what my treatment (e.g., uninstall and reinstall java) effected but now the code works fine. Thank you, Bourque and Stephen. – Hausdorff Jan 05 '22 at 06:50

0 Answers0