0

Question:

  1. I am unable to execute classes that extend other classes. I believe it is due to how I am declaring the classpath. I am executing the below via terminal. Any guidance would be awesome. Thanks!
Classes:
  1. Arachnid --> Parent
  2. Spider --> Subclass of Arachnid
  3. GardenSpider --> Subclass of Spider
Directory:

All code is executed from parent directory (I can see bin and src).

.
├── bin
│   ├── Arachnid.class
│   ├── GardenSpider.class
│   └── Spider.class
└── src
    ├── Arachnid.java
    ├── GardenSpider.java
    └── Spider.java
Classes: 1. Arachnid
// Chapter 6 : 6

/*
This is a superclass
*/
public class Arachnid {
/*
 * Constructor
 */
public Arachnid () {
        System.out.printf("%s%n%n", "Executing Arachnid constructor");
}
public static void main(String[] args) {
        Arachnid a = new Arachnid(); // create Arachnid object
}
}

2. Spider
// Chapter 6 : 6

/*
* This is a subclass - inherits all protected and public members from parent (field, methods, and nested classed)
*/
public class Spider extends Arachnid{
  /*
   * Constructor
   */
   public Spider () {
    System.out.printf("%s%n%n", "Executing Spider constructor");
  }
  public static void main(String[] args) {
    Spider a = new Spider(); // create  spider object
  }
}

3. Garden Spider
// Chapter 6 : 6

/*
This is a subclass
*/
public class GardenSpider extends Spider{
  /*
   * Constructor
   */
  public GardenSpider () {
    System.out.printf("%s%n%n", "Executing GardenSpider constructor");
  }

  public static void main(String[] args) {
    GardenSpider gs = new GardenSpider(); // create garden spider object
  }
}

Compile Classes
  1. javac -d bin src/*.java
Execute Classes: 1. java -cp bin/Arachnid.class src/Arachnid.java

Executing Arachnid constructor

2. java -cp bin/Arachnid.class:bin/Spider.class src/Spider.java
src/Spider.java:7: error: cannot find symbol
public class Spider extends Arachnid{
                            ^
  symbol: class Arachnid
1 error
error: compilation failed
3. java -cp bin/Arachnid.class:bin/Spider.class:bin/GardenSpider.class src/GardenSpider.java
src/GardenSpider.java:6: error: cannot find symbol
public class GardenSpider extends Spider{
                                ^
symbol: class Spider
1 error
error: compilation failed
References:
  1. What does "Could not find or load main class" mean?
Notes:
  1. bin/* shorthand option to leverage when declaring classpaths
  2. Class separator :
  3. FQN should be used for passing classpath
  4. You don't need .java when executing (if you do it right?)
FQN:
  • /Users/hw_2/src/*.classes
  • /Usershw_2/src/*.java
FQN Example:
  1. java -cp /Users/hw/hw_2/bin/Arachnid.class src/Arachnid.java
AlifUnseen
  • 33
  • 6

1 Answers1

1

"Could not find or load main class" means that the class could not be found, or that the class did not have a method with signature public static void main(String[]).

I think src/Arachnid.java is wrong in both cases. You don't execute the .java source file, you execute the .class file. Which in your case are in the /bin directory. Try something like:

java -cp bin/Arachnid.class:bin/Spider.class Spider

markspace
  • 10,621
  • 3
  • 25
  • 39
  • Thanks, @markspace. What do you mean by src/Arachnid.java being wrong? Also, when I execute `java -cp bin/Arachnid.class:bin/Spider.class Spider` I get an `Error: Could not find or load main class` – AlifUnseen Apr 13 '20 at 01:22
  • Copy and paste, this is what worked for me: `java -cp .\build\classes quicktest.ZipFileTest` Try adding the `.` at the beginning of the classpath, and don't list each individual Java class, adding just the directory to the classpath is enough. – markspace Apr 13 '20 at 02:32
  • (Obviously I'm working on Windows; use a correct path syntax for your system, and any other system differences you'll have to account for also.) – markspace Apr 13 '20 at 02:34
  • Success! My java execution calls were off indeed. Thanks for the guidance! You rock. – AlifUnseen Apr 14 '20 at 04:05