![](/img/trans.png)
[英]Exception in thread "main" java.lang.IllegalAccessError: failed to access class
[英]Getting Error Exception in thread “main” java.lang.IllegalAccessError:
我的目录结构如下所示:
ObjectPassing
|->student.txt
|->StudentAddress.java
|->StudentClient.java
|->StudentServer.java
|->StudentAddress.class
|->StudentClient.class
|->StudentServer.class
并且我在运行文件StudentClient.java时出错,该文件包含StudentAddress类的实例。服务器已成功启动,但客户端无法正常工作。
源代码 : -
import java.net.Socket;
import java.io.ObjectOutputStream;
public class StudentClient{
private String hostName;
private int portNumber;
private Socket client;
public StudentClient(String th,int tp){
this.hostName = th;
this.portNumber = tp;
try {
client = new Socket(hostName,portNumber);
ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
StudentAddress data = new StudentAddress();
data.getData();
output.writeObject(data);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new StudentClient("localhost",8000);
}
}
所有类StudentAddress,StudentServer,StudentClient均已成功编译。在运行StudentClient.java时,显示以下错误:
命令:javac StudentClient.java
Exception in thread "main" java.lang.IllegalAccessError: class StudentClient tried to access method StudentAddress.<init>()V (StudentClient is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @5e57643e; StudentAddress is in unnamed module of loader 'app')
at StudentClient.<init>(StudentClient.java:18)
at StudentClient.main(StudentClient.java:27)
哦,等等,您正在从ObjectPassing
目录运行javac,这就是问题所在。 从Networking
目录中运行它。
当您提到package
关键字时, javac
将在您运行它的当前目录下期望一个目录,其中包含提到的软件包名称的一个或多个目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.