• No se han encontrado resultados

MAPK FAMILY MEMBERS

5. IMMUNOSUPPRESSION

5.1. Immunophilins

This chapter provides notes and tutorials on JAR file tool, 'jar'. Topics include Java Archive (JAR) file forma, 'jar' command options, creating JAR files, managing JAR files with WinZIP, using manifest file, creating executable JAR files.

JAR - Java Archive File Format

'jar' - JAR File Tool Command and Options Creating the First JAR File - hello.jar Managing JAR Files with WinZIP

META-INF/MANIFEST.MF - JAR Manifest File Adding META-INF/MANIFEST.MF to JAR Files Using JAR Files in Java Class Paths

Creating Executable JAR Files Conclusions:

JAR files are ZIP files.

JAR files can have attributes stored in the META-INF/MANIFEST.MF file.

JAR files can be used in Java class paths.

JAR files can be "executable".

JAR - Java Archive File Format

JAR (Java Archive): A file format that compresses many files into a single package file. A JAR may contain additional package attributes and supporting data. It has some interesting features:

It uses the standard ZIP algorithm for compression.

Package attributes and supporting data are stored as files in a special directory called META-INF.

The main purpose of a JAR file is to aggregate your .class files, not your .java files, into a single file to distribute them to your customers. A JAR file can be directly included in the class path if you want to access those class files in the JAR file. No need to extract those class files into Java class directories.

Usually, JAR files are managed by the 'jar' tool provided in the JDK package.

But JAR files can also be managed by most ZIP tools, since JAR files are really ZIP files.

'jar' - JAR File Tool Command and Options

"jar": A command line tool for managing JAR files. "jar" is distributed as part of the Sun JDK package.

It has some interesting features:

It can create, update or extract a JAR file.

ZIP compression is optional.

"jar" command syntax:

Create jar file

jar c[v0M]f jarfile inputfiles

jar c[v0]mf manifest jarfile inputfiles Update jar file

jar u[v0M]f jarfile inputfiles

jar u[v0]mf manifest jarfile inputfiles Extract jar file

jar x[v]f jarfile [inputfiles]

List table of contents of jar file jar t[v]f jarfile [inputfiles]

where:

"c" - Creates a new JAR file.

"v" - Generates verbose output to standard output.

"0" - Specifies no compression.

"M" - Specifies no manifest file.

"f" - Specifies the JAR file name.

"m" - Specifies the manifest file name.

"u" - Updates a JAR file.

"x" - Extracts files out of a JAR file.

"t" - Displays the table of contents of a JAR file.

"jar" command is supported by the file, \j2sdk1.5.0\bin\jar.exe, if you installed JDK as in my previous chapter.

Creating the First JAR File - hello.jar

To create my first JAR file, I wrote the following Java file, Hello.java:

class Hello {

public static void main(String[] a) {

System.out.println("Hello world!");

} }

Here is what I did in a command window to create and extract my first JAR file, hello.jar:

C:\herong>javac Hello.java

C:\herong>jar cvf hello.jar Hello.class added manifest

adding: Hello.class(in = 416) (out= 285)(deflated 31%) C:\herong>jar tf hello.jar

META-INF/

META-INF/MANIFEST.MF Hello.class

>del Hello.class

C:\herong>jar xvf hello.jar created: META-INF/

extracted: META-INF/MANIFEST.MF extracted: Hello.class

C:\herong>dir Hello.class 416 Hello.class

C:\herong>type meta-inf\manifest.mf Manifest-Version: 1.0

Created-By: 1.4.2 (Sun Microsystems Inc.)

What happened here is that:

My first "jar" command created a new JAR file called hello.jar. "jar" automatically added

"manifest".

My second "jar" command displayed what is in hello.jar. As you can see, the first "jar"

command actually added a directory called META-INF into the jar file.

My third "jar" command extracted all files out of hello.jar.

My "type" command showed you what was added as "manifest": two package attributes:

version and create-by.

Managing JAR Files with WinZIP

The JAR specification says that "JAR file is a file format based on the popular ZIP file format". So are JAR files really ZIP files? Let's do some tests to find out.

