javaclassclasspath

How to execute Java program from parent directory


I have a directory structure like this:

./
 +-- myClass
 |   +-- MainClass.class
 |
 +-- dummy
     +-- DummyClass.class

MainClass contains the public static void main(String args[]) and imports dummy.DummyClass.

I compile with a simple command like javac myClass/MainClass.java dummy/DummyClass.java and all is fine. But when I try to execute it from ./ using java myClass.MainClass I get the "cannot find main class" error.

If I change the classpath with the -cp option then MainClass can't find DummyClass anymore.

Any hints?


Solution

  • Does MainClass have this at the top: package myClass;?

    This is necessary for the class to considered part of the package myClass and only then it can be correctly addressed as myClass.MainClass - the directory structure has to mirror the pacakge structure.