Deployment via jars and Webstart

Download Report

Transcript Deployment via jars and Webstart

Deployment via jars and
Webstart
How do we distribute our
application?


Lab says you need to submit CD
Limitations of CD





fixed size
classes are fixed
java vm requirement fixed
Granny can't run it in DesMoine
Maybe Granny likes to save money and use
the library computer
(JAR) Java ARchive files


based on pkzip (rename from boo.jar to boo.zip
and it becomes a zip file)
holds:




class files
information about the contained files
can hold other stuff, like gifs!
Best of all



can be made executable
application runs out of the jar file
don't have to unjar (unpack) it
Example: SimpleGui.jar

I have a project, called Gift


c:\Gift is the directory
I have a package, called cs201

c:\Gift\cs201 is the directory containing:
SimpleGUI.java
 SimpleGUI.class
 Olsson001.gif

Jar options (complete list at end of
slides)

jar –cf SimpleGUI.jar cs201


jar –xf SimpleGUI.jar


create
extract
jar –tf SimpleGUI.jar

list contents
Make it executable


See slides at end
need to include a file (manifest) that tells
java where the main() method is

jar –cvmf manifest.txt SimpleGUI.jar cs201

m in –cvmf means, include the manifest file
(manifest.txt) in the jar
How to run the jar file



double-click it
or
java –jar SimpleGUI.jar
Trust

Java security based on the Sandbox



we'll let you have limited access to system
resources
you cannot read or write to local disk
you (usually) only read from the website from
which you are downloaded

can't attack other sites, e.g.
Increased security

Signing a jar file



signifies who created it
somewhat informal
you can spend $$ for a certificate

increased trust among your customers
Signing a jar

Full details at end of slides
1.
2.
3.
4.
5.
Create a key file
Add your keys to the file
Sign the jar file with your key
Only you can sign it this way
For greater trust, buy a certificate $$
What happens when you execute a
jar file?
java opens the jar file and looks in a directory
called META-INF
it finds the file MANIFEST.MF
1.
2.

this was created from your manifest.txt file
looks to see where the main() method is
3.




Main-Class: cs201.SimpleGUI
knows to look in the cs201 package directory (in the
jar file)
look for the class SimpleGUI
call the main() method inside of SimpleGUI
We can also include images,
sounds, in the jar file

Details shown at end:
1.
2.
put the file in your package
use a URL to access the file
WebStart - STDGOADK




Your app lives on the web
You download it to your machine
It executes on your machine
Makes sure your version is up to date


can fetch updates
get fetch newer versions of the runtime
system, etc.
Web Start overview





create executable jar file
sign it
create a JNLP file
create an html file
put all this on a website
JNLP – java network launching
protocol

web-centric


"codebase" is on web, not users disk
something like running:



java –classpath
http://www.people.virginia.edu/~cwm2n/SimpleGU
I.jar cs201.SimpleGui
uses a JNLP file to guide the fetch and run
provides secure running (unless your user
gives permission)
JNLP file








xml – oh boy
specifies jar file
the java runtime
any optional packages
system properties
URLs pointing to any resources (images, sounds,
etc)
name of html file pointing to us
our name (i.e., SimpleGUI.jnlp)
Need a web page, too
Either put into your index.html
or something as simple as:
<a href="SimpleGUI.jnlp">
Simple GUI WebStart Application
</a>
Put into the file reference by the jnlp file
put all of this on a web page

www.people.virginia.edu/~cwm2n




i.e., in the public_html directory of your home
directory file share
SimpleGUI.jar
SimpleGUI.jnlp
index.html
Run it via WebStart
or click on the link
www.people.virginia.edu/~cwm2n/SimpleGU
I.jnlp

WebStart
Including the .gif in your project

right click on the
project




import
file system
next
import from a
directory like
c:\localdata
importing a gif into project


you will see a list of
all files
select Olsson001.gif
importing a gif into project



This will put file in
your project.
You drag it to the
package
in this picture, I've
already done so

importing a gif into
project
jar files from Eclipse







