19

I get this error message in Eclipse:

Access restriction: The type DirectoryWalker is not accessible
due to restriction on required library 
/Library/Java/Extensions/commons-io-1.4.jar

what does this mean? There's this other SO question on the same topic but it doesn't seem to apply in this case. I'm not creating a new java class, I'm trying to use one.

Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • possible duplicate of [Access restriction on class due to restriction on required library rt.jar?](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar) – blo0p3r Dec 18 '14 at 19:39
  • 2
    Um. I noted the similarity myself 5+ years ago, but this is slightly different. At least I thought it was at the time. But it was 5 years ago. – Jason S Dec 18 '14 at 20:17

9 Answers9

75

go to java build path and under Library tab, remove JRE System Library. Then again Add Library --> JRE System Library

Worked for me

Maheen
  • 751
  • 5
  • 2
  • 3
    Nice find. This should definitely be best answer. – Joel Christophel Aug 15 '13 at 03:29
  • works for me too, now how do i do this magic in my maven POM file? – Peter Perháč Feb 11 '14 at 16:49
  • 2
    Absurd. Had problems with JavaFX getting "access restrictions", and this trick fixed it! Thanks! – stolsvik Apr 13 '14 at 11:06
  • I had this error come up for trying to access JavaFX (jfxrt.jar) for the first time (HelloWorld tutorial for JavaFX). Small additional note: when you delete the jre system library, be sure and exit the "Build Configuration" area after doing so. Then, when you return and add it back in, the errors go away. Bizare bug and I have no explanation as to what is going on. – Phil Freihofner Jul 23 '15 at 20:29
13

This is my least-favorite error, I've run into it a few times, and the "restriction on required library" is just dull enough to leave you scratching your head. However, it's easy to fix.

The quickest way (I'm using Helios) is to go to Window->Preferences->Java->Compiler->Errors/Warnings, find and open the "Deprecated and Restricted API" bullet, and set Forbidden and Discouraged Reference to something other than Error.

"Warning" is usually best, as something out there in Java land is triggering this and you probably want a footnote on it. @SuppressWarnings and its cousins, 98% of the time, have the same effect on a programmer's personal and social logic as, say, an ether binge might. But that's just my humble analysis.

Best of luck!

6

At a guess another library you are using also requires Apache Commons I/O, but a different version. Having a piece of code (transitively) have access to two versions of the same packages is a little tricky. You can probably arrange it so your code and the other library use the same version. It's all much easier with CLASSPATH! :)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • 1
    is there a better well-known place to put .jar files that won't be automatically "sucked in"? – Jason S Jul 07 '09 at 02:03
  • (removing everything from my classpath & adding specific libs to my Eclipse build path solved it, thanks.) – Jason S Jul 07 '09 at 02:10
4

For what it's worth, I had this error recently too (different library).

My IDE (MyEclipse) references a certain library as part of the overall project classpath, but I also had a copy of the relevant JAR in my machine's Java extensions folder.

The resulting clash gave the (unhelpful) error that you've experienced. So for anyone else hitting this error, you might want to consider your classpaths too.

Ben
  • 7,548
  • 31
  • 45
3

Project build path settings -> Libraries tab -> select JRE System Library -> Edit button -> change from Execution Environment radio tab which says J2SE 1.x (jdk1.6.0_21) to Workspace default JRE (jdk1.6.0_21) [Collected]

This is the best solution for sure!

Hasan Sawan
  • 391
  • 2
  • 14
1

I had the same error the way I fixed was setting the default workspace JRE to the same as the project, I had JRE6 for the workspace and JRE7 for the project setting both to JRE7 fixed the problem

drverboten
  • 325
  • 3
  • 7
0

In my case, I was able to solve this by unticking the folder attribute 'Read-only' in the Properties of the project folder.

Then, I re-configured Build path of the project, then Clean.

It had solved the error.

CMA
  • 2,758
  • 5
  • 28
  • 40
0

For those looking why Eclipse can build the project, but Maven can't, check if you have JUnit as a Library, or as a plugin dependency. If it is added as a library, Maven will not be able to pick it up and throw many access restriction (type xyz is not API).

Joost
  • 536
  • 4
  • 16
0

If you are using m2eclipse, I find "Update Project Configuration" fixes it.

Right-click the project, select Maven > Update Project Configuration and give it a few seconds.

David Carboni
  • 1,556
  • 23
  • 24