4

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

5
  • Please post HowMARK_II_FitsInToBrainAnatomy.java Commented Apr 8, 2015 at 4:07
  • The links to your code are broken... Commented Apr 13, 2015 at 8:53
  • Yup.. links are broken but I just posted my answer explaining the issue. Hope that helps :) Commented Apr 13, 2015 at 12:43
  • Package names are not just pretty; they're where java will look for the class. If your class is named model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy it must live in model/MARK_II/vision/ within a classpath directory or JAR file. If it doesn't, you need to restructure your source directory or build with the -d option to place the class in the expected location. Commented Apr 16, 2015 at 16:34
  • Please award the bounty so that this question stops showing up under the Featured Questions. Commented Apr 17, 2015 at 13:41

7 Answers 7

6
+200

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

0

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.

5 Comments

Thanks for the help but I am just getting Error: Could not find or load main class model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy
Add the main method, or it won't be a main class.
After I read your answer I did do that you can see in line 30 here
Did you recompile? And recreate your jar?
Good idea but I recompiled my jars and am still getting the error
0

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

3 Comments

Thanks for the help but I am just getting Error: Could not find or load main class model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy
Do you have a method with signature public static void main(String[] args)?
Ya I have it on line 30 here
0

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.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.