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

Community
  • 1
  • 1
letter Q
  • 14,735
  • 33
  • 79
  • 118
  • Please post `HowMARK_II_FitsInToBrainAnatomy.java` – Elliott Frisch Apr 08 '15 at 04:07
  • The links to your code are broken... – assylias Apr 13 '15 at 08:53
  • Yup.. links are broken but I just posted my answer explaining the issue. Hope that helps :) – Arkantos Apr 13 '15 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. – Eric Hughes Apr 16 '15 at 16:34
  • Please award the bounty so that this question stops showing up under the [Featured Questions](http://stackoverflow.com/questions?&sort=featured). – Sildoreth Apr 17 '15 at 13:41

7 Answers7

6

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.

Community
  • 1
  • 1
Arkantos
  • 6,530
  • 2
  • 16
  • 36
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.

isalgueiro
  • 1,973
  • 16
  • 20
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

Karl-Bjørnar Øie
  • 5,554
  • 1
  • 24
  • 30
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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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
Kat
  • 4,645
  • 4
  • 29
  • 81
  • Thanks for the help but I am just getting `Error: Could not find or load main class model.MARK_II.vision.HowMARK_II_FitsInToBrainAnatomy ` – letter Q Apr 08 '15 at 04:35
  • Do you have a method with signature `public static void main(String[] args)`? – Kat Apr 08 '15 at 05:09
  • Ya I have it on line 30 [here](https://github.com/WalnutiQ/WalnutiQ/blob/feature-issue_169/experiments/model/MARK_II/vision/HowMARK_II_FitsInToBrainAnatomy.java) – letter Q Apr 09 '15 at 15:13
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.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
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).

Olivier Croisier
  • 6,139
  • 25
  • 34