author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Wed, 05 Aug 2015 23:55:34 +0300 | |
changeset 1729 | 22ffd80639c0 |
parent 1687 | da4a6ceb7e89 |
child 1833 | 378b566fd86b |
permissions | -rw-r--r-- |
1213 | 1 |
.. -*- coding: utf-8; -*- |
1334
9bf0d5a1f0cf
Include common header with quick links.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1270
diff
changeset
|
2 |
.. include:: HEADER.rst |
1213 | 3 |
|
4 |
================ |
|
5 |
Java language. |
|
6 |
================ |
|
1346
a2fbf50a43f4
Fix: Has no 'contents::' directive.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1334
diff
changeset
|
7 |
.. contents:: |
1213 | 8 |
|
9 |
Class version. |
|
10 |
============== |
|
11 |
||
1216 | 12 |
========= ====== ===================== |
13 |
major minor Java platform version |
|
14 |
========= ====== ===================== |
|
15 |
45 0x27 3 1.0 |
|
16 |
45 0x27 3 1.1 |
|
17 |
46 0x28 0 1.2 |
|
18 |
47 0x29 0 1.3 |
|
19 |
48 0x30 0 1.4 |
|
1677
3d2d4e6062f3
New Java byte code versions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1655
diff
changeset
|
20 |
49 0x31 0 5.0 |
3d2d4e6062f3
New Java byte code versions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1655
diff
changeset
|
21 |
50 0x32 0 6.0 |
3d2d4e6062f3
New Java byte code versions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1655
diff
changeset
|
22 |
51 0x33 0 7 |
3d2d4e6062f3
New Java byte code versions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1655
diff
changeset
|
23 |
52 0x34 0 8 |
1216 | 24 |
========= ====== ===================== |
1213 | 25 |
|
1216 | 26 |
where ``minor`` and ``major`` are value of 6 and 8 bytes in .class file:: |
27 |
||
28 |
0xCA, 0xFE, 0xBA, 0xBE, 0x00, minor, 0x00, major |
|
1213 | 29 |
|
30 |
Access modifiers. |
|
31 |
================= |
|
32 |
||
33 |
Public. |
|
34 |
------- |
|
35 |
||
36 |
* Public class is visible in other packages. |
|
37 |
* Public field is visible everywhere (class must be public too). |
|
38 |
||
39 |
Private. |
|
40 |
-------- |
|
41 |
||
42 |
* Private variables or methods may be used only by an instance of the same |
|
1216 | 43 |
class that declares the variable or method |
1213 | 44 |
* A private feature may only be accessed by the class that owns the feature. |
45 |
||
46 |
Protected. |
|
47 |
---------- |
|
48 |
||
49 |
* Is available to all classes in the same package and also available to all |
|
1216 | 50 |
subclasses of the class that owns the protected feature. |
1213 | 51 |
* This access is provided even to subclasses that reside in a different |
1216 | 52 |
package from the class that owns the protected feature. |
1213 | 53 |
|
54 |
default. |
|
55 |
-------- |
|
56 |
||
57 |
What you get by default ie, without any access modifier. |
|
58 |
||
59 |
* It means that it is visible to all within a particular package. |
|
60 |
||
61 |
static. |
|
62 |
------- |
|
63 |
||
64 |
* Static means one per class, not one for each object no matter how many |
|
65 |
instance of a class might exist. This means that you can use them without |
|
66 |
creating an instance of a class. |
|
67 |
* Static methods are implicitly final, because overriding is done based on |
|
68 |
the type of the object, and static methods are attached to a class, not an |
|
69 |
object. |
|
70 |
* A static method in a superclass can be shadowed by another static method in |
|
71 |
a subclass, as long as the original method was not declared final. |
|
72 |
* You can't override a static method with a nonstatic method. |
|
73 |
||
74 |
final. |
|
75 |
------ |
|
76 |
||
77 |
* A final class can't be extended ie., final class may not be subclassed. |
|
78 |
* A final method can't be overridden when its class is inherited. |
|
79 |
* You can't change value of a final variable. |
|
80 |
||
81 |
Exceptions. |
|
82 |
=========== |
|
83 |
||
84 |
A checked exception is some subclass of Exception (or Exception itself), |
|
85 |
excluding class RuntimeException and its subclasses. |
|
86 |
||
87 |
Unchecked exceptions are RuntimeException and any of its subclasses. Class |
|
88 |
Error and its subclasses also are unchecked. With an unchecked exception, |
|
89 |
however, the compiler doesn't force client programmers either to catch the |
|
90 |
exception or declare it in a throws clause. |
|
91 |
||
92 |
Inner classes. |
|
93 |
============== |
|
94 |
||
95 |
Nested top-level classes. |
|
96 |
------------------------- |
|
97 |
||
98 |
If you declare a class within a class and specify the static modifier, the |
|
99 |
compiler treats the class just like any other top-level class. |
|
100 |
||
101 |
Any class outside the declaring class accesses the nested class with the |
|
102 |
declaring class name acting similarly to a package. eg, outer.inner. Top-level |
|
103 |
inner classes implicitly have access only to static variables. There can also |
|
104 |
be inner interfaces. All of these are of the nested top-level variety. |
|
105 |
||
106 |
Member classes. |
|
107 |
--------------- |
|
108 |
||
109 |
Member inner classes are just like other member methods and member variables |
|
110 |
and access to the member class is restricted, just like methods and variables. |
|
111 |
This means a public member class acts similarly to a nested top-level class. |
|
112 |
||
113 |
The primary difference between member classes and nested top-level classes is |
|
114 |
that member classes have access to the specific instance of the enclosing |
|
115 |
class. |
|
116 |
||
117 |
Local classes. |
|
118 |
-------------- |
|
119 |
||
120 |
Local classes are like local variables, specific to a block of code. Their |
|
121 |
visibility is only within the block of their declaration. In order for the |
|
122 |
class to be useful beyond the declaration block, it would need to implement a |
|
123 |
more publicly available interface. |
|
124 |
||
125 |
Because local classes are not members, the modifiers public, protected, |
|
126 |
private, and static are not usable. |
|
127 |
||
128 |
Anonymous classes. |
|
129 |
------------------ |
|
130 |
||
131 |
Anonymous inner classes extend local inner classes one level further. As |
|
132 |
anonymous classes have no name, you cannot provide a constructor. |
|
133 |
||
134 |
64-bit problem. |
|
135 |
=============== |
|
136 |
||
137 |
http://www.java.com/en/download/faq/java_win64bit.xml |
|
138 |
Which version of Java should I download for my 64-bit Windows |
|
139 |
operating system? |
|
140 |
http://java.sun.com/javase/6/webnotes/install/system-configurations.html |
|
141 |
Java SE 6 Release Notes Supported System Configurations |
|
142 |
||
143 |
Java performance. |
|
144 |
================= |
|
145 |
||
146 |
http://java.sun.com/performance/reference/whitepapers/5.0_performance.html |
|
147 |
http://java.sun.com/performance/reference/whitepapers/6_performance.html |
|
148 |
||
149 |
Creating jar. |
|
150 |
============= |
|
151 |
:: |
|
152 |
||
153 |
$ jar cf myFile.jar *.class |
|
154 |
$ jar cmf myManifestFile myFile.jar *.class |
|
155 |
$ jar -cfe Main.jar foo.Main foo/Main.class |
|
156 |
||
1215 | 157 |
Profiling Java. |
1213 | 158 |
=============== |
159 |
:: |
|
160 |
||
161 |
$ java -Xprof com.vendor.product.Clazz |
|
1214
fe28cec0bb40
java -Xrunhprof:help
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1213
diff
changeset
|
162 |
$ java -Xrunhprof:help |
1213 | 163 |
|
1215 | 164 |
Debugging Java. |
165 |
=============== |
|
166 |
||
167 |
Compile with ``-g`` to preserve source code information:: |
|
168 |
||
169 |
$ javac -g -cp $CLASSPATH -sourcepath $SRC_DIR -d $BUILD_DIR |
|
170 |
||
171 |
To run Java program in debugger:: |
|
172 |
||
173 |
$ jdb -cp $CLASSPATH -sourcepath $SRC_DIR |
|
174 |
||
1238
53da16ba1897
-Xrunjdwp:transport
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1216
diff
changeset
|
175 |
To attach to Java application you firstly must run application with (use |
53da16ba1897
-Xrunjdwp:transport
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1216
diff
changeset
|
176 |
``dt_shmem`` for Windows and ``dt_socket`` for Linux):: |
1215 | 177 |
|
178 |
$ java -Xdebug -Xrunjdwp:transport=dt_shmem,server=y,suspend=n,address=$PORT \ |
|
179 |
com.vendor.product.Clazz |
|
180 |
||
181 |
and then attach with debugger:: |
|
182 |
||
183 |
$ jdb -attach $PORT |
|
184 |
||
1629
32f8c509119d
Dump current thread traces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1563
diff
changeset
|
185 |
Dump current thread traces and memory statistic to stdout:: |
32f8c509119d
Dump current thread traces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1563
diff
changeset
|
186 |
|
32f8c509119d
Dump current thread traces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1563
diff
changeset
|
187 |
$ kill -QUIT $PID |
32f8c509119d
Dump current thread traces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1563
diff
changeset
|
188 |
|
1643
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
189 |
Debug class loading. |
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
190 |
==================== |
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
191 |
|
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
192 |
To dump class loading and unloading to ``System.out`` add to ``java`` opts:: |
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
193 |
|
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
194 |
$ java -XX:+TraceClassLoading -XX:+TraceClassUnloading ... |
1644 | 195 |
$ java -verbose:class ... |
1643
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
196 |
|
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
197 |
To review loaded classes explore heap dump in ``visualvm`` (visit "Classes" |
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
198 |
tab). |
fed98aef5cae
Debug class loading.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1629
diff
changeset
|
199 |
|
1687
da4a6ceb7e89
Decompile class file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1686
diff
changeset
|
200 |
Decompile class file. |
da4a6ceb7e89
Decompile class file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1686
diff
changeset
|
201 |
===================== |
da4a6ceb7e89
Decompile class file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1686
diff
changeset
|
202 |
:: |
da4a6ceb7e89
Decompile class file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1686
diff
changeset
|
203 |
|
da4a6ceb7e89
Decompile class file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1686
diff
changeset
|
204 |
$ javap -v -p -c My.java |
da4a6ceb7e89
Decompile class file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1686
diff
changeset
|
205 |
|
1269 | 206 |
Find jar by class. |
207 |
================== |
|
208 |
||
1270
af6cbb1b5888
http://mvnrepository.com/search.html?query=PKG
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1269
diff
changeset
|
209 |
http://mvnrepository.com/search.html?query=PKG |
1269 | 210 |
http://www.jarfinder.com |
211 |
||
1686
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
212 |
Set default Java in Debian. |
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
213 |
=========================== |
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
214 |
:: |
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
215 |
|
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
216 |
$ update-java-alternatives -l |
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
217 |
$ sudo update-java-alternatives -s java-1.8.0-openjdk-i386 |
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
218 |
|
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
219 |
or individually for each executable:: |
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
220 |
|
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
221 |
$ sudo update-alternatives --config java |
4ea7c87dfb87
Set default Java in Debian.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1677
diff
changeset
|
222 |
|
1655
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
223 |
Import SSL certificate. |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
224 |
======================= |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
225 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
226 |
Get cert with:: |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
227 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
228 |
$ openssl s_client -connect promin-test.it.loc:433 |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
229 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
230 |
-----BEGIN CERTIFICATE----- |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
231 |
... |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
232 |
-----END CERTIFICATE----- |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
233 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
234 |
or by opening URL in broswer and exporting in "Page info" ==> "Security" menu. |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
235 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
236 |
Call import utility with default ``changeit`` password:: |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
237 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
238 |
$ keytool -importcert -file $YOUR.crt -keystore $JAVA_HOME/jre/lib/security/cacert -alias $ANY -storepass changeit |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
239 |
$ keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacert -storepass changeit |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
240 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
241 |
Import certificate system wide in Debian by (note, ``.crt`` extention is |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
242 |
mandatory):: |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
243 |
|
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
244 |
$ sudo mkdir /usr/share/ca-certificates/$ANY/ # don't mess with other certs |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
245 |
$ sudo cp /tmp/$YOUR.crt /usr/share/ca-certificates/$ANY/ |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
246 |
$ sudo dpkg-reconfigure --force ca-certificates # check your cert in curses GUI! |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
247 |
$ sudo update-ca-certificates --fresh --verbose |
1cabdf6fe296
Import SSL certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1644
diff
changeset
|
248 |
|
1379 | 249 |
Java EE versions. |
250 |
================= |
|
251 |
||
252 |
======= ======== ======== ======== |
|
253 |
Java EE Servlet JSP JSTL |
|
254 |
======= ======== ======== ======== |
|
255 |
6 3.0 2.2 - |
|
256 |
5 2.5 2.1 1.2 |
|
257 |
1.4 2.4 2.0 1.1 |
|
258 |
1.2 2.3 1.2 1.0 |
|
259 |
======= ======== ======== ======== |
|
260 |
||
1380 | 261 |
To set servlet version check ``WEB-INF/web.xml``:: |
1379 | 262 |
|
263 |
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"> |
|
264 |
||
265 |
See: |
|
266 |
||
267 |
http://jcp.org/aboutJava/communityprocess/final/jsr315/index.html |
|
268 |
Servlet 3.0 Specification |
|
269 |
http://jcp.org/aboutJava/communityprocess/mrel/jsr154/index.html |
|
270 |
Servlet 2.5 Specification |
|
271 |
http://www.mularien.com/blog/2008/04/24/how-to-reference-and-use-jstl-in-your-web-application/ |
|
272 |
How to Reference and Use JSTL in your Web Application |
|
273 |
http://en.wikipedia.org/wiki/Java_EE_version_history |
|
274 |
Java EE version history |
|
1563
9b4441d30cac
Java interactive shell.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1380
diff
changeset
|
275 |
|
9b4441d30cac
Java interactive shell.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1380
diff
changeset
|
276 |
Java interactive shell. |
9b4441d30cac
Java interactive shell.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1380
diff
changeset
|
277 |
======================= |
9b4441d30cac
Java interactive shell.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1380
diff
changeset
|
278 |
|
9b4441d30cac
Java interactive shell.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1380
diff
changeset
|
279 |
Just use Groovy. ``bsh`` is older alternative without code completion. |