0

I am learning to code in java, and using Eclipse, the android IDE as my code writing program. Whenever I create a new project, there is always a red exclamation mark on the icon for the main folder of my project in the project explorer section on the left side of the screen. Also, whenever I create a new class file, some of the code has an error in it, even when it shouldn't. One example of this is when I add the main method, with this code: public static void main(String[] args) I get the error "String cannot be resolved to a type".

There is also an automatically generated line of code at the beginning of the class which has an error in it: public class Class is giving me the error: "Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor" there is a red dotted line under the word "Class" in the code, so I assume its trying to tell me that's where the error is.

I am also getting an error in the code System.out.println();saying "System cannot be resolved".

I was able to create an integer without getting an error using this code: int number = 3;

I was able to use all this code mentioned above without getting an error before, I don't know what's wrong... Could I have changed some settings or something?

screenshot of eclipse errors in eclipse

VC1
  • 1,660
  • 4
  • 25
  • 42
user3308082
  • 137
  • 2
  • 10
  • 3
    Looks like you're not pointing to a JDK/SDK for your project. – Dave Newton Mar 27 '14 at 18:45
  • 6
    Also, don't call your class `Class`, lest you interfere with `java.lang.Class`. – Makoto Mar 27 '14 at 18:46
  • Don't name your class `Class` – donfuxx Mar 27 '14 at 18:46
  • Are you actually creating an Android app? Setting those up is different. – ajb Mar 27 '14 at 18:53
  • @DaveNewton how do i fix this? – user3308082 Mar 27 '14 at 18:54
  • @ajb at the moment i'm not creating an android app, im still learning java – user3308082 Mar 27 '14 at 18:55
  • The first thing you need to do is rename your class name like others have said above. – Noob Mar 27 '14 at 18:57
  • I changed the class to a different name, now what? – user3308082 Mar 27 '14 at 18:59
  • Now try to run the code. Is it still giving an error? If so tell us what the error is. – Noob Mar 27 '14 at 19:00
  • I renamed my class to Bob, ran the code, and got this error: `Exception in thread "main" java.lang.NoClassDefFoundError: Bob Caused by: java.lang.ClassNotFoundException: Bob at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)` – user3308082 Mar 27 '14 at 19:03
  • Right click on your project > properties > java build path > libraries. Do you see JRE System Library? – Noob Mar 27 '14 at 19:04
  • Did you also change the name of the source file `Class.java` to `Bob.java`? – ajb Mar 27 '14 at 19:05
  • Also you need to change the name of the java file to Bob – Noob Mar 27 '14 at 19:06
  • @Noob I see "JRE System Library [javaSE-1.7] (unbound)" – user3308082 Mar 27 '14 at 19:07
  • Go to Window > preferences > java > installed JREs. Is JRE there? – Noob Mar 27 '14 at 19:09
  • @ajb i deleted `Class.java` and created a new one named `Bob.java` which contains the exact same code that `Class.java` had, except for the `int number = 3;` and the `System.out.println();` – user3308082 Mar 27 '14 at 19:09
  • @Noob the only thing i see there is `Java SE 6 (MacOS X Default) (default)` – user3308082 Mar 27 '14 at 19:12
  • Find where thats located, then change the java build path to wherever java 6 is. – Noob Mar 27 '14 at 19:14
  • @Noob how do I do that? I see the location of it, but how do I change the build path? – user3308082 Mar 27 '14 at 19:16
  • First basic question: did you install Java Development Kit (JDK)? – Pshemo Mar 27 '14 at 19:21
  • @Pshemo i downloaded the SDK ADT bundle for mac from the android developer website. Whatever came with that, i have. I dont think it comes with the JDK, but I could be wrong – user3308082 Mar 27 '14 at 19:24
  • I wouldn't trust the Android bundle to work for non-Android-app projects, but I'm sure others know more about this than I do. – ajb Mar 27 '14 at 19:27
  • To be sure download [official JDK](http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html) and install it. Later let eclipse know about its installed location. Go to `Window -> Preferences -> Java -> Installed JREs` and `add` `SrandardVM` and as directory set up folder with installed JDK. – Pshemo Mar 27 '14 at 19:27
  • @ajb it was working perfectly fine before, then suddenly i think i did something and it stopped working – user3308082 Mar 27 '14 at 19:27
  • @Pshemo its asking me for the JRE home directory, and i don't know what folder it is... – user3308082 Mar 27 '14 at 19:40
  • If you installed Java then based on [this](http://stackoverflow.com/questions/15826202/where-is-java-7-installed-on-mac-os-x) question it seems that its directory should be in `/Library/Java/JavaVirtualMachines/`. In case of JRE you can also check something like `/usr/libexec/java_home -v 1.6`. If not there check directory where you installed `SDK ADT`. – Pshemo Mar 27 '14 at 20:11
  • @Pshemo when i put /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home in the jre home section, a bunch of stuff came up in the jre system libraries section. what do i do next? do i have to put anything in the "Default VM arguments" section? – user3308082 Mar 27 '14 at 20:35
  • I am not sure if JDK 8 (the newes is) the best choice for you since it may be not yet supported by Android (I am not sure but wouldn't be surprised). I would try with JDK 7. Also `a bunch of stuff came up in the jre system libraries section` That is OK. `what do i do next? do i have to put anything in the "Default VM arguments" section?` You don't have do add any VM arguments. If you want to enable assertions you can use `-ea` but that is not mandatory. If you can then just click finish and make your project use this version of JDK. – Pshemo Mar 27 '14 at 20:57
  • @Pshemo okay, I created a new java project in eclipse using JDK 8, and it seems to be working now, if i encounter any problems with android compatibility, i will try JDK 7. THANK YOU SO MUCH! I honestly had no idea what was wrong, i really appreciate your help – user3308082 Mar 27 '14 at 21:03

1 Answers1

0

Make a new project with any other name than a Reserved keyword.

You get errors because you changed your class name, when your compiler looks for the old name. Try clean and build.

Edit:give your package a name, and write

package yourname;

at the start of your .java file, before ANY code.

George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88
  • I was having this same problem in other projects i created where i used names that were not reserved keywords – user3308082 Mar 27 '14 at 19:15
  • Try renaming your package and writing `package yourname` at the start of the .java file. If that doesn't work neither, I got no other ideas. – George Irimiciuc Mar 27 '14 at 19:15
  • @user3308082 The JRE System Library node you've shown does not appear to be expandable. Could it be that you've removed or upgraded your Java version and the `Installed JREs` preference page's contents are stale--meaning you really *don't* have the standard classes that Eclipse says are not found, at least not where it thinks they are? – nitind Mar 27 '14 at 21:10