Re: jar, package and import relationship?



Lew wrote:
Dan Stromberg wrote:
What's the relationship between jar files, package statements and import?

Specifically, what do I need to put into a .java that goes into a jar that has just an interface, in order to be able to import just the interface in another .java and build against that interface?

Could someone please post a concise example?
Could someone please provide a pithy list of steps like that for java, including the object orientation and (the separation of interface and implementation) and the jar creation?

Let's say your interface is in the source tree /ifacesrc/, and that the implementation is in /implsrc/, and that the implementation has an appropriate main() method.

cd /ifacesrc/
javac foo/bar/baz/if/TheInterface.java
jar cf iface.jar foo/bar/baz/if/TheInterface.class

plus a bunch of manifest steps best followed from the instructions at Sun:
<http://java.sun.com/javase/6/docs/technotes/tools/index.html>
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/jar.html>

cd /implsrc/
javac -cp .:/ifacesrc/iface.jar foo/bar/baz/Implementation.java
javac -cp .:/ifacesrc/iface.jar foo.bar.baz.Implementation

You might want to review the information about CLASSPATH and related issues:
<http://java.sun.com/docs/books/tutorial/essential/environment/paths.html>
<http://java.sun.com/javase/6/docs/technotes/tools/findingclasses.html>

Part 2: package

Interface:

package foo.bar.baz.if;

Implementation:

package foo.bar.baz;
import foo.bar.baz.if.TheInterface;

The import statement, of course, is optional. You can always refer to classes by their fully-qualified names (FQNs).

--
Lew
.



Relevant Pages

  • jar, package and import relationship?
    ... What's the relationship between jar files, ... what do I need to put into a .java that goes into a jar ... that has just an interface, in order to be able to import just the ... In python, I'd just do the following, including object orientation, but ...
    (comp.lang.java.programmer)
  • Re: Looking for Java metrics article and tool
    ... The article was about a method of analyzing the 'oo-ness'/coupling of a java ... tool itself scored a 100%:) If I recall correctly, ... The article explained the opinion of the author that a package in Java ... should either completely be an 'interface' (consisting of only interfaces ...
    (comp.object)
  • Re: Darkroom software Help
    ... Apple's computer operating system OS-X includes the Java ... programing language, compiler, and integration software. ... have an Apple this fall. ... Said that, my system is based on a commercial lab interface, the ...
    (rec.photo.darkroom)
  • Re: A C++ Whishlist
    ... with the C++ string classes as compared with the Java ones. ... You can keep you structs entirely ... The simple separation of interface and implementation that header files ...
    (comp.lang.cpp)
  • Looking for Java metrics article and tool
    ... The article was about a method of analyzing the 'oo-ness'/coupling of a java ... The article explained the opinion of the author that a package in Java ... should either completely be an 'interface' (consisting of only interfaces ... solve this was a staged initialization procedure. ...
    (comp.object)