0

I am trying to run an application that uses an external library and I believe I have something missing in my setup. My project structure has the source code in the src directory, external libraries in lib and the resultant build in the bin directory. I have for purposes of this, just 2 files. The first one is in the src directory:

import decorations.*;

public class Christmas {

    public static void main(String[] args) {
        ChristmasBall xmas = new ChristmasBall("Happy Holidays");
        ChristmasBall blah = new ChristmasBall("merry Christmas");

        System.out.println(xmas.toString());
        System.out.println(blah.toString());
    }
}

The second file is in the lib folder of the project.

package decorations;

public class ChristmasBall {

    private String merry;

    public ChristmasBall(String message) {
        merry = message;
    }

    public String toString() {
        return merry;
    }
}

Its zipped up using the command from the lib directory: jar -cvf xmas.jar ChristmasBall.class

I get the following results:

added manifest
adding: ChristmasBall.class(in = 361) (out= 250)(deflated 30%)

Then I try to compile it and get the error that package decorations does not exist while using this command:

javac -Xlint -d ..\bin Christmas.java -cp ..\lib\xmas.jar

What am I missing?

codejunkie
  • 41
  • 6
  • You need the package structure in your jar, i.e. it should contain `decorations/ChristmasBall.class` – tgdavies Nov 10 '21 at 20:37
  • I'm not sure what you mean? 'package decorations;' is the first line in the class that is in the jar. – codejunkie Nov 10 '21 at 20:52
  • The class files in the jar need to be in a directory structure which matches the package hierarchy. – tgdavies Nov 10 '21 at 21:38
  • Does this answer your question? [How to create jar file with package structure?](https://stackoverflow.com/questions/18146361/how-to-create-jar-file-with-package-structure) – tgdavies Nov 10 '21 at 21:38
  • The best solution is to use maven to build your projects. – tgdavies Nov 10 '21 at 21:39
  • Your solution doesn't work. – codejunkie Nov 10 '21 at 22:27
  • What did you try, what error did you get, and what does `jar -tvf xmas.jar` say? – tgdavies Nov 10 '21 at 23:02
  • Wed Nov 10 16:21:38 CST 2021 META-INF/ 60 Wed Nov 10 16:21:38 CST 2021 META-INF/MANIFEST.MF 0 Wed Nov 10 16:21:34 CST 2021 decorations/ 0 Wed Nov 10 13:15:58 CST 2021 decorations/decorations/ 373 Wed Nov 10 13:15:58 CST 2021 decorations/decorations/ChristmasBall.class – codejunkie Nov 11 '21 at 14:34
  • Sadly, the answer is that the environment is so restricted that any attempt to define a classpath is denied. The result is to uncompress jar files into appropriate directories within the same directory as the source code. It also explains why runtime attempts to display classpath have the result of 'null'. – codejunkie Nov 11 '21 at 19:55

0 Answers0