0

I'm trying to import Bar.java into Foo.java, where my directory is structured like this:

.
└─bar
  └─Bar.java
└─foo
  └─Foo.java

So I create Bar.java:

package bar;
public class Bar {}

and Foo.java:

import bar.Bar;
public class Foo {}

I run javac from the foo directory:

javac Foo.java -cp ../bar

and receive the error:

Foo.java:1: error: package bar does not exist

My understanding was that the -cp option should be used to point towards the required class (Bar.java). Could someone shed some light on what I'm doing wrong?

Liam
  • 109
  • 3
  • 11
  • 2
    1) `-cp` is for compiled classes. You do not want to specify a class path but a *source path* 2) the path has to point to the root of the structure, not within the package’s directory. `javac -sourcepath .. Foo.java` or better, change the current directory to the root of the sources and use `javac -sourcepath . foo\Foo.java` – Holger Sep 22 '22 at 14:18
  • So if I also have `Bar.class` in the `bar` folder, how would I include that on the classpath when compiling `Foo.java`? – Liam Sep 22 '22 at 14:22
  • 1
    NO, it is not just Java19: using only `-cp` also works with Java 8 - but it must point to the parent directory of `bar` – user16320675 Sep 22 '22 at 14:25
  • @user16320675 you're right - pointing to the parent directory using `javac Foo.java -cp ..` works fine - I just don't really understand why... how can pointing to the root directory find the `bar` package, but pointing to the `bar` directory can't? – Liam Sep 22 '22 at 14:29

2 Answers2

1

Package bar does not exist because the prompt or terminal "working directory" is inside foo.

Easiest, make a folder java2 and put both folders in it and use use the storage folder as "system working directory" for terminal or prompt, However, normal "package" hierarchies compiled together are both in a top level starting directory tree.

E.g.

 /com/foo/ and /com/bar

So your package would look com.foo and com.bar Put the com folder in the java2(simply a name you can use any name for the storage folder)folder. Your import statement at the top of the class files should have the com directory added to import the other file. Just command inside java2 folder

javac /com/foo/Foo.java

-cp is for specifying to include dependent .jar libraries for the compilation.

If Foo does not import Bar then it will be required to be separately compiled the same way as Foo.

When framework API libraries are made its the user program requires to mark a starting point to unload the jar or look for a package folder and is traditionally "com", however, it does not distinguish which API until a folder lower so your package folder really should be structured

/com/mypersonalapi/foo /com/mypersonalapi/bar

com.mypersonalapi.foo com.mypersonalapi.bar

Samuel Marchant
  • 331
  • 2
  • 6
0

You need to give like this:

javac Foo.java -cp ..

When you give classpath as shown in your question, it expects the package "bar" under the folder "bar"; which is not intended.

Paulson
  • 1
  • 2