letter Q
letter Q

Reputation: 15405

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

Upvotes: 4

Views: 1976

Answers (7)

isalgueiro
isalgueiro

Reputation: 2033

if HowMARK_II_FitsInToBrainAnatomy is in model.MARK_II.vision package, file HowMARK_II_FitsInToBrainAnatomy.class has to be in model/MARK_II/vision directory in the filesystem.

Upvotes: 1

Olivier Croisier
Olivier Croisier

Reputation: 6149

Your first command-line suggests that the class to compile is in the current directory. Compiling it will produce a .class file in that same directory.

Now, when you try to run your program, you are perfectly right to pass the full name (package + class name) of the class to run ; but to find that class, java applies a convention stating that each segment of the package must correspond to a physical directory on the hard drive, starting from the current directory.

So instead of looking for your class in the current directory, it will try to look for a model/MARK_II/vision/ directory tree, and for a file named HowMARK_II_FitsInToBrainAnatomy.class there.

For your class to be found, you must set your current directory to the parent directory of model, still add . to your classpath (as it will be used as the root to find subdirectories) and keep your command line as-is (perhaps adjusting the paths to the additional jars as required).

Upvotes: 0

Your class HowMARK_II_FitsInToBrainAnatomy isn't normal executable class, but a JUnit test case. To run a JUnit test case from the command line you can use the org.junit.runner.JUnitCore class.

Compile

javac -cp build/libs/WalnutiQ-master.jar:referencedLibraries/junit-4.12.jar:referencedLibraries/gson-2.2.4.jar:. experiments/model/MARK_II/vision/HowMARK_II_FitsInToBrainAnatomy.java

And run

java -cp build/libs/WalnutiQ-master.jar:referencedLibraries/junit-4.12.jar:referencedLibraries/hamcrest-all-1.3.jar:referencedLibraries/gson-2.2.4.jar:experiments/ org.junit.runner.JUnitCore model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy

I ran these from the root of your project from GitHub. I had to download JUnit and Hamcrest manually

Upvotes: 1

SubOptimal
SubOptimal

Reputation: 22993

You need to change your compile command from

javac -cp <the jars>:. HowMARK_II_FitsInToBrainAnatomy.java

to

javac -cp <the jars>:. -d . HowMARK_II_FitsInToBrainAnatomy.java

This will create the HowMARK_II_FitsInToBrainAnatomy.class in the right directory.

Upvotes: 0

Arkantos
Arkantos

Reputation: 6608

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.

Upvotes: 6

Kat
Kat

Reputation: 4695

When running with java, you must specify the fully qualified name (ie, with the package). So if the class you want to run is HowMARK_II_FitsInToBrainAnatomy and its package is model.MARK_II.vision, then you'd have to run:

java -cp <...> model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

The first line of your class is

package model.MARK_II.vision;

That means you must specify the fully-qualified name to run it.

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

Of course, it will also need a method

public static void main(String[] args)

as an entry-point.

Upvotes: 0

Related Questions