Test 1. Create a JAR file, test.jar, with "jar" command. Rename test.jar to test.zip. Unzip test.zip with WinZIP. You should have no problem to extract all files out of test.zip with WinZIP.

Test 2. Create a ZIP file, tutu.zip, with WinZIP. Rename tutu.zip to tutu.jar. Extract files from tutu.jar with "jar". You should have no problem to extract all files out of tutu.jar with "jar" command.

Test 3. Create a password protected ZIP file, secure.zip, with WinZIP. Rename secure.zip to secure.jar.

Extract files from secure.jar with "jar". You should get a run time exception like this:

C:\herong>jar tf secure.jar

java.io.FileNotFoundException: secure.jar (The system cannot find the file specified)

at java.io.FileInputStream.open(Native Method)

at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66) at sun.tools.jar.Main.run(Main.java:185)

at sun.tools.jar.Main.main(Main.java:904)

So JAR files and ZIP files are fully compatible, as long as no encryptions are used on ZIP files.

I guess I don't need WinZIP anymore.

META-INF/MANIFEST.MF - JAR Manifest File

"manifest" in a JAR file is a file named as META-INF/MANIFEST.MF. It contains attributes about the JAR file and its contents.

Attributes in "manifest" are recorded as a list of name-value pairs in the form of "name: value\n". The

list is divided into a main section for package-level attributes and multiple sub sections for entry-level attributes. Note that each attribute must be ended with a new line character.

The main section may contain only the package level attributes like:

Manifest-Version

Created-By

Main-Class

Class-Path

Implementation-Vendor

Implementation-URL

Sub sections are optional. They are used to provide entry-level attributes with one section per entry. A sub sections must be preceded with a blank line and started with a "Name" attribute to associate this section with a content entry in the JAR file. Example of entry-level attributes are:

Content-Type

Java-Bean

Adding META-INF/MANIFEST.MF to JAR Files

There are two ways to add "manifest" to a JAR file.

1. Adding "manifest" through "jar" command line. Store all your manifest attributes in a file. And specify this file in the "jar" command line with the "m" option:

jar c[v0]mf manifest jarfile inputfiles

For example, I created my own manifest file called manifest.txt with one attribute in it:

Main-Class: Hello

Remember to press the <Enter> key at the end of the attribute. This will insert a new line character (\n) to terminate the attribute.

Here is how I added my manifest to a JAR file with the "m" option:

C:\herong>jar cvmf manifest.txt hello.jar Hello.class added manifest

adding: Hello.class(in = 416) (out= 285)(deflated 31%) C:\herong>jar xvf hello.jar

created: META-INF/

extracted: META-INF/MANIFEST.MF extracted: Hello.class

C:\herong>type META-INF\MANIFEST.MF Manifest-Version: 1.0

Created-By: 1.4.2 (Sun Microsystems Inc.) Main-Class: Hello

As you can see that "jar" command copied the attribute from my manifest file to the end of the auto-generated MANIFEST.MF file.

2. Adding "manifest" through META-INF/MANIFEST.MF file. Create META-INF/MANIFEST.MF as a text file. Enter all your manifest attributes in this file. And include META-INF/MANIFEST.MF as an input file to JAR file.

For example, I created my own META-INF/MANIFEST.MF with a text editor as:

Manifest-Version: 3.3 Created-By: Herong Yang Main-Class: Hello

Here is how I added my manifest to a JAR file with the "M" option:

C:\herong>jar cvMf tutu.jar Hello.class META-INF

adding: Hello.class(in = 416) (out= 285)(deflated 31%) adding: META-INF/(in = 0) (out= 0)(stored 0%)

adding: META-INF/MANIFEST.MF(in = 19) (out= 21)(deflated -10%) C:\herong>jar tf tutu.jar

Hello.class META-INF/

META-INF/MANIFEST.MF

Note that:

The "M" option stops "jar" to auto-generate the META-INF/MANIFEST.MF file.

When MANIFEST.MF is included as an input file, it was compressed. This is different than the auto-generated version, which was not compressed at all.

Using JAR Files in Java Class Paths

One advantage of aggregating individual class files into a JAR file is that other Java tools recognize JAR files as collections of class files and allow you to use them in the class paths.

To test this, I created the following class, TempratureConvertorBean.java:

