Error: Unable to open class file R.java

38,158

Solution 1

I have had same issue, all what I needed to do, was create project and restart eclipse.

//Edit: Complete collapsing of "Package" tree works as well.

Solution 2

This is a mighty annoying error.

It happens 100% of the time when I create a new android project.

What seems to work for me is to just delete the file (which DOES exist and is accessible by my user), then hit build, and it will regenerate it and stop moaning.

It's clearly a bug as there are too many people getting it simply by filling in the boxes in the new project dialog (like me) without even modifying any code!

Solution 3

Have a look at your console. It is an aapt aborted error. Since aapt is the tool that auto-generates R.java, therefore, R.java was not getting generated.

Since R.java is a auto-generated file that stores information about your drawables you should rebuild your project. Before that have a look at your layout xml files and check them for errors.

Notice that Project -> Build All should be selected. Also try to run your project.

Solution 4

Removing Read Only attribute on Eclipse workspace directory and Project->Clean->Build Project did the trick for me. I then clicked on the project directory from within Eclipse and did a F5 (Refresh) . R.java was generated in the gen folder.

Solution 5

I had a similar error on the creation of new projects myself. After messing around I tried moving the R.Java file into the same location as my projects java file (the src file). Eclipse then saw I removed the R.Java file from its location and created another and that seemed to clean up the error.....I dont know if it was because eclipse rebuilt the file and then saw it or what but that seemed to work for now.

Share:
38,158
Croydon Dias
Author by

Croydon Dias

Updated on July 18, 2022

Comments

  • Croydon Dias
    Croydon Dias almost 2 years

    Did a fresh install of Eclipse, JDK and android-sdk.

    I am currently receiving this error when creating a new project

    [2010-09-26 16:07:56 - Test] ERROR: Unable to open class file C:\workspace\Test\gen\com\example\test\R.java: No such file or directory

    What's the reason for this and how do I fix it?

    Eclipse Helios 32 bit
    java version "1.6.0_21"
    Android sdk API 8
    

    P.S. I'm new to Android development.

    EDIT: I tried most of your solutions, but nothing worked. So I started using my friend's install of Eclipse Ganymede.

  • Chris Huang-Leaver
    Chris Huang-Leaver over 13 years
    I think that's the same thing I discovered, I like your way better :-)
  • Admin
    Admin over 13 years
    Confirmed that deleting R.java and letting it regenerate works. Thanks Sam Bartle.
  • Ahmed Jihad
    Ahmed Jihad about 13 years
    this worked for me once. then I did a clean&build and I got the same error again.
  • oob
    oob about 13 years
    +1 have not seen this solution. i have got other things to work as well, but this seems to be the best solution.
  • Adam Woś
    Adam Woś about 13 years
    Great advice! Thanks, it was driving me crazy!
  • erdomester
    erdomester about 13 years
    i did this is and now eclipse says Unknown error: java.lang.NullPointerException
  • erdomester
    erdomester about 13 years
    But maybe this is an error caused by sg else, so for me your solution seems to have worked
  • James andresakis
    James andresakis about 13 years
    I found out what the problem was.....my debugging key had expired and all I had to do was erase it and eclipse or the adt generated a new one when I went back and cleaned all the projects. It was in the .android file on my computer. It may be in a different spot for you than it was for me