Frank-Rene Schäfer
Frank-Rene Schäfer

Reputation: 3352

java mysterious classpath behavior

I failed specifying the class path. Here's my setup:

File: "root/src/hello/German.java"

   package hello;
   public class German {
        public void greet() { System.out.println("Hallo"); }
   }

I compile this in "root":

> javac root/src/hello/German.java -d root/package

where "root/package/hello" exists as an empty directory. Fine. Now I want to test and write

File: "root/test/testHello.java"

import hello.German;
public class helloTest {
    public static void main(String[] args) {
        German guy = new German();
        guy.greet();
    }
}

I compile

> javac testHello.java -cp ../package

To summarize, I have:

  1. root/package/hello/German.class
  2. root/test/helloTest.class

I execute in "root/test/":

 > java testHello                      => class not found except.
 > java testHello -cp ../package       => class not found except.
 > java testHello -cp ../package/hello => class not found except.

However, copying the 'hello' directory into test such that there is

  1. root/test/hello/German.class
  2. root/test/helloTest.class

I can execute in "root/test/"

 > java testHello

and it greets friendly in German. I want to specify the classpath, though. But, I have no idea why '-cp' and '-classpath' is not accepted.

Upvotes: 2

Views: 68

Answers (1)

Julien Lopez
Julien Lopez

Reputation: 1854

Try this:

java -classpath .:../package testHello

.:../package to use the current directory and ../package as classpath.

Upvotes: 1

Related Questions