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

There is not a performance or overhead cost to doing import .* vs importing specific types. However, I consider it to be a best practice to never use import .* My primary reason for this is I just like to keep things straightward, clean and with as little ambiguity as possible, and I think with a .* import you lose that.

Monday, October 3, 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

Ideally, you shouldn't use a Spring Boot application (something that's been repackaged) as a dependency. From the documentation:

Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.

If the proposed solution isn't possible in your situation, the documentation goes on to describe an alternative:

If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.

To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as dependency.

To configure a classifier of exec in Maven, the following configuration can be used:

Thursday, November 24, 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 :