Формат JAR
Простое приложение “Привет калькулятор!”
Компиляция
Создание JAR файла
Создание JAR файла из другой директории
Использование JAR архива
Библиотека “Калькулятор!”
Компиляция и создание JAR архива
Простое приложение “Привет калькулятор!”
Компиляция и запуск
Создание JAR архива приложения и запуск
Манифест
Простое приложение “Привет калькулятор!”
Компиляция
Создадим манифест
Создание JAR архива приложения и запуск
Более простой способ задания точки входа
Автоматически добавленное поле Main-Class
Манифест
Библиотека “Калькулятор!”
Компиляция и создание JAR архива
Простое приложение “Привет калькулятор!”
Компиляция и запуск
Создадим манифест
Создание JAR архива приложения и запуск
1.07M
Категория: ПрограммированиеПрограммирование

Введение. JAR архивы (Java ARchive). (Тема 1.3)

1.

I. Введение
3. JAR архивы
1

2. Формат JAR

JAR (Java ARchive) – формат архива обычно используемый для того чтобы собрать
вместе class файлы Java и связанные с ними метаданные и ресурсы в один файл
для распространения приложения или библиотеки на платформе Java.
JAR файл позволяет эффективно развёртывать набор классов и связанных
ресурсов. Содержимое JAR файла может быть сжато, что вместе с возможностью
загрузить всё приложение или библиотеку за один раз делает загрузку JAR файла
более удобным чем загрузку большого количества несжатых файлов приложения
или библиотеки.
JAR файлы используют формат файлов ZIP и имеют расширение jar. Пользователи
могут создавать JAR архивы используя утилиту jar.exe которая поставляется в JDK.
Для создания JAR файлов можно использовать zip, но порядок записей в заголовке
zip файла важен, так как манифест должен идти первым.
2

3. Простое приложение “Привет калькулятор!”

package
package org.cud.hello;
org.cud.hello;
public
public class
class HelloCalc
HelloCalc {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
}}
}}
System.out.println("Hello
System.out.println("Hello Calculator");
Calculator");
Calculator
calculator
=
new
Calculator calculator = new Calculator();
Calculator();
System.out.println("2+3+4
System.out.println("2+3+4 == "" ++ calculator.sum(2,
calculator.sum(2, 3,
3, 4));
4));
class
class Calculator
Calculator {{
public
public int
int sum(int...
sum(int... numbers)
numbers) {{
int
int total
total == 0;
0;
}}
}}
for
for (int
(int ii :: numbers)
numbers) {{
total
total +=
+= i;
i;
}}
return
return total;
total;
3

4. Компиляция

G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
src\org\cud\hello
src\org\cud\hello
in
in drive
drive GG has
has no
no label.
label.
Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\src\org\cud\hello
G:\src\org\cud\hello
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
05:02
<DIR>
..
05:02 PM
PM
<DIR>
05:02
<DIR>
..
05:02 PM
PM
<DIR>
..
05:17
PM
420
HelloCalc.java
05:17 PM
420 HelloCalc.java
11 File(s)
420
File(s)
420 bytes
bytes
22 Dir(s)
Dir(s) 42,080,108,544
42,080,108,544 bytes
bytes free
free
G:\>javac
G:\>javac -d
-d bin
bin src\org\cud\hello\HelloCalc.java
src\org\cud\hello\HelloCalc.java
G:\>dir
G:\>dir bin\org\cud\hello
bin\org\cud\hello
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin\org\cud\hello
G:\bin\org\cud\hello
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
05:18
<DIR>
..
05:18 PM
PM
<DIR>
05:18
<DIR>
..
05:18 PM
PM
<DIR>
..
05:20
369
05:20 PM
PM
369 Calculator.class
Calculator.class
05:20
PM
760
HelloCalc.class
05:20 PM
760 HelloCalc.class
22 File(s)
1,129
File(s)
1,129 bytes
bytes
22 Dir(s)
Dir(s) 42,080,108,544
42,080,108,544 bytes
bytes free
free
G:\>
G:\>
4

5. Создание JAR файла

