【问题标题】:Java: ClassNotFound Classpath issuesJava:ClassNotFound 类路径问题
【发布时间】:2020-06-01 17:13:15
【问题描述】:

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

但是,我得到的错误是ClassNotFoundException。除了我运行java 'file' 命令时,'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();
    }
}

【问题讨论】:

  • 你看过java命令的-cp选项吗?

标签: java jvm classpath classnotfound


【解决方案1】:

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

【讨论】:

  • 您使用的是哪个 IDE?它应该显示编译错误说“变量'newService'可能没有被初始化”
猜你喜欢
  • 2023-03-16
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2010-09-24
相关资源
最近更新 更多