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!
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!