export
jar file
select the resources
give it a name, like SimpleGUI.jar
next look at packaging options (doesn't matter)
next, generate a manifest file and specify the
main() class: e.g.:cs201.SimpleGui
click on finish
Jar options

Shown on next slides
Jar options

jar –cf SimpleGUI.jar cs201




-c means "create a jar"
f means "with the file name"
SimpleGUI.jar is the name of the jar file
cs201 is the package directory that will be put
into the jar
Jar options

jar –xf SimpleGUI.jar



-x means "extract a jar"
f means "with the file name"
SimpleGUI.jar is the name of the jar file
Will extract the contents of the jar file (i.e.,
create the cs201 package directory and all
the files therein)
Jar options

jar –tf SimpleGUI.jar



-x means "extract a jar"
t means "display the contents"
SimpleGUI.jar is the name of the jar file
Will list contents of jar file
Executable jar files with packages

assume same set of class directories


c:\Gift\cs201 is the package directory
go to top level directory


cd c:\Gift
create a manifest file, manifest.txt, that tells us where
the main() method is to be found



Main-class: cs201.SimpleGUI
put manifest.txt in c:\Gift
create the executable jar file

jar –cvmf manifest.txt SimpleGUI.jar cs201

m in –cvmf means, include the manifest file (manifest.txt) in
the jar
Signing a Jar file, part I

keys


keytool -genkey -keystore newKeys -alias team99




generates a keystore called myKeys and adds a holder cwm2n
prompt you for the password (echoes to screen)!
Your name, organization, city, state, etc.
keytool -selfcert -alias team99 -keystore newKeys




create a keystore to hold your keys
-selfcert – informal
-alias team99 who is signing this
-keystore where to put the keys
keytool -list -keystore newKeys

how to see your keys
Signing a Jar file, part II

use jarsigner

jarsigner -keystore myKeys SimpleGUI.jar
cwm2n
How to access gifs, sounds in the
jar file

Suppose is want to open a file in a method of
the class SimpleGUI



ImageIcon img= new
ImageIcon("Olsson001.gif");
Since jar file is running from the c:\Gift directory,
might not find the .gif file
Do we put Olsson001.gif in c:\Gift

if we do, and jar it up, this still does not work since
Olsson001.gif is not in the c:\Gift directory
How to access gifs, sounds in the
jar file

Package to the rescue!





put Olsson001.gif in cs201 package directory
change statement to:
ImageIcon img= new
ImageIcon("cs201/Olsson001.gif");
Now, java knows to look in the package
cs201 inside the jar file
Works!
How to access gifs, sounds in the
jar file

URL to the rescue!


keep Olsson001.gif in cs201 package directory
change statements to:





URL url = SimpleGUI.class.getResource("Olsson001.gif");
Image img= Toolkit.getDefaultToolkit().getImage(url);
need import java.net.*;
Now, java knows to look in the package cs201
inside the jar file
Works!
JNLP file specifics

line by line follows this slide
Line by line look at JNLP file
<?xml version "1.0" encoding="utf-8"?>

using xml version 1 and the encoding we'll
use
Line by line
<jnlp spec="1.0"
codebase="http://www.people.virginia.edu/~cw
m2n"
href="SimpleGUI.jnlp">



version of jnlp to use
codebase is where the code lives
last line is required, seems redundant but useful
if installed on desktop
Line by line
<information>
<title>Simple GUI App
</title>
<vendor>CS201 Teams Rule!
</vendor>
<homepage href="index.html"/>
<description>Simple GUI webstart demo
</description>
<offline-allowed/>
</information>
Line by line
<security>
<all-permissions/>
</security>
 Dangerous!

you are asking for carte blanche


read files, write files, read clipboard, etc.
user will allow this, even if you have written
poor code (and erase her disk!)
Line by line



<resources>
<j2se version="1.4"/>
<jar href="SimpleGUI.jar"/>
</resources>
which version of java to use
which executable jar file
can include things like sounds and images
Line by line
<application-desc
main-class="cs201.SimpleGUI"/>
Like the manifest file, tells which class has a
main() method
putting it all together
<?xml version "1.0" encoding="utf-8"?>
<jnlp spec="1.0"
codebase=
"http://www.people.virginia.edu/~cwm2n"
href="SimpleGUI.jnlp">
<information>
<title>Simple GUI App</title>
<homepage href="index.html"/>
<vendor>cwm2n</vendor>
<description>Simple GUI webstart
demo
</description>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.4"/>
<jar href="SimpleGUI.jar"/>
</resources>
<application-desc mainclass="cs201.SimpleGUI"/>
</jnlp>