jar, package and import relationship?

D

Dan Stromberg

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?

I googled for some time, but didn't find much joining the three worlds:
the java interface definition world, the filesystem world (including jar
creation) and the interface consumers (implementation and client of the
interface) world.

Following are two examples of something kind of similar to what I'm
hoping to see for java - one in python, and one in C:

In python, I'd just do the following, including object orientation, but
not including separation of interface and implementation:
1) Use a main program like:
#!/usr/bin/env python

import sys
sys.path.insert(0, '/my/directory')
import my_module

pub = my_module.hello(1)
pub.hello_world()

2) ...and then /my/directory/my_module.py would just be:
class hello:
def __init__(self, value):
self.variable = value

def hello_world(self):
print 'hello',self.variable
3) You don't really need a Makefile - you just run your main
program after chmod 755'ing it (on a Linux or UNIX system),
like:
chmod 755 main
./main

....and usually the implementation -is- the interface due to the duck
typing, except for identifiers that start with an underscore which are
usually "hands off" and may even be mangled to prevent external use.

For C, I'd do something like this, including the separation of interface
and implementation and library creation, but not including anything
object oriented or even object based:

1) /my/directory/interface.h has simply:
void hello_world();
2) implementation.c (in the current working directory) would look
like:
#include <stdio.h>

void hello_world()
{
printf("hello world\n");
}
3) ...and the main program (again in the CWD) would look like:
#include "interface.h"

int main()
{
hello_world();
return 0;
}
4) And a basic Makefile (again in the CWD) for this would look
like:
go: main
# run the program
./main

libimplementation.a: implementation.o
# create the library, analogous to a jar
rm -f libimplementation.a
ar cqv libimplementation.a implementation.o

main.o: main.c
# build the .o - analogous to a .class file
cc -c -I/my/directory main.c

main: libimplementation.a main.o
# link it all together
cc -o main main.o -L. -limplementation


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?

The make part is not important to me - ant or just a list of shell
commands would be good for that too.

Thanks!
 
L

Lew

Dan said:
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>
 
L

Lew

Lew said:
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).
 
M

Mark Space

Dan said:
sys.path.insert(0, '/my/directory')

This I don't really like. If you ever change the location of
/my/directory, you have to edit each source file where this appears and
alter the path. Maybe you've got a config file some place that you'd
use in real production code, but still it feels a bit ugly.

1) /my/directory/interface.h has simply:
void hello_world();

In Java, the .class files themselves have this information.
2) implementation.c (in the current working directory) would look

This would be equivalent to Implementation.java.
like:
#include <stdio.h>

Note that import and #include do different things. However both require
that you have a properly configured environment. #include requires a
correct INCLUDE path. Java does too: the CLASSPATH must be correct.
It's a bit different for anything that comes with the default with Java,
because the JVM handles those a bit differently with out environment
variables, but for any library (.jar) or .class file you download or
create, you need to tell your tools (IDE, javac compiler, etc.) where to
find them.

This is no different from C here. Totally the same idea.
void hello_world()
{
printf("hello world\n");
}

In file Hello_World.java:

public class Hello_World {
public static void hello_world() {
System.out.println("hello world");
}
}
3) ...and the main program (again in the CWD) would look like:
#include "interface.h"

int main()
{
hello_world();
return 0;
}

File Main.java:

public class Main {
public static void main(String ... args) {
Hello_World.hello_world();
}
}
4) And a basic Makefile (again in the CWD) for this would look
like:

Don't forget that C also requires your LIBPATH to be set correctly, for
any libraries which load at runtime. For Java, this is the same with
respect to any loose .class files that the runtime needs to find, or any
..jar files that you use. CLASSPATH is used for this as well.

(Remember that .jar files and .class files take the place of .h files in
C., so really it makes sense to just configure one CLASSPATH variable
rather than have one compile time include path and one separate LIBPATH
at runtime.)

From the command line:

Brenden@Homer ~/Dev/misc/hwtest/src
$ ls
Hello_World.java Main.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ javac Hello_World.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ javac Main.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ ls
Hello_World.class Hello_World.java Main.class Main.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ java -cp . Main
hello world

Brenden@Homer ~/Dev/misc/hwtest/src
$ java Main
hello world

Brenden@Homer ~/Dev/misc/hwtest/src
$ echo $CLASSPATH
..;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip



Note that the last two runs are equivalent because I have "." in my
classpath. Also, I haven't used packages, which is normally a bad idea.
But it's enough for this very simple example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top