Home > other >  Error: Main class Test could not be found or loaded
Error: Main class Test could not be found or loaded

Time:02-05

I created an easy script with notepad and run in the cmd with this command:

java -cp . Test

Result:

Error: Main class Test could not be found or loaded

Code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        String fileName = "npcData.csv";
        String line = "";
        String csvSplitBy = ";";
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            while ((line = br.readLine()) != null) {
                String[] npc = line.split(csvSplitBy);
                System.out.println("nonCritSpecialNpc['"   npc[0]   "'] = true;");
            }
        } catch (IOException e) {
            System.out.println("Fehler beim Lesen der Datei: "   e.getMessage());
        }
    }
}

What am I'm missing?

CodePudding user response:

There are a couple of things you're missing here.

First, for public classes, the name of the class should match the name of the file. I.e., you either need to rename your class to Test, or rename the file to Main.java.

Second, you're missing a crucial step here - compiling. Java is not an interpreted language. You need to convert your .java class to something the JVM can run. This is done using the Java Compiler, or javac for short:

javac Main.java

After doing that, you'll have a .class file that the JVM can execute:

java -cp . Main
  •  Tags:  
  • java
  • Related