Two java files. Getting IllegalAccessError when running class with main method trying to access a method from the other file

前端 未结 3 346
野的像风
野的像风 2020-12-22 08:12

Learning Java. I have two files, each containing one java class. When I run the file with the main method, I get the following error:

Exception in threa

相关标签:
3条回答
  • 2020-12-22 08:43

    Actual Issue

    I got this exact same error* doing something very silly:

    I tried to run the file as java {main-class}.java. That simple!

    Instead, be sure to run it simply as java {main-class}.


    *Specifically, the error format I had, like yours:

    Exception in thread "main" java.lang.IllegalAccessError: failed to access class {pack.other-class} from class {pack.main-class} ({pack.other-class} is in unnamed module of loader 'app'; {pack.main-class} is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @29f69090)

      at {pack.main-class}.{who-cares-where}
      at {pack.main-class}.{who-cares-why}
                 . . .


    Extra Advice

    You can get a similarly annoying error on the same issue, namely inability to access packages in the same directory, if you only compile your {main-class}.

    So instead of javac {directory}/{main-class}.java

    Be sure to compile all of them at the same time, so there's no issue in cross-referencing:
      javac {directory}/*.java


    OP Specific

    This would just be a silly command-line mistake. If it's occurring in IntelliJ as well, as you say, this isn't your issue. However, I hope it's at least helpful to the others who come across your question with this error!

    0 讨论(0)
  • 2020-12-22 08:44

    Make sure each class is in the same folder, since the error is saying TapeDeckTestDrive can not find TapeDeck. I would recommend starting out with an IDE like Eclipse since it will help you focus more on coding and less with folder problems.

    I know your code is all good (in java 8 at least) since when I copied it in eclipse it works no problem, meaning it has to be a folder problem, a problem with the installed version of java, or the way you are running the code is not working for some reason. If both files are in the exact same folder then I would make sure your java version says 1.8 something in the system files (Program Files(x86) most likely in windows), if it does not say that version then it could be another problem with the code and syntax for that version. Another thing that might help is to put public behind the "class" on the first line of each class and make the Boolean public. This might be a syntax requirement on other versions of java or something that is needed when running off command prompt.

    0 讨论(0)
  • 2020-12-22 08:56

    Quick possible fix, try making the classes and methods "Public".

    Under normal circumstances this shouldn't be necessary but you may be hitting a specific case where it might be (It's an edge case so I don't know if it's a problem or not off the top of my head):

    Java is really uncomfortable with stuff being in the "Default" package--code from other packages can't access objects in the default package (Meaning no package statement). Although this shouldn't cause problems in your case, maybe your "package" level security settings are also not working in the default package.

    If this is actually the problem, the two fixes would be to make the packages and methods public (as I said above) or move both classes into a package.

    0 讨论(0)
提交回复
热议问题