gpt4 book ai didi

Java:ClassNotFound 类路径问题

转载 作者:行者123 更新时间:2023-12-01 16:26:18 25 4
gpt4 key购买 nike

我正在尝试将文件中的一些信息读取到某些对象中。 Main 方法只是将信息读入一些字符串变量,然后使用这些字符串来初始化对象。很简单。这些对象使用 BST 存储。

但是,我收到的错误是ClassNotFoundException。除非我运行 java 'file' 命令,'file' 的拼写和大写正确。

我一直在读到您可以更改 JVM 在搜索类文件时使用的路径。

所以我尝试了:

设置 CLASSPATH=$CLASSPATH=~/../../BackEnd

但这没有做任何事情..

这是我的主文件..

import java.util.Scanner;
import java.io.File;
class BackEnd
{
public static void main(String args[]) throws java.io.FileNotFoundException
{


Tree.ServiceTree providers = new Tree.ServiceTree();
String path = "./providers.txt";
Scanner read = new Scanner (new File(path));
read.useDelimiter(",");
String information[] = new String[5];//array of strings used to store info from file, then used to initialize objects
try
{
while(read.hasNext())
{
for(int i = 0; i < 5; ++i)
{
information[i] = read.nextLine();//read in all the info into the array
}

Services.Service newService;//used as dynamic reference to be passed to tree
Services.Service serviceInfo = new Services.Service(information[0], information[1]);//initalizes base class to be passed to derived constructor

switch(information[0])//check type to initalize appropriate object
{
case "Dogwalk":
newService = new Services.Dogwalk(serviceInfo, information[2], information[3]);
case "Groceries":
newService = new Services.Groceries(serviceInfo, information[2], information[3]);
case "Housework":
newService = new Services.Housework(serviceInfo, information[2], information[3]);
}
providers.insert(information[4], newService);
}
read.close();
throw new java.io.FileNotFoundException("File not found...");
}
catch(java.io.FileNotFoundException exception)
{
System.out.println("File not found");
}
//providers.display();
}
}

最佳答案

想通了。错误与编译或类路径无关,是由于未初始化的变量newService

关于Java:ClassNotFound 类路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62137397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com