-1

I have a very simple class file (.class) which I am fully sure it is working fine, when I do comment package assignment:

package com.domain.core;
public class Challenge {
    public static final int ID = (int) Math.abs(Math.random()*10);

    public static void getDescription() {
        System.out.println("Challenge ID: " + ID);
    }

    public static void main(String args[]) {
        getDescription();
    }
}

I am testing this on Windows machine, through usual CMD. There is no global CLASSPATH specified. I have located .class file in the directory "myclasses" within C drive. So path to the class is: "C:\myclasses"

The purpose here is to run the class with the -classpath option of jvm. I have checked the following ways:

Within the C drive:

  1. java -cp "\myclasses" com.domain.core.Challenge
  2. java -cp \myclasses com.domain.core.Challenge
  3. java -cp .\myclasses com.domain.core.Challenge
  4. java -cp ".\myclasses" com.domain.core.Challenge
  5. java -cp "/myclasses" com.domain.core.Challenge
  6. java -cp /myclasses com.domain.core.Challenge
  7. java -cp ./myclasses com.domain.core.Challenge
  8. java -cp "./myclasses" com.domain.core.Challenge

Within the directory "C:\myclasses"

  1. java -cp "C:\myclasses" com.domain.core.Challenge
  2. java -cp C:\myclasses com.domain.core.Challenge
  3. java -cp "C:/myclasses" com.domain.core.Challenge
  4. java -cp C:/myclasses com.domain.core.Challenge

Replacing -cp with -classpath is resulting the same:

Error: Could not find or load main class com.domain.core.Challenge

JVM:

C:\>java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

Really odd issue. Could you guys pin point what I do here wrong ?

Thanks!

1 Answers1

1

The JVM expects to find that the Challenge.class file in the com/domain/core subdirectory of a directory in the classpath, not directly in a directory in the classpath. So if you have it in C:\myclasses, it's not where it's expected to be. You need to move it to C:\myclasses\com\domain\core and specify C:\myclasses in the class path.

This is as documented.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Crowder, thanks for the response. What I am seeing from the documentation https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html the way I have specified my class should be fine. Please have a look specially into this statement there: "Note: An interesting consequence of the package specification mechanism is that files which are part of the same package may actually exist in different directories. The package name will be the same for each class, but the path to each file may start from a different directory in the class path." – weltraumfahrer_x Sep 11 '18 at 15:08
  • May be problem is that I refer to Java 7 documentation, while I am running JVM 1.8...? – weltraumfahrer_x Sep 11 '18 at 15:09
  • 1
    1) I don't know what you think that means, but it actually referring to the case where you have two or more directories (or JAR / ZIP files) on the classpath. Each directory in the classpath is effectively a "root directory" for a path produced by mapping package names to directories. 2) No it isn't. The classpath mechanism has been pretty much the same since Java 1.1. – Stephen C Sep 11 '18 at 15:12
  • I have obviously misunderstood the statement from the doc. Thanks for the clarification! – weltraumfahrer_x Sep 11 '18 at 16:46