WWDC2000 Session 184
Transcript
Kind: captions
Language: en
my name is Blaine Garth I'm the manager
of one of the Java groups at apple and
what we're going to talk about today is
Java in depth so I want to just jump
into it I'm actually going to try to
have some fun today so hopefully you'll
have some fun with me we're going to try
to do a little bit a few demos and show
you what's really going on inside Mac
was 10 inside the Java Runtime
environment and stuff so the demo God's
struck early so at eight o'clock this
morning the system wouldn't boot off of
our boot disk so we got that figured out
at eight-thirty and at ten minutes to
eight we figured out that our login user
names weren't quite the same as what
they were the last time we ran the demo
so you know they're striking early but I
think by the time we get to that point
we'll have things pretty much in control
so so far at this conference you heard
from Stephan are off about the overview
of what we're doing where we've been
where we are now and some of what we're
trying to go where we're trying to go
you may have slipped over and listen to
the QuickTime talk after that found out
how to do really cool graphics
programming Annapolis 10 much as you do
19 you may have gone to Pete Dye hours
talk on how to cook up a job application
I'm not going to try to repeat very much
of those previous slides nor the
performance talk either but what I do
want to talk about is some of the stuff
that's that it did get talked about so
this is the talk where you get to ask
questions where we're going to try to
give you detail more detail more you
know type that when project filter
doesn't quite do what it's supposed to
be doing for you quite yet some of the
tricks we use or some of the things we
have to use in the interim until it does
so it's nine o'clock have you had your
Java yet pop quiz
what do the following screen shots all
have in common set of assistant the
first program that runs after you to act
was ten network manager when you find
yourself in a strange and unworldly NFS
environment this is program you run to
untangle yourself blast app source code
and a game that chips on your ships on
your CD a an applet perhaps the you're
kind of getting the idea here jbuilder
you saw that running yesterday it
unfortunately is not shipping on your CD
and you're supposed to go talk to Blake
if if you would like to see it shipping
on your CD sometime quicktime digital
media is i mentioned before and of
course the answer is they're all written
in java now this isn't supposed to be
all that remarkable is it but i think
it's really kind of neat java I mean
these applications are kind of like when
you walked up to Mac os10 they're like
the walls of apps I mean you know you
got a TV room you've got a kitchen
you've got your living room and stuff I
mean when you're inside the house and
Mac was ten running these applications
you don't really think about how they
run or you're not really supposed to but
for the folks of you inside this room
when the wiring is what is important to
you that is while you're here so what
we're going to talk about is what's
inside the walls of that house you know
the Java wiring in detail so as a brief
overview we're going to talk about the
wiring in certain groove depth we're
going to talk about once you get your
your app up and running and more or less
working you know how to run a little bit
of profiling stuff against it where we'd
like to go with that where we think
we'll be going with that how to do some
debugging right now both for your
benefit and possibly four hours and
there's a few things that you
due to augment your applications and so
we're going to try to go through a
little bit more detail on how to do that
in other words how to use Jay and I and
our kind of an environment how you have
to do today versus how we'd like to see
you do it and a few other topics so
let's just get going when you write your
job application what you think about are
the classes you right and of course when
the program is running you know the data
it takes up you know you can't just you
know Alec you know go and read data
forever because eventually despite what
we do in terms of memory reclamation
things like that if you're just leaking
memory by holding on to it forever you
know it's not going to work so the rest
of the system however is a little bit
more complex so what's under those
covers is the stuff that we generally
worry about at apple and we try to make
the bottom part of this diagram you know
as efficient as possible so I just like
to talk so so let me go through the
diagram first so of course you've got
you know the JDK class is all I can't
remember how many thousands of them
there are that you might be using you
know the runtime which does all that
synchronization and garbage collection
and compilation the compiler below that
on the side what I have in green are jni
support libraries both the ones that we
use to keep things going and I just put
two up there we have about nine and the
Jan I libraries that you might be
writing as well because you're bringing
across and legacy code and stuff like
that below if all or the frameworks that
are common to macula step things like
carbon things like the colonel things
like you know all that other stuff
that's below us that we all are clients
of now private data is a little thin
there it's actually private data extends
all the way down at the side of that
diagram in truth I would just want to
put up the sort of the Java private data
in this in this diagram but let's talk
about the job of the M first of all if
you were at peets talk yesterday you saw
that the Java VM framework is sort of
the fuse panel
or Java inside necklace 10 we have
inside that framework lots and lots of
stuff we have the virtual machines that
execute your java bytecodes we have all
of our system jars in the classes folder
things like that we have jan i support
libraries wedged into some other little
subdirectory we have headers because
this is a sea environment for those of
you writing jni code we have few headers
for that actually probably on DP for a
few more headers than we really need to
have out there so the ones you should be
thinking about her pretty much just jni
we have the Java home with the system
property file and you know there's a
whole bunch of stuff and I'll let you
wander around and take a look at that
but that is you know sort of the moral
equivalent to your NMR Jay programmer
the the mr j libraries folder so in DP
for i'll tell you the truth I'm really
happy you guys have hot spot well he
brought hot spot in just a few weeks
before we should be before we've been
working with hot spot for the year and a
half we had some demos running last year
at this time but keeping track tracking
what they're doing shifting to their c1
compiler writing a compiler for the
PowerPC takes a certain amount of time
and so what you find on your GP for CD
is actually the same environment that we
shipped in DP three as well the classic
interpreter plus the semantic jit
compiler so we engineered this such that
we could make a very easy switch over
and so there might be some times when
that's going to be of some advantage or
some use to you to do that in different
ways so the way we parameterize that is
we have a symbolic link now we all know
this is dump shock now you know this is
a UNIX system underneath
so symbolic links have some limited
purposes and this is one of the ones we
use it for everything that links against
the Java Runtime links against this
thing that's called lib JVM so we do
some trickery such that that's all that
any program that needs to be Jay and I
ever sees when it does a link that way
if it overcomes the path that you need
to switch your Java VM you can reset
that one symbolic point which the
Macintosh terms is you know an alias
really on on hfss file systems to be the
vm of your choice so we ship in some
ways three vm we ship hotspot client
compiler as the default vm we also have
a debug version of that same technology
and i'll tell you a little bit about
that later and again classic interpreter
so there are several ways to get at that
i really hope that you'd never have to
reset that symbolic link honestly
because we do it lots of ways to get to
it here are two of them one is you can
simply type dash classic or dash hotspot
in order to have the you know the jni
invitation code pick the right library
to go after load up and initialize lift
if you can't get to it via the command
line or that inconvenient for you you
can use a environment variable the J via
or Java jbm library environment variable
is also looked at and in that last line
this is the preferred way to get to the
say the debug version of hotspot now I
want to caution you the debug version of
hotspot is slower because it has
thousands of assertions compiled in and
so lots of C++ inlining that makes
things really fast is an inline and so
it's considerably slower but you're not
really care about that if you're at
stage and i'll tell you when you might
get to that stage so here's here's a
little bit about here's a little bit
about profiling the hot spot or the JDK
12 the the java to specification for the
runtime has a very nice interface it's
called the Java Virtual Machine
profiling interface and the
implementation of that for the classic
interpreter works pretty well but the
implementation for that in the hot spot
interpreter does not quite run there in
your GP for we are working really hard
with our folks and developer technical
support to figure out a pathway to get
you some updates to what's on your CD I
mean you're what you see what's the bits
that are on your CD or about two weeks
old so that's a lifetime for us so what
we have running up on stage and what you
see on your in your CDs is slightly
different it's our goal to try to get
you what we have on stage including some
project builder stuff out to you via the
web very soon so in the interim however
the hot spot itself is just chock full
of very interesting technology so one of
the things that makes hot spot really
good is that it chooses which methods
need be compiled on the fly and based on
their usage patterns which in methods
and the inline and ottoman line inside
of there in order to do that it has its
own sampling technology to figure out
hey what's going on so every now and
then clock ticks why's it goes and takes
it looks at the stacks and figures out
which methods are being interpreted and
so an interesting side effect of that is
that they've enabled that and sort of
the profiling kind of a
a manner such that you can get to that
using the dash X cross flag so we
actually have used that recently in the
past two weeks we've used that to
improve the launch time of a duty based
applications by two or three seconds so
what I'd like to do now is actually show
you a little bit of demo of that and i'm
going to have yen's al q join me on
stage now the end as a few demos cooked
up for us but if we could get there we
go good to do way this demo is going to
be in a terminal but not supposed to do
that but if you really the only way to
show off x cross right now so i have a
command line all set up here what I've
done is I've seeded into the directory
of one of the silly little sample
applets that you all know and love this
one of Arc test and I'm going to run
Java command I'm paranoid visited a demo
so I'm going to force it to use hotspot
and I'm using the magic dash X cross
flag and then the regular Java arguments
in this case just arc test an American
named Klaus so what's going to happen is
that it's going to launch our test
normally but every time it's bread
finishes it's going to spit out to the
console some profiling data about that
thread so as we launch our test which is
going to come up in the background
you'll first see the main thread finish
and it's dumped out some stuff there
here's our test do a little bit of stuff
with it we quit our test and then all
the other threads finish and we see
their profiling information get dumped
out too so what is this stuff it's
really pretty easy to interpret it
compared to the profiling out from JDK
11 it's not quite as in depth but it's
fairly easy to read so first we've got
the thread name here mane and first it's
going to show us the profiling data for
the stuff that got interpreted as
opposed to the methods that we're
running native says that the thread took
four point one three seconds
240 for ticks and this confused me at
first these are not text as in mac OS
tix which were sixtieth of a second tick
here just means samples so it depends on
how many times it sampled it and so we
spent eighteen point three percent of
the time that this thread would active
in j direct library OS X dot fine symbol
which is part of the implementation of j
direct three which actually is what goes
out and maps the native methods to the
actual carbon or system or whatever
functions that back them up and the
numbers here mean that we spent zero
seconds and slided interpreted Java code
zero ticks we're inside of interpreted
Java code 26 ticks were inside of native
code which makes sense we spent a whole
bunch of time loading carbon basically
is what this means and J direct looking
at carbon methods and doing some GN I've
stuff under the hood to plug them up
then we spend eleven point three percent
of our time down in view set geometry
which is part of the core clipping code
for heavyweight components and again
that with all spent down in native code
down below here the next group here is
for the same thread but this is compiled
code so this is time spent in actual
methods that have been jitter to
compiled into native code by hotspot and
you can see there's almost nothing here
the main thread just doesn't run very
long it doesn't look that much it's
mostly doing initialization stuff since
that's the thread where the Apple
actually creates the component and
initializes the toolkit and all that
stuff so these are a bunch of little
threads that aren't doing very much
stuff awt event queue 0 you'll find
generally where most of the real work is
happening in any java application that's
the event dispatch thread here we spent
twenty percent of our time down in CGS
graphics AWT to corpse draw a line and
so this is where we actually call into
corpse to draw lines and so you'd expect
ya in your civil kind of a WTF applets
where you're going to be spending a lot
of your time so if I said this is pretty
straightforward stuff it's not going to
give you the entire stack crawl of how
you got to this method where you were
spending all your time where the old
profiler did and it does have to be run
from the terminal but both of those
limitations are being worked on and I
think by the time we ship things will be
better like GM get the other side back
up ok so the traditional profiling stuff
inside the job vm h bra we actually
haven't run recently so on classic we
believe it works so we strongly advise
you to join that mailing list Makka lift
and Java that our DTS folks run allen
samuels will get up and tell you where
that's at again and join us and help
tell us what's what's there and what's
useful to you and share some tips on
that so we strongly believe that the jvm
p I is going to be a super interface
that third parties some of whom may be
sitting in this room right now we're
going to be able to take advantage of
right away we have talked with some
folks about this already and it's our
strong hope that we get some rather
awesome tools coming onto our platform
and the profiling interface lets you
want it fully operational tells you all
kinds of things about object allocation
stacks that were in existence when
object camel came along so it's a very
very powerful interface and we think
that this is going to bring development
on 10 a leap forward compared to some of
the key based environments where these
tools are or harder harder to accomplish
so just as in wearing up a house or
wiring up a circuit board or something
you have debugging to do so again what
you have in TP for is a classic
interpreter that we've been shipping for
a long time it has a debater interface
it works and with hot spot we have one
that almost works but it doesn't quite
and so our folks last week working hard
on getting the debugger interface up and
running unfortunately we didn't quite
get it where we needed to be for this
demo today so I can't quite show you jbm
di running on hot spot today but you can
expect that that will be there very
shortly and again we're going to try to
figure out how to get updates to you so
that you can see that as well so what's
on your disk is a debugger interface it
works in the classic environment so
again you have to go to the command line
to get use of that you have to go to jdv
to see that so you should be using jdb
dashti classic to fire up the runtime
and get to set breakpoints and that kind
of stuff obviously we don't think this
is the way you really ought to be doing
bugging and so we've been working with
folks on the project builder team to
make this a much more pleasant
environment and we do have some success
stories with that and I think engines
going to share one of those with you a
little bit later so the truth is you're
going to have to find out if you need to
do JTB level debugging you're going to
have to find out how to launch this
thing from the command line at the
moment so if you happen to write those
jni libraries then you're writing the
key and so it might happen that you
don't write perfectly the first time it
might happen that you'll be interested
in running GV on that part of your
program that's written in C that gets
called into across the jni interface in
which case you need to learn how to
launch jdb or GDP on the real job binary
so the real job of binary is kind of
wedged off in that little moral
equivalent of mr g libraries its wedged
in the java vm framework and so mentally
concatenate the first two lines of this
slide the font on the system or for the
slides was an all
so you essentially have to GD be the
true java you know bring up executable
in order to get attached to the current
to that process there are a few things
that you can do if you run with classic
you don't have to run question but if
you run with classic the next to line
the call dump thread and the call gut
monitors work and these are to some
degree the equivalents to some of the mr
JD commands that let you inside the
runtime and see what's going on and a
little bit more detail the last line is
simply gbb syntax for doing a back trace
on all the threads so that'll give you
some idea of what's going on which ones
have some of your code in the middle of
it so I'm a little bit I'm optimistic
that I'm also a realist so given that
the hot spot that we have oh now we're
going to skip the debugging demo right
now redevelop it later given that the
hot spot you have is actually fairly
young it's very mature technology and it
runs everything we can throw at it
except that it's not fully functional
but it's very solid but there might be
some occasions where you'll trip across
something that doesn't quite work right
so one thing you can do for us is help
you know and for yourselves that is
determine whether or not some strange
crash you see is your fault or ours so
if your Java fed crashes one thing that
you could do right away is just try
running it in classes since it's a whole
different technology underneath and it
may well work there that's not proof
positive that it's our fault but it's a
good signal that it might be so if your
code runs in classic but doesn't with
the default hotspot you can do us a
favor by running it with that debug
version of hotspot
because again there's lots of assertions
in there and if there's something funky
going on which might actually be your
fault if you're passing in say a zero
for a j-type environment pointer the
debug version of hotspots going to catch
that and throw an assertion whereas the
compiled version of that long or might
not so there are some cases where it'll
it might text some things that are your
fault and it certainly will catch things
earlier if they are our fault and so
obviously if you find something that is
our fault we want to know about it and
so I'm putting my email address up there
right now for that you can mail me the
details of that and I'll see that we get
some attention page of that clearly if
you're on our discussion with necklace
10 Java discussion list folks out there
might well want to know a little bit
about it also so again this is how you
set up the hotspot debug there is an
underbar between hotspot and debug it's
hardly visible on this monitor but
anyway so don't forget those under bars
they are important ok so again this is a
unix environment how many how many of
you out there are have been exclusively
mrj programmers okay there's just a few
hands and so let me try ask a different
question how many of you have already
programmed in sort of the unix or
windows environment the vast majority so
you all know what this is going to be
about okay so how do you build you know
Jay and I support library you write some
Java code with NATO's you compile it
with Java see you build the sea header
as necessary you can copy of the sea
header / is your template and start
hacking in your C code you build the
library about the only thing that's
different about this I'm a close Ted is
that the the ending library there are
two different types of libraries on Mac
os10 one's called a bundle and the
others called sort of several friends
lovers there are two types of dynamic
library
one called the bundle and the other is
called a dynamic library so we happen to
like the bundle variant of the libraries
for our purposes and so from a
command-line viewpoint this is a very
simple way to build one of those guys
who just remember the dash bundle magic
but I mean this is this is kind of messy
and what we really want to do is put all
the stuff inside a project builder and
so one of our friends on the project
builder team debuting has been stand
until midnight his time which is Denver
time trying to make this stuff happen so
I'd like to bring ends back up on stage
to see what some of this might look like
inside project builder for it belong
we get the monitor keep putting on both
screens this time whether that's easy or
not
I'm bringing up a pdx project showing a
very simple jni example that a plane
worth the jni example and then debuting
made the pdx project so I'm just here to
be Vanna White and show it off we have a
couple of files here we have calc engine
dot C which is our native code just
can't end a couple of jni methods I'm
not going to go into too much detail
there's not time the talks really
explained jni and I'm numbers you've
probably used it and there's some very
good books up there in a Java soft
tutorial calc Engine Java then the side
you probably can't read the text on here
anyway unless you're in the front row so
we have a couple of native methods in
here which are going to get bound into
that native code what's interesting in
here and this is just you know the
tantalizing promise because you can't
actually do this in DP for you're
getting used to that line is we have
three targets here we have the Java
target which has calc Engine Java source
file and we have a shell script here
that's been cleverly set up so we're
going to run the nfh and we're going to
run this script that calls user din java
h run that on calc engine so that's the
post processing step so after we compile
the job we were on Java H on it which
then generates the header file declaring
what native what the native jni
functions are then we have a second
target here for our jni library itself
which is a regular kind of c target
containing the calc engine via source
file the cal attention dot H file that
got generated by the Java target and
this one the product type here is a
library which is going to build a
regular OS 10 bundl dynamic library then
we have a third target and if you're
used to codewarrior this will be second
nature we have a third target Hulk Alec
example which is just an aggregate
target and the the other targets appear
down in here which means that they are
linked to it they're dependent on it
it's dependent on them so building the
calc example target will then build
first the Java target and then the jni
target and caused everything to be
generated and as blaine alluded to
earlier the demo gods were not happy we
sacrificed beefy giblets to them but
they really want to be tuna eggs and
became very angry so we can't actually
build this stuff it's actually more like
file permission issues but I would have
shown it running but you can just kind
of imagine that yes it's going to kind
of run and do it stuff we will ship this
demo project went in this fabled
upcoming release where we have a PBX
support for doing the DNI stuff and
that's it hmm did I say pdx ok so I
think one thing in there that you
probably may be noticed was that we had
like four methods that were wrapped like
add subtract divide multiply and I mean
that's what those are the names of C
functions and those were in fact the
names of the you know native methods
that we're going to give you access to
that so if you've been on mr j before
you know that that kind of a mapping is
like kind of tedious in some ways so
there is in mr j a better way to do that
and that has been called j direct and so
we have j direct three on necklace ten
that both helps us out and can help you
out with that so J direct three is
pretty much the same as datarow too from
your perspective not entirely the same
but pre dog on the same and that's due
to the heroic efforts of nick clegg zyk
put together a j directory package which
is pretty much entirely rewritten
underneath but from your perspective
it's not going to be that much different
but there i will go a little bit into
what is different about j directory
so along with the metaphor data rec 3 is
kind of like your virtual electrician
here you bring this electrician in and
he does all this wiring for you and you
don't have to be very much work at all
so if you've got gobs of existing code
or if you look under you and those see
frameworks that are below at all and
find see code that's kind of interesting
to you this has been true in the past
and it's probably may well still be true
the future and you happen to be a little
bit decaffeinated as a programmer and
don't want to write all those little
methods even if project builder makes it
just easy as as you can think the
solution is to let Jay direct possibly
write those see functions that glue
between the native and from the native
jni argument type implementation and the
sea functions that are being supplied to
you somewhere else so we use that for
the AWT as I said this is technology
that we've been using for a long time a
quick time also uses this technology and
so you know it's been there before we we
know about if you can you can you can
count on it as well so briefly how do
you how do you get this virtual
electrician to do its job so what it
needs is the name of the library that
contains those key functions you know
can't it's not going to go grab an NM
the system to find functions for you and
you need to tell it which class has
those native declaration that's a pretty
simple contract I tell it where the
library is tell it tell it what what
functions you weren't ready for on your
behalf and so you have to tell us in
these two ways you tell the name of the
library and the static string inside
that class that has those native methods
there are other options as well and then
you tell the clot even you hand the
class itself to the you know to the
constructor of this J direct function so
I actually have a little bit of code
here
show you how easy did you can read the
code right the first line is that string
J direct underbar mäkelä 10 as value
primed eyelids so presumably Prime has
this you know very fancy super calc
engine kind of technology underneath it
you tell you ask a direct and second
line second functional line to go ahead
and build those those stub functions so
you handed crime class now note that
this is the prime duck class itself so
you're just using that that fancy one
that one dot class directive to hand an
instance of or hand the class
description into J direct itself J
direct uses reflection to go find out
what methods to write native code for
and allocate some see memory rights and
key functions in that sea memory jams
them into a funky little jni object
itself and hands you back the handle to
all those gizmos in that thing called
that we store in linkage so you actually
need to hold on to that object that
linkage object because if you don't it
will be garbage collected you know if
it's garbage collected and you try to
access those functions well we're not
going to help you with that so the last
line of this is the actual method that
we're in this and this example tried to
do if I would have had more lines on
this slide I would have done the four
methods that were in the earlier example
so anyway j directory is in this example
absolutely simple to use so you ought to
note though that j directory is a rather
interesting set of technology so you can
have there's a slight difference in the
types of arguments you can use with it
in j directory you can use scalars and
array an array of scalars although
arrays of boolean dry understand aren't
quite there you cannot pass objects
however we don't have any way to really
map objects in any intelligent fashion
what we're doing under the covers is in
the case of arrays is we're actually
copying the bits out of the runtime into
a privately allocated memory rummaging
with it and then when we're done with
the copying it back in this is the
contract jni has for gaining access to a
rage so we haven't seen issues with that
even for quick time so it's just a note
for you as I mentioned earlier there are
different ways to name different
libraries if you put that same string
inside of an interface and then your
class implements that interface J direct
will do reflection on the interfaces and
find those strings and so you can have
several libraries that held the
implementations of a set of methods that
are described in one class you can write
code that runs on jay direct to in the
118 environment on mr j in that
environment we that we tune the run time
to look for a very magic interface and
so if you extended that interface we
knew how to we do to call the call the
invoker or call the callback sure you do
that that glue bindery we will be
writing a text note for that and so if
that's important to you then it is
possible that's not too bad again that
last line or told you about you have to
hold on to the results of that in order
to keep it working so a final note you
know one of those frameworks underneath
that might be interesting to you is the
carbon framework the toolbox has now
been carbonized and awt uses the
carbonized toolbox the contract there is
that you cannot go rummaging inside a
lot of carbon objects as you used to be
able to do on mr j so you have to use
the accessor functions just like
everybody else does so in this case we
have an example where you have to use
the accessor functions instead of going
off some magic offset and pulling out
the data yourself
finally our diagram again what happens
when you need to take an entire job
environment and put it inside another
application that is what we used to do
with Jay direct or Jay manager too and
we have a successor to that and we don't
have a name for it yet I guess it's the
embedding the story for mac OS 10 and
the person who can best speak to this is
none other than yes so I'll call him
back up to stage and see if he can run
you through some of these examples and
he will obviously speak more
knowledgeably about it than I will
thanks so we talked about wiring job
into your carbon application little
asterisk there because we would
eventually like this to be able to work
for cocoa applications as well but the
technology to do so isn't quite
available yet the first the bad news for
all you Jay manager fans Jay manager is
not supported on OS 10 the good news is
that well it's actually good news that
she mounted she's not supported because
it's a very complicated API it requires
you to do a lot of work in your
application everybody who did it ended
up doing it in kind of slightly
different ways and causing
incompatibilities for us and our
developers which is actually kind of our
fault in the design of the API so the
good news is that we have a new API and
the API is very simple because we now
have a modern we don't have a much more
modern environment to work in we don't
have the same kind of limitations we did
back when we did Jay direct to first
there are a couple of different pieces
to what you need to do first you need to
obviously get java running and you use
absolutely cross-platform standard jni
in fact the j no indication api to do
that you need to describe the kind of
code that is that you want to embed in
this kids talking about applets is that
the primary target is the web browser or
other application that can show applets
you can also use other kinds of code you
fill out a fairly simple struct with the
information about the location or the
tense of the applet then you create a
Java control or a job a window yes we
are using the control manager now so for
better for worse the control manager is
the sort of component story for 10 at
least in carbon and we have a job of
control that you do like going create
Java control and that is mostly all
there is to the story once the control
is there you need to be a good host to
control you need to make sure that your
controls and particularly the Java
control get all the events that they
need and I'll talk about that in more
detail in a minute we have a sort of
preview of this in DP for which is
mostly just a header file you can look
at and start thinking about this much
promised future add-on release will have
a functional implementation of it so the
first step as I said just use Jay and I
to start up the Java Virtual Machine and
this is exactly how you do it we we type
this in ourselves we could have torn it
out of sun's java tutorial or any of the
jni books that are out there at your
favorite computer bookstore first of all
we set up this Java VM in it arg
structure which is what tells the Java
Virtual Machine all the different
parameters it needs for starting up
things like the default heap size stack
size things like that and we don't care
about weird can use the default so we
fill in the version number and the
struct and call Jay and I get default
Java VM in it argh and that's going to
fill out the structure for us with the
default values and if you wanted to
tweak some of those values to increase
the heap size or something like that you
could do it after that then all we need
is Jay and I create Java VM that's going
to initialize the Java VM for us and
start it up and the it returns us a JVM
structure Java VM structure which is
rarely needed and a jay n IM structure
which is always needed for jni calls so
we need to save that one off second step
is making this magic struct that tells
our embedding code where the code you
want to run is
in this case we have a simple example of
where we're giving it a file path to a
local Apple if it's in our hard disk so
we create this emerging applet
descriptor struct we do a bit of core
foundation magic to create a URL given a
a unique style path and we null out the
rest of the structure if you wanted to
you could also use a real HTTP or
whatever URL to specify that you can
also provide a different code base and
doc base you can also pass in the
contents the raw contents of the applet
tag or a pre parse Set of apps of
properties the latter will be more
useful to you if you're a web browser
and you've already been parsing the HTML
the second step then is to take this mrj
code descriptor and stuff the Apple
descriptor into it applets being only
one of the ways that you can use to tell
which kind of code you want to embed
finally then we embed the control we
need a bounds rectangle that will fill
out and then we call a create Java
control passing in the jni environment
the window we want to control to go into
the bounding box and then the code
descriptor and as usual the last one is
an out parameter that actually returns
to control back to us and that's it so
behind the scenes the embedding library
is going to start up an applet context
go out and load the applet start up the
appropriate threads forward send at the
events and do some magic stuff with awt
so that the the frame that the applet
gets embedded into is actually the
control in a portion of your window so
you need to be able to run controls and
for the most part this is kind of Java
program or Mac programming 101 type of
stuff there's actually really no extra
work to handle windows that come up if
you if you want the Apple to come up in
a window or if the applet opens up its
own windows after the fact they simply
pop up as windows in your lair and the
nice thing is that carbons event manager
does all the work of making
sure the events for those windows go to
those windows and not into your event
loop so about all you have to do is make
sure that you don't step on them you
don't inadvertently when you're
traversing the window lists go and
assume that it's one of your windows and
you know dereference the rest con and
cast it to your struct and try to do
stuff with it just pay attention to
which windows are years and which ones
aren't the same deal goes for menus if
an AWP window is active it will append
menus to your menu bar and it will
remove them when it becomes inactive and
if you go around / versing the menu bar
for whatever reason you need to pay
attention to which menus belong to you
and which ones don't and there are some
access or functions in the API that will
help you determine whether or not this
menu where this window belongs to Java
for control you need to make sure that
all the relevant events are dispatched
to controls and in Mac programming 101
back in the nineteen eighty four days
the only kind of things that controls
really got were clicks because controls
were just limited things like buttons
and check boxes with the appearance
manager and the last couple years things
have gotten a lot more sophisticated and
controls canal they can now have focus
they can get key presses they can do
curse or tracking while the cursors over
them they can do drag and drop so there
are several new calls in the controls
dot H that are there to allow you to
pass those events into the control it
isn't that much more work to do there's
about three more places in your event
loop or you need to do the right kind of
control stuff and the appearance manager
SDK has all the sample codes for how to
do that sort of thing but the real real
way to do it the good way to do it is to
have your app adopt the new carbon event
model if you tens any of the sessions on
that yesterday you'll know that the
carbon event model is incredibly cool
and it's just a vast step forward for
the way Mac applications work the vents
are dispatched to the object that
they're intended for it's a lot more
like you're used to with Java
programming where if there's an activate
event for a window well the windows is
it the windows event handler will get
that event if there's an event for our
control the controls of that handle will
get that event there's no need for the
applications event loop even to get
involved
so you get all this stuff basically for
free for using the carbon of that model
you put the control on your window and
just runs itself at the bottom here I
have a couple a list of some of the new
appearance functions that you'll need to
use with an old style event loop create
route control handle control click etc
and if that's the way you're doing is
that I encourage you to go out and look
at the control manager documentation
that came out with the appearance
manager and with OS 8.5 to describe
pretty well in there so now I get to be
my own demo boy and run the demo back
here on this machine so I wrote it out
pretty quickly that shows off this stuff
and since I wanted to do pretty quickly
I used to carbon event model and what I
ended up with and probably a little too
small for you to read this is four pages
of code of which the first about two and
a half or some kind of generic stuff to
[Music]
create a new window add some event
handlers to the window create a text
controls we have a text field where you
can type in the applet path add some
listeners to that create a push-button
so we can push it to run the applet very
standard carbon event stuff we have a
window listener function the text
control listener function and then down
here we have the meat and do this all
fits on one screen with a little pipe we
first have
yeah down here we have the jni
indication stuff and this is almost
exactly as it appeared on the slide
earlier we call Jay and I get default vm
in it args and then Jay and I create
Java VM and again the stuff that shows
the applet is almost exactly as it
appears we create a CF string rep for
the path the CF URL ref and then we call
create Java control and again i'm mostly
showing the source code just to point
out how small this is the four pages of
code it took need a very short amount of
time to write and the application itself
looks like this so being at as carbon
events application we get all these
behaviors for free window dragging
window resizing etc text field runs we
will run draw test another one of these
silly sample applets push the Run button
and here's our apple
we can resize the window everything
redraws itself as you'd expect movable
this applet doesn't happen to do text
input that that works as well all in a
four page app so it think offense a lot
simpler than using J manager and as I
said in the past only a couple of
applications ever used a manager partly
because it was just rather intimidating
how to rather steep learning curve and
compatibility problems we hope that in
the future since this new API is so much
easier that more applications will think
about embedding Java thing to do though
one more thing yeah the debugging demo
did you plugin will not work for demo
gods were angry or we will not be able
to build the application so we can't run
it ah I tell you ours were the work
young re ready to swear in front of all
these people did debugging work I
solemnly swear that debugging worked we
were able to set breakpoints yay we were
even able to step through the code &
view stack traces it was good it truly
is good project builder which is the
intro session is going on right next to
us but you're not seeing you've seen a
little bit of project builder here
project folder is really looking good we
know how to hook up debugging inside of
there debuting is really doing a great
job making all that happen and we work
really hard to try to get that here to
show you because we honestly don't
believe that J to be is the way for you
guys go out into bug Java stock so well
there's nothing like if we don't have
worrying okay yeah we're all bow and
we're just totally living on the edge
now but you know if we fall down our
pants leave apologize real followed us
so if we can run this thing
it so Dave Seiss it's a great point now
you while you're running it's in project
builder I just hit run any kids how
would you guys yeah
ya need to put stuff
Java debugger see if there can see us
okay from do we have break points that
instead of waiting to connect so it's
the speaker for running hot spot isn't
it no yeah you're right sure that'll
help she's got a run job on orders of
the bug Java it turns out he were
loading it lazily that wasn't good was
it okay forget I tried it worked great
before ok back to the back to the side
ok so basically we know that we've been
postponing a lot of questions until now
so we're trying to leave a little bit of
time for some questions here so you
should remember Java is you know wired
into the walls of Mac OS 10 you
shouldn't be shocked at what you find on
DP for because we're going to make it
better and I'm going to point everybody
to Alan Samuel to make sure that you
know that he's the guy you need to ask
about all the updates I mean after him
so the take-home exam we had a pop quiz
at the beginning well I want to just
carry the metaphor going free to take
home right a Jay and I enable java class
debug your code using it profile your
code make sure it runs fast throw away
the jni stuff use j direct instead and
finally some some bedding is so simple i
mean it looks like almost I could
program that and well anyway I don't do
GUI so that it looks simple enough for
me to use so it ought to be simple
enough for you guys to use roadmap we
only got one other Java session that's
this year at wwc and immediately follows
this one so if you have marketing style
questions you have you know kudos for us
or non kudos for us take those to the
feedback forum to find out you know
where we fit in relationship to the rest
of the world of Java and things like
that for this session if you've got
technical questions about how something
happens and things like that what I'd
like to do is call up members of the
Java team you guys can hit the mics and
ask us anything else you want
you