Download Lecture Handout

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Further information:
How to be a programmer
http://samizdat.mines.edu/howto/HowToBeAProgrammer.html
New 2 Java
http://www.oracle.com/technetwork/topics/newtojava/overview/index.html
Online textbooks
Thinking in Java
http://www.mindview.net/Books/TIJ/
Introduction to Programming Using Java
http://math.hws.edu/javanotes/
Other recommended texts
Computer Ethics and Professional Responsibility
Edited by Terrell Ward Bynum and Simon Rogerson
http://southernct.edu/organizations/rccs/?p=603
Installing
So far we’ve been writing our own programs and running them ourselves,
usually from the command line. But what if you want to send or you’re
your files to a user and get them to install and run them themselves?
There are three parts to such a process:



Get the files to them.
Check they have the Java Runtime Environment (JRE) ie the JVM.
Let them run the program.
The first part is easy enough. The second is the hardest. All computer
programs suffer the same problem of needing to check that code which
helps the application is already on the machine.
Distribution:
The easy option is to zip everything up in a zip file with a text file,
traditionally called “Readme.txt”, containing instructions for getting the
JRE from the Java website and running the program with java.exe. You
can also include the JRE in the zip file, if you’re feeling generous, and
have checked the licence on the Oracle site covers you. The user then
uncompresses everything from the zip file and runs the program from the
command line. Zip files can contain a directory structure to install into,
and can be made “self extracting” – that is, they extract themselves
without needing other software. One good program for making these is
7Zip (http://www.7-zip.org/).
Checking the JRE:
The better way to check if they have the JDK is to use an Installer. These
check whether the JDK exists, install it if it doesn’t, put your files in a
user-chosen directory and add desktop icons etc. If you’ve ever installed a
program of any type, chances are you’ve used an Installer. Here are some
links to popular and/or free Installers…
Commercial:
http://www.installshield.com
http://www.zerog.com
Free
http://www.thefreecountry.com/programming/setup.shtml
http://www.freeprogrammingresources.com/installers.html
http://msdn.microsoft.com/en-us/library/Aa370566
Java only / Java-centred installers:
http://java-source.net/open-source/installer-generators
Install4J (Recommended: reasonably powerful and simple)
http://www.ej-technologies.com/products/install4j/overview.html
http://launch4j.sourceforge.net/ (free version)
Advanced Installer (Java walkthrough: Java version costs ~£150)
http://www.advancedinstaller.com/user-guide/tutorial-java.html
While many of these installers allow you to check whether the JRE is
already installed, however, if you are starting with installers, the easiest
thing is just to include the JRE in the install jar/application and run it just
for this application, or to check first, and if it is missing to do this. The JRE
license should be checked for your particular case, but includes the
following:
From:
http://www.oracle.com/technetwork/java/javase/terms/license/index.html
B. LICENSE TO DISTRIBUTE SOFTWARE. Subject to the terms and conditions of
this Agreement and restrictions and exceptions set forth in the README File,
including, but not limited to the Java Technology Restrictions and Limitations on
Redistribution of these Supplemental Terms, Oracle grants you a non-exclusive,
non-transferable, limited license without fees to reproduce and distribute the
Software, provided that (i) you distribute the Software complete and unmodified
and only bundled as part of, and for the sole purpose of running, your Programs,
(ii) the Programs add significant and primary functionality to the Software, (iii)
you do not distribute additional software intended to replace any component(s) of
the Software, (iv) you do not remove or alter any proprietary legends or notices
contained in the Software, (v) you only distribute the Software subject to a
license agreement that: (a) is a complete, unmodified reproduction of this
Agreement; or (b) protects Oracle's interests consistent with the terms contained
in this Agreement and that includes the notice set forth in Section H, and (vi) you
agree to defend and indemnify Oracle and its licensors from and against any
damages, costs, liabilities, settlement amounts and/or expenses (including
attorneys' fees) incurred in connection with any claim, lawsuit or action by any
third party that arises or results from the use or distribution of any and all
Programs and/or Software. The license set forth in this Section C does not extend
to the Software identified in Section G.
The README can be found here:
http://www.oracle.com/technetwork/java/javase/jre-7-readme430162.html
It largely lists files that may be ignored if you are installing the JRE just
for one application, allowing you to cut down the size. You can also find
details of how to cut down the size here:
http://weblogs.java.net/blog/stanleyh/archive/2005/05/deployment_unde
_1.html
Plus more details on the deployment of the JRE here:
http://java.sun.com/javase/6/webnotes/runtime.html
http://www.oracle.com/technetwork/java/javase/runtime-windows137915.html
http://docs.oracle.com/javase/1.5.0/docs/guide/deployment/deploymentguide/silent.html
Running the Program:
Ideally, rather than users having to open the command prompt and start
a program by sending it to the interpreter manually, we’d like our users to
be able to click on the application (or a desktop shortcut to it) and for it to
open. There are several ways of doing this.
1.
2.
3.
4.
Can
Can
Can
Can
get them to set this up as a shortcut themselves.
compile as an .exe, or wrap it in one.
use a self-executing jar file.
use a .bat file
These can all be done either automatically, or you can request the user do
them.
1) You can get an installation program which constructs a suitable
shortcut, for example containing text along the lines of
“d:/Program Files/javasoft/jre/bin/javaw MyClass”.
Note that javaw opens the JVM without the command line opening, ie it
runs invisibly (unless you use GUI elements, in which case these are
shown when you setVisible(true)).
2) Compile the program as native source code for each operating system,
and offer different versions (this will save you having to install the JRE as
well). The only problem with this is that you then have multiple versions
of the same program to test, and some OSs don’t have Java to Native
compilers. The Microsoft SDK for Java did include a program (jexegen) for
doing this for Windows. You might still be able to find the SDK at…
http://www.microsoft.com/downloads/results.aspx?freetext=java%20sdk
There are detailed instructions in the documentation. However, it looks
like this has been discontinued following the Microsoft-Sun lawsuit. There
is a list of more free compilers at:
http://en.wikipedia.org/wiki/Java_compiler
http://www.thefreecountry.com/compilers/java.shtml
Microsoft also very kindly supplied a program to convert Java to their
own-brand C# : the Java Language Conversion Assistant:
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=
14349
Also, you can use J#, if you are desperate to integrate into the Windows
COM environment
http://msdn2.microsoft.com/en-us/library/ms973842.aspx
3) Put the classes in a self-executing jar file. Jar files are just like zip files
except they have a Manifest File inside them, which is a text file
describing the packaging and directories to the JVM. One thing you can
set in this is the Class to start the application when the jar file is double
clicked on in a File Explorer. More details can be found in the Java tools
docs in the JDK under the jar.exe tool.
4) Give your users “wrapper” files that runs the program. An example file
for Windows is given below. This could be double clicked on to start the
program.
DOS/Windows file “start.bat” in the root of the directory tree for the package:
@echo off
rem This is the DOS wrapper script
rem Start the app, using the -classpath option
rem myPackage.MyClass is the package and Class.
javaw -classpath "%CLASSPATH%" myPackage.MyClass
@echo on
Many of the installers, for example Install4J, will create wrappers for jar
files that simply allow this to happen inside the wrapping .exe file. This
certainly seems a more elegant option.
Multithreadded program
Example of implementing Runnable.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Multithreadded program
Example of extending Thread.
listing 4
// Create a second thread by extending Thread
class NewThread extends Thread {
NewThread() {
// Create a new, second thread
super("Demo Thread");
System.out.println("Child thread: " + this);
start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
java.lang.System.arraycopy()
int [] fullArray = new int[0];
/**
* Adds an array of ints to the end of the array.<P>
* @param intArray : int array.
**/
public void addIntArray(int[] intArray) {
int oldLength = 0;
if (fullArray == null) {
fullArray = new int[intArray.length];
} else {
oldLength = fullArray.length;
int tempArray [] =
new int[oldLength];
System.arraycopy
(fullArray, 0, tempArray, 0, oldLength);
fullArray = new int[oldLength + intArray.length];
System.arraycopy
(tempArray, 0, fullArray, 0, oldLength);
}
}
for (int i = 0; i < intArray.length; i++)
fullArray [oldLength + i] = intArray[i];
}
/**
* Removes an int from the array.<P>
* @param index: index of int in array, starting with zero.
*/
public void removeInt(int index){
if (fullArray == null) return;
int[] tempArray = new int[fullArray.length];
System.arraycopy
(fullArray, 0, tempArray, 0, fullArray.length);
fullArray = new int[tempArray.length - 1];
System.arraycopy(tempArray, 0, fullArray, 0, index);
System.arraycopy(tempArray, index + 1, fullArray, index,
tempArray.length - (index + 1));
}
public static void arraycopy(Object src,
int src_position,
Object dst,
int dst_position,
int length)
Copies an array from the specified source array (src), beginning at the specified
position (src_position), to the specified position (dst_position) of the destination array
(dst). A subsequence of array components are copied from the source array
referenced by src to the destination array referenced by dst. The number of
components copied is equal to the length argument. The components at positions
src_position through src_position+length-1 in the source array are copied into
positions dst_position through dst_position +length-1, respectively, of the destination
array.
Given the following code, how would you add the int to the array of ints at the end?
To add a number to the end of the array, you have to copy the array into a temporary array of the
same type, ditch the old array in anything but name, by setting it equal to null, then copy the
temporary array back into it.
int[] intArray = {1, 2, 3, 4, 5};
int intValue = 6;
Write code to add the intValue to intArray at the end. If you manage this, have a go at
adding it in after the “3”. Remember that you call static methods using the Class
name, not an object, i.e.,
System.arraycopy(src, src_position, dst, dst_position, length);