What Does "file does not contain class api.configuration" Mean?

Please keep in mind that I am completely new to Java. I don't know what 'classes' and stuff are.

When trying to compile (javac -g Sphinx.java) this code:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;    
import java.io.PrintWriter;

import api.Configuration;
import api.SpeechResult;
import api.LiveSpeechRecognizer;

public class Sphinx {
    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();

        configuration.setAcousticModelPath("models/en-us/en-us");
        configuration.setDictionaryPath("models/en-us/cmudict-en-us.dict");
        configuration.setLanguageModelPath("models/en-us/en-us.lm.bin");

        PrintWriter pw = new PrintWriter(new PrintWriter("status.txt"));
        LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
        recognizer.startRecognition(true);
        pw.print("running");
        SpeechResult result = recognizer.getResult();
        recognizer.stopRecognition();
        pw.print("stopped");
        pw.close();

        PrintWriter pw2 = new PrintWriter(new PrintWriter("result.txt"));
        pw2.println(result);
        pw2.close();
    }
}

I get this error:

Sphinx.java:8: error: cannot access Configuration
import api.Configuration;
          ^
  bad source file: .\api\Configuration.java
    file does not contain class api.Configuration
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.

I don't quite understand what 'file does not contain class api.configuration' means, or how to fix it.

1 answer

  • answered 2019-06-24 20:47 JClassic

    Looking at your error message, it seems like your ./api/Configuration.java file is missing a package declaration.

    Can you make sure that in ./api/Configuration.java the first line has

    package api;
    

    This tells the compiler that your file is accessible through the api package, not the default package.