Asked  6 Months ago    Answers:  5   Viewed   2.6k 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).

Friday, June 18, 2021
answered 6 Months ago

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.

Wednesday, July 21, 2021
answered 5 Months ago

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!
Friday, July 23, 2021
answered 5 Months ago

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

Sunday, August 22, 2021
Ouerghi Yassine
answered 4 Months ago

As far as i know (correct me if i am wrong), you cannot create a Class in a specific package using JShell (classes created within JShell are always in the default package).

That being said, you cannot access your package-private classes from within JShell. This is "normal" Java behaviour.

Saturday, October 2, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :