Why does the `package` instruction in java make the class file impossible to run?

I'm trying to compile a java file with a package directive. However, when adding the directive to the most simple program stub, I get an error and cannot launch the program anymore... What's wrong ?

Dummy0:

class Dummy0 {
  public static void main( String[] args ) {
    System.out.println("Hello, world!");
  }
}

Dummy1:

package de.train;

class Dummy1 {
  public static void main( String[] args ) {
    System.out.println("Hello, world!");
  }
}

And here is the output I had. Everything compiled fine. But I cannot run the class de.train.Dummy1, although it's obviously there.

$ javac Dummy*.java
$ java Dummy0
Hello, world!
$ java Dummy1
Error: Could not find or load main class Dummy1
$ java de.train.Dummy1
Error: Could not find or load main class de.train.Dummy1
$ javap Dummy1.class
Compiled from "Dummy1.java"
class de.train.Dummy1 {
  de.train.Dummy1();
  public static void main(java.lang.String[]);
}

I'm under windows for this project. Is this some security restriction? How to remove it?

Jon Skeet
people
quotationmark

It's looking for the class de.train.Dummy1, which means it will look for a file called Dummy1.class in a directory de\train - but the file will actually be in the current directory.

Options:

  • Keep your source code where it is, but get the compiler to generate the directory structure for you:

    > javac -d . Dummy*.java
    > java de.train.Dummy1
    
  • Move Dummy1.java into a de\train directory:

    > javac de\train\Dummy*.java
    > java de.train.Dummy1
    

people

See more on this question at Stackoverflow