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 org.name definitions at the top

I am running javac test.java where test.java is in a subdomain

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

and at the top of test.java I add import org.name

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

 Answers

97

Are they in the right subdirectories?

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

EDIT: If you don't already know this, you should probably read this: http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html#Understanding

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 test.java, 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
 
muaaz
answered 6 Months ago
47

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 test_file.py 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 test_file.py 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
 
Trott
answered 5 Months ago
94

This works:

com/example/model/BearExtra.java

package com.example.model;

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

com/example/web/Bear.java

package com.example.web;

import com.example.model.*;

public class Bear {
  public static void main(String[] args) {
    BearExtra.go();
  }
}

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

*nix/MacOS

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

Windows

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
 
seaders
answered 5 Months ago
69

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
72

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
 
AsGoodAsItGets
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 :  
Share