/**

* TempratureConvertorBean.java

* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/

*/package herong;

public class TempratureConvertorBean { private double celsius = 0.0;

private double fahrenheit = 32.0;

public double getCelsius() { return celsius;

}

public void setCelsius(double c) { celsius = c;

fahrenheit = 1.8*c + 32.0;

}

public double getFahrenheit() { return fahrenheit;

}

public void setFahrenheit(double f) { fahrenheit = f;

celsius = (f-32.0)/1.8;

}

public String getInfo() {

return new String("My TempraturConvertorBean - Version 1.00");

} }

I did the following to create a JAR file, herong.jar:

C:\herong>mkdir cls

C:\herong>javac -d cls TempratureConvertorBean.java C:\herong>jar cvf herong.jar -C cls herong

added manifest

adding: herong/(in = 0) (out= 0)(stored 0%)

adding: herong/TempratureConvertorBean.class(in = 798) (out= 458)...

C:\herong>jar tf herong.jar META-INF/

META-INF/MANIFEST.MF herong/

herong/TempratureConvertorBean.class

I also created a testing class, F2C.java:

/**

* F2C.java

* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/

*/

import herong.TempratureConvertorBean;

public class F2C {

public static void main(String[] arg) {

TempratureConvertorBean b = new TempratureConvertorBean();

double f = 0.0;

if (arg.length>0) f = Double.parseDouble(arg[0]);

b.setFahrenheit(f);

double c = b.getCelsius();

System.out.println("Fahrenheit = "+f);

System.out.println("Celsius = "+c);

System.out.println(b.getInfo());

} }

Here is what I did to test using JAR files in a class path:

C:\herong>javac -classpath herong.jar F2C.java C:\herong>java -cp .;herong.jar F2C 70.0

Fahrenheit = 70.0

Celsius = 21.11111111111111

My TempraturConvertorBean - Version 1.00

This is nice. Right? I can take herong.jar to anywhere on any system. Just add it to "-classpath" for

"javac" command, and "-cp" for "java" command.

Creating Executable JAR Files

JAR files can be used to distribute supporting classes. They can also be used to distribute main classes

for stand-alone applications.

If you put a stand-alone application into a JAR file, you could make it executable by adding the "Main-Class" attribute to the manifest file.

Here is what I did to add my F2C class to herong.jar and make it executable.

First I created a manifest file, fs_f2c.txt:

Main-Class: F2C

Then I added F2C class to herong.jar:

C:\herong>javac -classpath herong.jar F2C.java C:\herong>jar uvmf fs_f2c.txt herong.jar F2C.class updated manifest

adding: F2C.class(in = 919) (out= 551)(deflated 40%) C:\herong>jar tf herong.jar

META-INF/

META-INF/MANIFEST.MF herong/

herong/TempratureConvertorBean.class F2C.class

Now herong.jar contains a small stand-alone application. But how to execute this JAR file? That's easy.

You can run a JAR file with the "java -jar" command like this:

C:\herong>java -jar herong.jar 60.0 Fahrenheit = 60.0

Celsius = 15.555555555555555

My TempraturConvertorBean - Version 1.00

Cool. Right? But you can make it even better. You can register JAR files to be automatically opened by the "java -jar" command on Windows system to make JAR files "truly" executable. Here is what I did to make this happen.

Warning: this following tutorial modifies Windows registry database. Please do not follow it if you don't have enough knowledge about Windows system.

1. Go to Start > Run. And enter "regedit" followed by clicking OK. Registry Editor window shows up.

2. Go to [HKEY_CLASSES_ROOT\jarfile\shell\open\command]. You should see the current value like this: ("C:\Program Files\Java\j2re1.4.2\bin\javaw.exe" -jar "%1" %*). This was added when you install JDK on your system.

3. Double click this value and change it to: ("C:\Program Files\Java\j2re1.4.2\bin\java.exe" -jar "%1"

%*).

4. Open a command window and try this:

C:\herong>herong.jar 50 Fahrenheit = 50.0

Celsius = 10.0

My TempraturConvertorBean - Version 1.00

Better. Right? You can run any JAR files by just typing their file names now.

You can also double click any JAR files on Window Explorer now. Try it and see what happens.

Chapter - 11