Asked  2 Years ago    Answers:  5   Viewed   5k times

So there's a folder /usr/share/stuff in the root directory

in stuff there are a bunch of java files with package definitions at the top

I am running javac where is in a subdomain

I added /usr/share/stuff to my class path.

and at the top of I add import

But I get a package does not exist error...why?



Are they in the right subdirectories?

If you put /usr/share/stuff on the class path, files defined with package should be in /usr/share/stuff/org/name.

EDIT: If you don't already know this, you should probably read this:

EDIT 2: Sorry, I hadn't realised you were talking of Java source files in /usr/share/stuff. Not only they need to be in the appropriate sub-directory, but you need to compile them. The .java files don't need to be on the classpath, but on the source path. (The generated .class files need to be on the classpath.)

You might get away with compiling them if they're not under the right directory structure, but they should be, or it will generate warnings at least. The generated class files will be in the right subdirectories (wherever you've specified -d if you have).

You should use something like javac -sourcepath .:/usr/share/stuff, assuming you've put the .java files that were under /usr/share/stuff under /usr/share/stuff/org/name (or whatever is appropriate according to their package names).

Tuesday, October 25, 2022

What you want is a relative import like:

from ..a.b import module

The problem with this is that it doesn't work if you are calling as your main module. As stated here:

Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "main", modules intended for use as the main module of a Python application should always use absolute imports.

So, if you want to call as your main module, then you should consider changing the structure of your modules and using an absolute import, else just use the relative import from above.

Saturday, August 27, 2022

This works:


package com.example.model;

public class BearExtra {
  public static void go() {
    System.out.println("Yay, it works!");


package com.example.web;

import com.example.model.*;

public class Bear {
  public static void main(String[] args) {

Now, to compile and run these classes, go to the directory where you can "see" the com folder and do:


javac -cp . com/example/model/*.java com/example/web/*.java
java -cp . com.example.web.Bear 


javac -cp . comexamplemodel*.java comexampleweb*.java
java -cp . com.example.web.Bear 

and the following is being printed to the console:

Yay, it works!
Sunday, September 25, 2022

I found the solution to my problem. I just needed to add the external libraries to the classpath, as described here:
Javadoc Documentation: classpath
Example: -classpath D:/folder/lib.jar

Tuesday, October 4, 2022

David, The location of the directories doesn't matter. It's the packages that matter. You can add multiple directories to your classpath when you compile/run the program to refer to these extra directories.

Friday, August 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages