两个Java文件.使用主方法尝试从其他文件访问方法时,运行类时出现IllegalAccessError [英] Two java files. Getting IllegalAccessError when running class with main method trying to access a method from the other file

查看:441
本文介绍了两个Java文件.使用主方法尝试从其他文件访问方法时,运行类时出现IllegalAccessError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Java.我有两个文件,每个文件包含一个java类.使用main方法运行文件时,出现以下错误:

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:

线程"main"中的异常java.lang.IllegalAccessError:无法从TapeDeckTestDrive类访问TapeDeck类(TapeDeck在加载程序"app"的未命名模块中; TapeDeckTestDrive在加载程序com的未命名模块中. sun.tools.javac.launcher.Main $ MemoryClassLoader @ 18bf3d14) 在TapeDeckTestDrive.main(TapeDeckTestDrive.java:3)

Exception in thread "main" java.lang.IllegalAccessError: failed to access class TapeDeck from class TapeDeckTestDrive (TapeDeck is in unnamed module of loader 'app'; TapeDeckTestDrive is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @18bf3d14) at TapeDeckTestDrive.main(TapeDeckTestDrive.java:3)

class TapeDeckTestDrive{
  public static void main(String[] args){
    TapeDeck t = new TapeDeck();
    t.canRecord = true;
    t.playTape();

    if (t.canRecord == true) {
        t.recordTape();
    }
  }
}
class TapeDeck {
  boolean canRecord = false;
  void playTape(){

    System.out.println("tape playing");
  }
  void recordTape(){

    System.out.println("tape recording");
  }
}

请帮忙吗?

推荐答案

请确保每个类都位于同一文件夹中,因为错误提示TapeDeckTestDrive找不到TapeDeck.我建议您从像Eclipse这样的IDE开始,因为它可以帮助您将精力更多地集中在编码上,而较少地关注文件夹问题.

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.

我知道您的代码都很好(至少在Java 8中是这样),因为当我在eclipse中复制它时,它没有问题,这意味着它一定是文件夹问题,已安装的Java版本或方法您正在运行代码由于某种原因无法正常工作.如果两个文件都在完全相同的文件夹中,那么我将确保您的Java版本在系统文件(Program Files(x86)最有可能在Windows中)中显示1.8,如果未说明该版本,则可能是另一个问题该版本的代码和语法. 可能有帮助的另一件事是,在每个类的第一行将public放在"class"后面,并使Boolean公开.这可能是其他版本的Java的语法要求,或者是在运行命令提示符时所需的某些东西.

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.

这篇关于两个Java文件.使用主方法尝试从其他文件访问方法时,运行类时出现IllegalAccessError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