我正在尝试将文件中的某些信息读取到某些对象中。 Main方法只是将Information读取到一些字符串变量中,然后使用这些字符串来初始化对象。很简单使用BST存储对象。
但是,我得到的错误是ClassNotFoundException
。除了我运行java 'file'
命令时,“文件”的拼写和大写正确。
我一直在阅读,您可以更改JVM在搜索类文件时使用的路径。
所以我尝试了:
set 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