Search code examples
javacollectionsconsolejavac

Java - Error starting program with arguments in console


In general, no water. The input is a link (path to the file) in CSV format, each line (array) should be output. For convenience, I downloaded a third-party library to work with CSV files. Running through the command line. Compiles well, did everything right (sort of), registered the path to the library, etc .:

E:\projects\java.Adam2.0\src\com\company>javac -cp "E:\projects\Librari\opencsv-5.2.jar" QI100.java

E:\projects\java.Adam2.0\src\com\company>cd E:\projects\java.Adam2.0\src

I run (like ... I'm not sure here anymore) correctly, specifying the argument (path to the file), but it gives the following error:

E:\projects\java.Adam2.0\src>java QI100 C:\Users\ARTHUR\Downloads\file.csv

ERROR:

Error: Could not find or load main class QI100
Caused by: java.lang.ClassNotFoundException: QI100

Forgot about the code itself:

package com.company;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class QI100 {
    public void main (String[] args) throws IOException, FileNotFoundException, CsvValidationException {
        String pathToFile = args[0];
        BufferedReader reader = new BufferedReader(new FileReader(pathToFile));
        CSVReader csvReader = new CSVReader(reader);
        ArrayList<String[]> lineArray = new ArrayList<>();
        String[] line;
        while ((line = csvReader.readNext()) != null) {
            lineArray.add(line);
            System.out.println(line);
        }
    }
 }

Solution

  • You have compiled the Java file at E:\projects\java.Adam2.0\src\com\company while you are trying to access it at E:\projects\java.Adam2.0\src. Therefore, the system is not able to find the class. Use the command as follows:

    E:\projects\java.Adam2.0\src>javac -d . -cp "E:\projects\Librari\opencsv-5.2.jar" QI100.java
    
    E:\projects\java.Adam2.0\src>java -cp "E:\projects\Librari\opencsv-5.2.jar" com.company.QI100 C:\Users\ARTHUR\Downloads\file.csv
    

    Note: The . specifies the current directory and the switch -d specifies where to place generated class files. To learn more about it, just use the command, javac.