Search code examples
javaclasspathcommand-line-argumentsjvm-arguments

Running Java on Linux with Error: Could not find or load main class


I am getting the above error and the answer here isn't helping.

Basically I can't seem to run a file that I have compiled in Java. The file I am trying to run HowMARK_II_FitsInBrainAnatomy.java is here

I am using the following command to compile all needed .jars and the current directory with :. in the -cp argument at the end:

javac -cp /home/ugrads/majors/quinnliu/workspace/WalnutiQ/build/libs/WalnutiQ.jar:/home/ugrads/majors/quinnliu/workspace/WalnutiQ/referencedLibraries/gson-2.2.4.jar:. HowMARK_II_FitsInToBrainAnatomy.java

So after I use the above command I create the compiled file HowMARK_II_FitsInToBrainAnatomy.class but the the following command to run the file gives me the ERROR in the title of this question:

java -cp /home/ugrads/majors/quinnliu/workspace/WalnutiQ/build/libs/WalnutiQ.jar:/home/ugrads/majors/quinnliu/workspace/WalnutiQ/referencedLibraries/gson-2.2.4.jar:. model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy

I don't see what I am doing wrong as I add :. to my -cp


Solution

  • When you say,

    java -cp jars-to-add:. model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy
    

    As your class has package declaration like this

    package model.MARK_II.vision;
    

    you need to use the Fully Qualified Class Name to invoke the main() in that class which you're doing already but also need to execute the command from correct directory.

    I think you're already inside your model/MARK_II/vision directory when you're invoking this javac command, you need to come out of this directory and execute the command from a directory which has all these directories something like below

    DirectoryToExecute
        --model
           --MARK_II
            --vision
              --HowMARK_II_FitsInToBrainAnatomy.class
    

    So I suggest you cd to that directory and then invoke the above command, then it will work :)

    Have a look at this answer on a similar issue.