G:\bin>dir
G:\bin>dir
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin
G:\bin
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:15
<DIR>
..
11:15 AM
AM
<DIR>
11:15
<DIR>
..
11:15 AM
AM
<DIR>
..
09:42
AM
<DIR>
org
09:42 AM
<DIR>
org
00 File(s)
00 bytes
File(s)
bytes
33 Dir(s)
Dir(s) 42,079,842,304
42,079,842,304 bytes
bytes free
free
G:\bin>jar
G:\bin>jar cfv
cfv hello.jar
hello.jar org
org
added
manifest
added manifest
adding:
adding: org/(in
org/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/(in
org/cud/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/hello/(in
org/cud/hello/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/hello/Calculator.class(in
org/cud/hello/Calculator.class(in == 369)
369) (out=
(out= 301)(deflated
301)(deflated 18%)
18%)
adding:
org/cud/hello/HelloCalc.class(in
=
760)
(out=
460)(deflated
39%)
adding: org/cud/hello/HelloCalc.class(in = 760) (out= 460)(deflated 39%)
G:\bin>dir
G:\bin>dir
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin
G:\bin
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:15
<DIR>
..
11:15 AM
AM
<DIR>
11:15
<DIR>
..
11:15 AM
AM
<DIR>
..
11:15
AM
1,688
hello.jar
11:15 AM
1,688 hello.jar
09:42
AM
<DIR>
org
09:42 AM
<DIR>
org
11 File(s)
1,688
File(s)
1,688 bytes
bytes
33 Dir(s)
Dir(s) 42,079,838,208
42,079,838,208 bytes
bytes free
free
G:\bin>
G:\bin>
5

6. Создание JAR файла из другой директории

G:\>dir
G:\>dir
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\
G:\
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:16
<DIR>
bin
11:16 AM
AM
<DIR>
bin
09:41
<DIR>
src
09:41 AM
AM
<DIR>
src
00 File(s)
0
File(s)
0 bytes
bytes
22 Dir(s)
Dir(s) 42,079,825,920
42,079,825,920 bytes
bytes free
free
G:\>jar
G:\>jar cfv
cfv hello.jar
hello.jar -C
-C bin
bin org
org
added
added manifest
manifest
adding:
adding: org/(in
org/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/(in
org/cud/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/hello/(in
org/cud/hello/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/hello/Calculator.class(in
org/cud/hello/Calculator.class(in == 369)
369) (out=
(out= 301)(deflated
301)(deflated 18%)
18%)
adding:
adding: org/cud/hello/HelloCalc.class(in
org/cud/hello/HelloCalc.class(in == 760)
760) (out=
(out= 460)(deflated
460)(deflated 39%)
39%)
G:\>dir
G:\>dir
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\
G:\
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:16
<DIR>
bin
11:16 AM
AM
<DIR>
bin
11:20
1,688
11:20 AM
AM
1,688 hello.jar
hello.jar
09:41
<DIR>
src
09:41 AM
AM
<DIR>
src
11 File(s)
1,688
File(s)
1,688 bytes
bytes
22 Dir(s)
42,079,821,824
bytes
Dir(s) 42,079,821,824 bytes free
free
G:\>
G:\>
6

7. Использование JAR архива

G:\>dir
G:\>dir
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\
G:\
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:16
<DIR>
bin
11:16 AM
AM
<DIR>
bin
11:20
1,688
11:20 AM
AM
1,688 hello.jar
hello.jar
09:41
AM
<DIR>
src
09:41 AM
<DIR>
src
11 File(s)
1,688
File(s)
1,688 bytes
bytes
22 Dir(s)
Dir(s) 42,079,821,824
42,079,821,824 bytes
bytes free
free
G:\>java
G:\>java -classpath
-classpath hello.jar
hello.jar org.cud.hello.HelloCalc
org.cud.hello.HelloCalc
Hello
Calculator
Hello Calculator
2+3+4
2+3+4 == 99
G:\>
G:\>
7

8.

Библиотека
8

9. Библиотека “Калькулятор!”

package
package org.cud.calc;
org.cud.calc;
public
public class
class Calculator
Calculator {{
public
public int
int sum(int...
sum(int... numbers)
numbers) {{
int
int total
total == 0;
0;
}}
}}
for
for (int
(int ii :: numbers)
numbers) {{
total
total +=
+= i;
i;
}}
return
return total;
total;
9

10. Компиляция и создание JAR архива

G:\>javac
G:\>javac -d
-d bin
bin src\org\cud\calc\Calculator.java
src\org\cud\calc\Calculator.java
G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
bin\org\cud\calc
bin\org\cud\calc
in
in drive
drive GG has
has no
no label.
label.
Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin\org\cud\calc
G:\bin\org\cud\calc
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:54
<DIR>
..
11:54 AM
AM
<DIR>
11:54
<DIR>
..
11:54 AM
AM
<DIR>
..
11:58
369
11:58 AM
AM
369 Calculator.class
Calculator.class
11 File(s)
369
File(s)
369 bytes
bytes
22 Dir(s)
Dir(s) 42,079,776,768
42,079,776,768 bytes
bytes free
free
G:\>jar
G:\>jar -cfv
-cfv bin/calculator.jar
bin/calculator.jar -C
-C bin
bin ..
added
added manifest
manifest
adding:
adding: calculator.jar(in
calculator.jar(in == 111)
111) (out=
(out= 64)(deflated
64)(deflated 42%)
42%)
adding:
adding: org/(in
org/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
org/cud/(in
=
0)
(out=
0)(stored
adding: org/cud/(in = 0) (out= 0)(stored 0%)
0%)
adding:
org/cud/calc/(in
=
0)
(out=
0)(stored
adding: org/cud/calc/(in = 0) (out= 0)(stored 0%)
0%)
adding:
adding: org/cud/calc/Calculator.class(in
org/cud/calc/Calculator.class(in == 369)
369) (out=
(out= 300)(deflated
300)(deflated 18%)
18%)
G:\>
G:\>
10

11. Простое приложение “Привет калькулятор!”

package
package org.cud.hello;
org.cud.hello;
import
import org.cud.calc.Calculator;
org.cud.calc.Calculator;
public
public class
class HelloCalc
HelloCalc {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
}}
}}
System.out.println("Hello
System.out.println("Hello Calculator");
Calculator");
Calculator
Calculator calculator
calculator == new
new Calculator();
Calculator();
System.out.println("2+3+4
System.out.println("2+3+4 == "" ++ calculator.sum(2,
calculator.sum(2, 3,
3, 4));
4));
11

12. Компиляция и запуск

G:\>javac
G:\>javac -d
-d bin
bin -cp
-cp bin\calculator.jar
bin\calculator.jar src\org\cud\hello\HelloCalc.java
src\org\cud\hello\HelloCalc.java
G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
bin\org\cud\hello
bin\org\cud\hello
in
in drive
drive GG has
has no
no label.
label.
Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin\org\cud\hello
G:\bin\org\cud\hello
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:54
<DIR>
..
11:54 AM
AM
<DIR>
11:54
<DIR>
..
11:54 AM
AM
<DIR>
..
12:08
759
12:08 PM
PM
759 HelloCalc.class
HelloCalc.class
11 File(s)
759
File(s)
759 bytes
bytes
22 Dir(s)
Dir(s) 42,079,776,768
42,079,776,768 bytes
bytes free
free
G:\>dir
G:\>dir bin
bin
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin
G:\bin
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:58
<DIR>
..
11:58 AM
AM
<DIR>
11:58
<DIR>
..
11:58 AM
AM
<DIR>
..
11:59
1,972
11:59 AM
AM
1,972 calculator.jar
calculator.jar
11:54
AM
<DIR>
org
11:54 AM
<DIR>
org
11 File(s)
1,972
File(s)
1,972 bytes
bytes
33 Dir(s)
Dir(s) 42,079,776,768
42,079,776,768 bytes
bytes free
free
G:\>java
G:\>java -cp
-cp bin;bin\calculator.jar
bin;bin\calculator.jar org.cud.hello.HelloCalc
org.cud.hello.HelloCalc
Hello
Calculator
Hello Calculator
2+3+4
2+3+4 == 99
G:\>
G:\>
12

13. Создание JAR архива приложения и запуск

G:\>jar
G:\>jar -cfv
-cfv bin/hello.jar
bin/hello.jar -C
-C bin
bin org\cud\hello\HelloCalc.class
org\cud\hello\HelloCalc.class
added
added manifest
manifest
adding:
adding: org/cud/hello/HelloCalc.class(in
org/cud/hello/HelloCalc.class(in == 759)
759) (out=
(out= 461)(deflated
461)(deflated 39%)
39%)
G:\>dir
G:\>dir bin
bin
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin
G:\bin
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
12:14
<DIR>
..
12:14 PM
PM
<DIR>
12:14
<DIR>
..
12:14 PM
PM
<DIR>
..
11:59
1,972
11:59 AM
AM
1,972 calculator.jar
calculator.jar
12:16
PM
956
hello.jar
12:16 PM
956 hello.jar
11:54
<DIR>
org
11:54 AM
AM
<DIR>
org
22 File(s)
2,928
File(s)
2,928 bytes
bytes
33 Dir(s)
Dir(s) 42,079,768,576
42,079,768,576 bytes
bytes free
free
G:\>java
G:\>java -cp
-cp bin\hello.jar;bin\calculator.jar
bin\hello.jar;bin\calculator.jar org.cud.hello.HelloCalc
org.cud.hello.HelloCalc
Hello
Calculator
Hello Calculator
2+3+4
2+3+4 == 99
G:\>
G:\>
13

14.

Манифест
14

15. Манифест

JAR файл может содержать манифест – файл META-INF/MANIFEST.MF. Записи в
манифесте определяют как JAR файл может использоваться. Каждая запись
находится на отдельной строке и состоит из пары заголовок, значение разделённых
двоеточием. Запись Classpath: может использоваться для указания других JAR
файлов для загрузки с текущим JAR файлом. Эта запись состоит из списка
абсолютных или относительных путей к другим JAR файлам. Запись Main-Class:
может использоваться для указания точки входа приложения для создания
исполняемых JAR файлов.
Хотя запись Classpath: в манифесте должна была упростить использование JAR
файлов, на практике её использование оказалось очень хрупким, поскольку
расположения JAR файлов не должно меняться.
15

16.

Исполняемые JAR архивы
16

17. Простое приложение “Привет калькулятор!”

package
package org.cud.hello;
org.cud.hello;
public
public class
class HelloCalc
HelloCalc {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
}}
}}
System.out.println("Hello
System.out.println("Hello Calculator");
Calculator");
Calculator
calculator
=
new
Calculator calculator = new Calculator();
Calculator();
System.out.println("2+3+4
System.out.println("2+3+4 == "" ++ calculator.sum(2,
calculator.sum(2, 3,
3, 4));
4));
class
class Calculator
Calculator {{
public
public int
int sum(int...
sum(int... numbers)
numbers) {{
int
int total
total == 0;
0;
}}
}}
for
for (int
(int ii :: numbers)
numbers) {{
total
total +=
+= i;
i;
}}
return
return total;
total;
17

18. Компиляция

G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
src\org\cud\hello
src\org\cud\hello
in
in drive
drive GG has
has no
no label.
label.
Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\src\org\cud\hello
G:\src\org\cud\hello
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
05:02
<DIR>
..
05:02 PM
PM
<DIR>
05:02
<DIR>
..
05:02 PM
PM
<DIR>
..
05:17
PM
420
HelloCalc.java
05:17 PM
420 HelloCalc.java
11 File(s)
420
File(s)
420 bytes
bytes
22 Dir(s)
Dir(s) 42,080,108,544
42,080,108,544 bytes
bytes free
free
G:\>javac
G:\>javac -d
-d bin
bin src\org\cud\hello\HelloCalc.java
src\org\cud\hello\HelloCalc.java
G:\>dir
G:\>dir bin\org\cud\hello
bin\org\cud\hello
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin\org\cud\hello
G:\bin\org\cud\hello
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
11/13/2012
05:18
<DIR>
..
05:18 PM
PM
<DIR>
05:18
<DIR>
..
05:18 PM
PM
<DIR>
..
05:20
369
05:20 PM
PM
369 Calculator.class
Calculator.class
05:20
PM
760
HelloCalc.class
05:20 PM
760 HelloCalc.class
22 File(s)
1,129
File(s)
1,129 bytes
bytes
22 Dir(s)
Dir(s) 42,080,108,544
42,080,108,544 bytes
bytes free
free
G:\>
G:\>
18

19. Создадим манифест

Main-Class:
Main-Class: org.cud.hello.HelloCalc
org.cud.hello.HelloCalc
19

20. Создание JAR архива приложения и запуск

G:\>dir
G:\>dir
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\
G:\
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
12:50
<DIR>
bin
12:50 PM
PM
<DIR>
bin
01:11
39
01:11 PM
PM
39 manifest.mf
manifest.mf
12:48
<DIR>
src
12:48 PM
PM
<DIR>
src
11 File(s)
39
File(s)
39 bytes
bytes
22 Dir(s)
Dir(s) 42,079,703,040
42,079,703,040 bytes
bytes free
free
G:\>jar
G:\>jar cvmf
cvmf manifest.mf
manifest.mf bin/hello.jar
bin/hello.jar -C
-C bin
bin org
org
added
added manifest
manifest
adding:
adding: org/(in
org/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/(in
org/cud/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
org/cud/hello/(in
=
0)
(out=
0)(stored
adding: org/cud/hello/(in = 0) (out= 0)(stored 0%)
0%)
adding:
adding: org/cud/hello/Calculator.class(in
org/cud/hello/Calculator.class(in == 369)
369) (out=
(out= 301)(deflated
301)(deflated 18%)
18%)
adding:
adding: org/cud/hello/HelloCalc.class(in
org/cud/hello/HelloCalc.class(in == 760)
760) (out=
(out= 460)(deflated
460)(deflated 39%)
39%)
G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
bin
bin
in
in drive
drive GG has
has no
no label.
label.
Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin
G:\bin
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
12:50
<DIR>
..
12:50 PM
PM
<DIR>
12:50
<DIR>
..
12:50 PM
PM
<DIR>
..
01:30
1,722
01:30 PM
PM
1,722 hello.jar
hello.jar
12:48
<DIR>
org
12:48 PM
PM
<DIR>
org
11 File(s)
1,722
File(s)
1,722 bytes
bytes
33 Dir(s)
42,079,703,040
bytes
Dir(s) 42,079,703,040 bytes free
free
G:\>java
G:\>java -jar
-jar bin/hello.jar
bin/hello.jar
Hello
Hello Calculator
Calculator
2+3+4
2+3+4 == 99
20

21. Более простой способ задания точки входа

G:\>javac
G:\>javac -d
-d bin
bin
src\org\cud\hello\HelloCalc.java
src\org\cud\hello\HelloCalc.java
G:\>jar
G:\>jar cfev
cfev bin/hello.jar
bin/hello.jar org.cud.hello.HelloCalc
org.cud.hello.HelloCalc -C
-C bin
bin org
org
added
manifest
added manifest
adding:
adding: org/(in
org/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
(out=
adding: org/cud/(in
org/cud/(in == 0)
0)Исполняемый
(out= 0)(stored
0)(stored
0%)
JAR 0%)
архив содержит манифест в котором указана
adding:
adding: org/cud/hello/(in
org/cud/hello/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
входа
приложения
т.е.
полное301)(deflated
квалифицированное имя класса
adding:
adding: org/cud/hello/Calculator.class(in
org/cud/hello/Calculator.class(in == 369)
369) (out=
(out= 301)(deflated 18%)
18%)
содержащего
метод
public
static
void
main(String[]
args).
adding:
org/cud/hello/HelloCalc.class(in
=
760)
(out=
460)(deflated
39%)
adding: org/cud/hello/HelloCalc.class(in = 760) (out= 460)(deflated 39%)
точка
G:\>java
G:\>java -jar
-jar bin/hello.jar
bin/hello.jar
Для создания исполняемого JAR архива можно использовать два
Hello
Hello Calculator
Calculator
подхода: указать файл с манифестом или непосредственно указать
2+3+4
2+3+4 == 99
G:\>
G:\>
точку входа приложения.
21

22. Автоматически добавленное поле Main-Class

Manifest-Version:
Manifest-Version: 1.0
1.0
Created-By:
1.6.0_35
Created-By: 1.6.0_35 (Sun
(Sun Microsystems
Microsystems Inc.)
Inc.)
Main-Class:
org.cud.hello.HelloCalc
Main-Class: org.cud.hello.HelloCalc
22

23.

Работа с classpath
23

24. Манифест

По умолчанию classpath включает текущую директорию. Дополнительные
директории можно указать используя запись Classpath: в манифесте.
При использовании исполняемых JAR файлов опция –cp или –classpath утилиты
java игнорируется.
24

25. Библиотека “Калькулятор!”

package
package org.cud.calc;
org.cud.calc;
public
public class
class Calculator
Calculator {{
public
public int
int sum(int...
sum(int... numbers)
numbers) {{
int
int total
total == 0;
0;
}}
}}
for
for (int
(int ii :: numbers)
numbers) {{
total
total +=
+= i;
i;
}}
return
return total;
total;
25

26. Компиляция и создание JAR архива

G:\>javac
G:\>javac -d
-d bin
bin src\org\cud\calc\Calculator.java
src\org\cud\calc\Calculator.java
G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
bin\org\cud\calc
bin\org\cud\calc
in
in drive
drive GG has
has no
no label.
label.
Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin\org\cud\calc
G:\bin\org\cud\calc
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:54
<DIR>
..
11:54 AM
AM
<DIR>
11:54
<DIR>
..
11:54 AM
AM
<DIR>
..
11:58
369
11:58 AM
AM
369 Calculator.class
Calculator.class
11 File(s)
369
File(s)
369 bytes
bytes
22 Dir(s)
Dir(s) 42,079,776,768
42,079,776,768 bytes
bytes free
free
G:\>jar
G:\>jar -cfv
-cfv bin/calculator.jar
bin/calculator.jar -C
-C bin
bin org
org
added
added manifest
manifest
adding:
adding: calculator.jar(in
calculator.jar(in == 111)
111) (out=
(out= 64)(deflated
64)(deflated 42%)
42%)
adding:
adding: org/(in
org/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
org/cud/(in
=
0)
(out=
0)(stored
adding: org/cud/(in = 0) (out= 0)(stored 0%)
0%)
adding:
org/cud/calc/(in
=
0)
(out=
0)(stored
adding: org/cud/calc/(in = 0) (out= 0)(stored 0%)
0%)
adding:
adding: org/cud/calc/Calculator.class(in
org/cud/calc/Calculator.class(in == 369)
369) (out=
(out= 300)(deflated
300)(deflated 18%)
18%)
adding:
adding: org/cud/hello/(in
org/cud/hello/(in == 0)
0) (out=
(out= 0)(stored
0)(stored 0%)
0%)
adding:
adding: org/cud/hello/HelloCalc.class(in
org/cud/hello/HelloCalc.class(in == 759)
759) (out=
(out= 461)(deflated
461)(deflated 39%)
39%)
G:\>
G:\>
26

27. Простое приложение “Привет калькулятор!”

package
package org.cud.hello;
org.cud.hello;
import
import org.cud.calc.Calculator;
org.cud.calc.Calculator;
public
public class
class HelloCalc
HelloCalc {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
}}
}}
System.out.println("Hello
System.out.println("Hello Calculator");
Calculator");
Calculator
Calculator calculator
calculator == new
new Calculator();
Calculator();
System.out.println("2+3+4
System.out.println("2+3+4 == "" ++ calculator.sum(2,
calculator.sum(2, 3,
3, 4));
4));
27

28. Компиляция и запуск

G:\>javac
G:\>javac -d
-d bin
bin -cp
-cp bin\calculator.jar
bin\calculator.jar src\org\cud\hello\HelloCalc.java
src\org\cud\hello\HelloCalc.java
G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
bin\org\cud\hello
bin\org\cud\hello
in
in drive
drive GG has
has no
no label.
label.
Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin\org\cud\hello
G:\bin\org\cud\hello
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:54
<DIR>
..
11:54 AM
AM
<DIR>
11:54
<DIR>
..
11:54 AM
AM
<DIR>
..
12:08
759
12:08 PM
PM
759 HelloCalc.class
HelloCalc.class
11 File(s)
759
File(s)
759 bytes
bytes
22 Dir(s)
Dir(s) 42,079,776,768
42,079,776,768 bytes
bytes free
free
G:\>dir
G:\>dir bin
bin
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\bin
G:\bin
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11:58
<DIR>
..
11:58 AM
AM
<DIR>
11:58
<DIR>
..
11:58 AM
AM
<DIR>
..
11:59
1,972
11:59 AM
AM
1,972 calculator.jar
calculator.jar
11:54
AM
<DIR>
org
11:54 AM
<DIR>
org
11 File(s)
1,972
File(s)
1,972 bytes
bytes
33 Dir(s)
Dir(s) 42,079,776,768
42,079,776,768 bytes
bytes free
free
G:\>java
G:\>java -cp
-cp bin;bin\calculator.jar
bin;bin\calculator.jar org.cud.hello.HelloCalc
org.cud.hello.HelloCalc
Hello
Calculator
Hello Calculator
2+3+4
2+3+4 == 99
G:\>
G:\>
28

29. Создадим манифест

Main-Class:
Main-Class:
Class-Path:
Class-Path:
org.cud.hello.HelloCalc
org.cud.hello.HelloCalc
calculator.jar
calculator.jar
29

30. Создание JAR архива приложения и запуск

G:\>dir
G:\>dir
Volume
Volume in
in drive
drive GG has
has no
no label.
label.
Volume
Volume Serial
Serial Number
Number is
is 3400-744D
3400-744D
Directory
Directory of
of G:\
G:\
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
01:58
<DIR>
bin
01:58 PM
PM
<DIR>
bin
01:53
69
01:53 PM
PM
69 manifest.mf
manifest.mf
01:44
PM
<DIR>
src
01:44 PM
<DIR>
src
11 File(s)
69
File(s)
69 bytes
bytes
22 Dir(s)
Dir(s) 42,079,608,832
42,079,608,832 bytes
bytes free
free
G:\>jar
G:\>jar cvmf
cvmf manifest.mf
manifest.mf bin/hello.jar
bin/hello.jar -C
-C bin
bin org/cud/hello/HelloCalc.class
org/cud/hello/HelloCalc.class
added
manifest
added manifest
adding:
adding: org/cud/hello/HelloCalc.class(in
org/cud/hello/HelloCalc.class(in == 759)
759) (out=
(out= 461)(deflated
461)(deflated 39%)
39%)
G:\>dir
G:\>dir
Volume
Volume
Volume
Volume
bin
bin
in
in drive
drive GG has
has no
no label.
label.
Serial
Number
is
3400-744D
Serial Number is 3400-744D
Directory
Directory of
of G:\bin
G:\bin
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
11/14/2012
01:58
<DIR>
..
01:58 PM
PM
<DIR>
01:58
<DIR>
..
01:58 PM
PM
<DIR>
..
01:58
PM
897
calculator.jar
01:58 PM
897 calculator.jar
02:04
1,009
02:04 PM
PM
1,009 hello.jar
hello.jar
01:44
<DIR>
org
01:44 PM
PM
<DIR>
org
22 File(s)
1,906
File(s)
1,906 bytes
bytes
33 Dir(s)
42,079,608,832
bytes
Dir(s) 42,079,608,832 bytes free
free
G:\>java
G:\>java -jar
-jar bin/hello.jar
bin/hello.jar
Hello
Hello Calculator
Calculator
2+3+4
2+3+4 == 99
30
English     Русский Правила