/[gentoo]/xml/htdocs/doc/en/java.xml
Gentoo

Contents of /xml/htdocs/doc/en/java.xml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.43 - (show annotations) (download) (as text)
Wed Sep 16 15:26:43 2009 UTC (5 years, 7 months ago) by nightmorph
Branch: MAIN
Changes since 1.42: +36 -11 lines
File MIME type: application/xml
new stuff about build-only VMs added, bug 285181

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/java.xml,v 1.42 2008/09/28 20:29:25 nightmorph Exp $ -->
3 <!DOCTYPE guide SYSTEM "/dtd/guide.dtd">
4
5 <guide link="/doc/en/java.xml">
6 <title>Gentoo Java Guide</title>
7
8 <author title="Author">
9 <mail link="nichoj@gentoo.org">Joshua Nichols</mail>
10 </author>
11 <author title="Author">
12 <mail link="karltk@gentoo.org">Karl Trygve Kalleberg</mail>
13 </author>
14 <author title="Editor">
15 <mail link="nightmorph@gentoo.org">Joshua Saddler</mail>
16 </author>
17
18 <abstract>
19 This guide will introduce you to Java and explain how to use Java with Gentoo
20 Linux.
21 </abstract>
22
23 <!-- The content of this document is licensed under the CC-BY-SA license -->
24 <!-- See http://creativecommons.org/licenses/by-sa/2.5 -->
25 <license/>
26
27 <version>0.18</version>
28 <date>2009-09-16</date>
29
30 <chapter>
31 <title>What is Java?</title>
32 <section>
33 <title>Overview</title>
34 <body>
35
36 <p>
37 Java is a programming language developed by engineers of Sun Microsystems. The
38 language is object-oriented and designed to run on multiple platforms without
39 the need of recompiling code for each platform. Although Java can be compiled
40 as a native program, much of Java's popularity can be attributed to its
41 portability, along with other features such as garbage collection. To make
42 platform independence possible the Java compiler compiles the Java code to an
43 intermediate representation called "Java bytecode" that runs on a JRE (Java
44 Runtime Environment) and not directly on the operating system.
45 </p>
46
47 <p>
48 In order to run Java bytecode, one needs to have a JRE (Java Runtime
49 Environment) installed. A JRE provides core libraries, a platform dependent
50 Java Virtual Machine, plugins for browsers, among other things. A JDK (Java
51 Development Kit) adds programming tools, such as a bytecode compiler and a
52 debugger.
53 </p>
54
55 </body>
56 </section>
57 </chapter>
58
59 <chapter>
60 <title>Before You Begin</title>
61 <section>
62 <title>Existing installs</title>
63 <body>
64
65 <p>
66 For existing installs, regardless of if you have installed anything Java
67 before, make sure you have followed the <uri
68 link="/proj/en/java/java-upgrade.xml">Java Upgrade Guide</uri>.
69 </p>
70
71 </body>
72 </section>
73 <section>
74 <title>New installs</title>
75 <body>
76
77 <p>
78 New installs should require no further preparation.
79 </p>
80
81 </body>
82 </section>
83 </chapter>
84
85 <chapter>
86 <title>Installing a Virtual Machine</title>
87 <section>
88 <title>The choices</title>
89 <body>
90
91 <p>
92 Gentoo provides numerous Runtime Environments (JREs) and Development Kits
93 (JDKs). Among the current choices, we have:
94 </p>
95
96 <table>
97 <tr>
98 <th>Vendor</th>
99 <th>JDK</th>
100 <th>JRE</th>
101 </tr>
102 <tr>
103 <ti>The Blackdown Java Kit</ti>
104 <ti>dev-java/blackdown-jdk</ti>
105 <ti>dev-java/blackdown-jre</ti>
106 </tr>
107 <tr>
108 <ti>Sun's Java Kit</ti>
109 <ti>dev-java/sun-jdk</ti>
110 <ti>dev-java/sun-jre-bin</ti>
111 </tr>
112 <tr>
113 <ti>The IBM Java Kit</ti>
114 <ti>dev-java/ibm-jdk-bin</ti>
115 <ti>dev-java/ibm-jre-bin</ti>
116 </tr>
117 <tr>
118 <ti>BEA WebLogic's J2SE Development Kit</ti>
119 <ti>dev-java/jrockit-jdk-bin</ti>
120 </tr>
121 </table>
122
123 <!--
124 TODO: list free implementations?
125 note about not drop-in replacements
126 kaffe/sablevm/gcj/jamvm
127 -->
128 <p>
129 The default for Java 1.4 is the Blackdown JRE/JDK pair, as it is freely
130 ("free as in beer") available without any registration fuss.
131 </p>
132
133 <p>
134 JREs and JDKs from Sun, IBM, and BEA are generally faster, but getting them
135 is a bit more work, as you are required to read and accept their license before
136 downloading (IBM additionally requires you to register).
137 </p>
138
139 </body>
140 </section>
141 <section>
142 <title>Installing a JRE/JDKs</title>
143 <body>
144
145 <p>
146 To install your profile's default JDK, you can run <c>emerge virtual/jdk</c>.
147 Or to install your profile's default JRE, you can <c>emerge virtual/jre</c>.
148 </p>
149
150 <p>
151 In recent events, Sun has relicensed their JDK and JRE under a more Linux
152 distro friendly license. As a result, Sun releases Java 1.5 and onwards are
153 freely downloadable, without any further hassle.
154 </p>
155
156 <note>
157 A JDK also includes a JRE, so if you install a JDK you shouldn't have to also
158 have to install a JRE.
159 </note>
160
161 </body>
162 </section>
163 <section>
164 <title>Installing fetch-restricted virtual machines</title>
165 <body>
166
167 <p>
168 As already mentioned, some of the JDKs and JREs require you to jump through a
169 few hoops before installing. Simply emerge the packages as you normally would.
170 The ebuilds will then instruct you where to go and what to download.
171 </p>
172
173 <p>
174 You should download the indicated file(s) into
175 <path>/usr/portage/distfiles</path>. Once there, you can rerun the emerge
176 command, at which point the JRE/JDK will be begin to install.
177 </p>
178
179 </body>
180 </section>
181 </chapter>
182
183 <chapter>
184 <title>Configuring your virtual machine</title>
185 <section>
186 <title>Overview</title>
187 <body>
188
189 <p>
190 Gentoo has the ability to have multiple JDKs and JREs installed without causing
191 conflicts.
192 </p>
193
194 <p>
195 Using the <c>java-config</c> tool, you can set the system-wide default
196 (provided you have root access). Users can also use <c>java-config</c> to set
197 up their own personal default.
198 </p>
199
200 <note>
201 You can also use <e>eselect</e> to change the system and user vm. See
202 <c>eselect java-vm help</c>.
203 </note>
204
205 </body>
206 </section>
207 <section>
208 <title>Setting a default virtual machine</title>
209 <body>
210
211 <p>
212 Running the command <c>java-config --list-available-vms</c> will give you a
213 list of all JREs and JDKs installed on your system. Here is an example of
214 output:
215 </p>
216
217 <pre caption="Listing available VMs">
218 # <i>java-config --list-available-vms</i>
219 The following VMs are available for generation-2:
220 1) Blackdown JDK 1.4.2.03 [blackdown-jdk-1.4.2] <comment>(Build Only)</comment>
221 2) Blackdown JRE 1.4.2.03 [blackdown-jre-1.4.2] <comment>(Build Only)</comment>
222 3) IcedTea6-bin 1.4.1 [icedtea6-bin]
223 4) Sun JDK 1.5.0.20 [sun-jdk-1.5] <comment>(Build Only)</comment>
224 *) Sun JDK 1.6.0.16 [sun-jdk-1.6]
225 </pre>
226
227 <note>
228 VMs marked as Build Only may contain security vulnerabilities and/or be EOL.
229 Gentoo recommends not setting these VMs as either your system or user VM.
230 Please see <uri link="java.xml#build-only">Build Only VM</uri> for more
231 information.
232 </note>
233
234 <p>
235 The <e>*</e> indicates this is the current active vm (system-vm or user-vm when
236 set). The name in the brackets (<e>[]</e>) is the handle or ID for that
237 particular VM. You use the handle or the number to <c>java-config
238 --set-system-vm</c>. Here is an example of how to set the system VM.
239 </p>
240
241 <pre caption="Setting the System VM">
242 <comment>(By handle (preferred))</comment>
243 # <i>java-config --set-system-vm blackdown-jdk-1.4</i>
244 Now using blackdown-jdk-1.4 as your generation-2 system JVM
245 WARNING: blackdown-jdk-1.4 is marked as a build-only JVM. Using this vm is not recommended.
246 <comment>(By number)</comment>
247 # <i>java-config --set-system-vm 5</i>
248 Now using sun-jdk-1.6 as your generation-2 system JVM
249 </pre>
250
251 <p>
252 As a regular user, you can use <c>java-config --set-user-vm</c>.
253 </p>
254
255 <note>
256 You no longer have to <c>source</c> the profile for updates to the user/system
257 VM take place.
258 </note>
259
260 </body>
261 </section>
262 <section id="build-only">
263 <title>Build Only VM</title>
264 <body>
265
266 <p>
267 Some virtual machines are flagged as build-only due to being EOL and/or
268 containing security vulnerabilities. These virtual machines will not
269 automatically be used by Gentoo for the running of applications using Gentoo
270 launchers but will still be available for use by Gentoo's build environment as
271 some packages may require them for building. The setting of these virtual
272 machines as either your system or user VM is strongly discouraged as these VMs
273 will then be used when running the <path>/usr/bin/{java,javac,..}</path>
274 executables and will also be used by any packages not using Gentoo's launcher
275 scripts.
276 </p>
277
278 </body>
279 </section>
280 <section id="preferred-vm">
281 <title>Preferred VM</title>
282 <body>
283
284 <p>
285 While merging Java packages, the VM can and will be switched as necessary.
286 </p>
287
288 <p>
289 Because of the wide variety of available VMs, we do not have the resources to
290 test and verify every package works on all of them. So to ensure that every
291 packages merges smoothly, we have defined a list of <e>default/supported
292 VMs</e> per arch. You can find them in
293 <path>/usr/share/java-config-2/config/jdk-defaults.conf</path>. When you are
294 merging a Java package, and it detects one of the VM in that file is installed,
295 it will automatically use that VM, instead of the system-vm.
296 </p>
297
298 <p>
299 The merge time VM switching is also needed when, for example, your system-vm is
300 set a 1.4 VM and the package you are merging requires a 1.5 VM. While merging
301 it will use the preferred 1.5 VM, leaving your system-vm choice intact.
302 </p>
303
304 <p>
305 Of course, Gentoo is all about choice, so you can override these defaults in
306 <path>/etc/java-config-2/build/jdk.conf</path> and have complete control over
307 which VM will get used. Some examples:
308 </p>
309
310 <pre caption="Example /etc/java-config-2/build/jdk.conf">
311 <comment>(I always want it to use a sun-jdk, ie sun-jdk-1.4 for 1.4, sun-jdk-1.5 for 1.5, etc)</comment>
312 *=sun-jdk
313 </pre>
314
315 <pre caption="Example /etc/java-config-2/build/jdk.conf">
316 <comment>(Always use sun-jdk-1.5 wherever possible, except for when a 1.4 or 1.3 VM is explicitly required)</comment>
317 *=sun-jdk-1.5
318 </pre>
319
320 <pre caption="Example /etc/java-config-2/build/jdk.conf">
321 <comment># For 1.3 I prefer sun-jdk 1.4 but when it is not available, use ibm-jdk-bin,
322 # For 1.4, use blackdown-jdk, and for 1.5, use sun-jdk </comment>
323 1.3=sun-jdk-1.4 ibm-jdk-bin
324 1.4=blackdown-jdk
325 1.5=sun-jdk
326 </pre>
327
328 <warn>
329 You do not have to edit this file. If you change these options to use a
330 unsupported VM, things could possibly break. Bugs reported with a unsupported
331 VM won't be prioritized as much as bugs present within supported VMs.
332 </warn>
333
334 </body>
335 </section>
336 </chapter>
337
338 <chapter>
339 <title>Compilers</title>
340 <section>
341 <body>
342
343 <p>
344 The standard Java compiler used for building is javac, which comes with each
345 JDK. In addition to configuring the VM used at build time, it is also possible
346 configure which compiler is used. Essentially, you define a list your
347 preference for which compiler to use in
348 <path>/etc/java-config-2/build/compilers.conf</path>.
349 </p>
350
351 <pre caption="/etc/java-config-2/build/compilers.conf">
352 # If the ebuild supports it
353 # it will check the COMPILERS var front to back and
354 # use the first compiler that is installed
355
356 COMPILERS="ecj-3.1 jikes javac"
357 </pre>
358
359 <p>
360 Some compilers don't support all possible -target and -source arguments.
361 Therefore, each compiler in the list is checked to see if it can support the
362 desired -source/-target. javac will work in all cases, so if no other suitable
363 compiler is found, it will be used instead.
364 </p>
365
366 <p>
367 More details about each compiler are provided below:
368 </p>
369
370 <table>
371 <tr>
372 <th>Name</th>
373 <th>Handle</th>
374 <th>Package</th>
375 <th>Description</th>
376 </tr>
377 <tr>
378 <ti>javac</ti>
379 <ti>javac</ti>
380 <ti>N/A</ti>
381 <ti>
382 This is the default compiler that will be used, and comes with each JDK.
383 </ti>
384 </tr>
385 <tr>
386 <ti>jikes</ti>
387 <ti>jikes</ti>
388 <ti>dev-java/jikes</ti>
389 <ti>
390 Jikes was originally developed by IBM. Anecdotally, it is generally quicker
391 than javac. Note however, that it is more pedantic, and will fail under a
392 few circumstances where javac has no issue. It also does not support Java
393 1.5 syntax yet.
394 </ti>
395 </tr>
396 <tr>
397 <ti>Eclipse Compiler for Java</ti>
398 <ti>ecj-3.1</ti>
399 <ti>=dev-java/eclipse-ecj-3.1*</ti>
400 <ti>
401 ECJ is the compiler used by the Eclipse software development kit. It is
402 very full featured, and is pretty fast. It does support Java 1.5 syntax.
403 </ti>
404 </tr>
405 </table>
406
407 </body>
408 </section>
409 </chapter>
410
411 <chapter>
412 <title>Setting a default CLASSPATH</title>
413 <section>
414 <body>
415
416 <warn>
417 The options explained in this section should be considered deprecated and will
418 most likely be removed in the future. We strongly recommend against using
419 these, because your Java projects or application should ideally manage their
420 own classpaths. If you choose to specify a default CLASSPATH, some applications
421 may behave unexpectedly, because classes they weren't expecting would be on the
422 classpath.
423 </warn>
424
425 <p>
426 <c>java-config</c> can also be used to set a system-wide default CLASSPATH, as
427 well a user-specific default CLASSPATH.
428 </p>
429
430 <p>
431 First, you will want to list available Java libraries installed on your system
432 that might want to be put in your CLASSPATH. Here is an example of output:
433 </p>
434
435 <pre caption="Listing classes">
436 # <i>java-config --list-available-packages</i>
437 [xerces-2] The next generation of high performance, fully compliant XML parsers in the Apache Xerces family (/usr/share/xerces-2/package.env)
438 [junit] Simple framework to write repeatable tests (/usr/share/junit/package.env)
439 [bsh] BeanShell: A small embeddable Java source interpreter (/usr/share/bsh/package.env)
440 [bcel] The Byte Code Engineering Library: analyze, create, manipulate Java class files (/usr/share/bcel/package.env)
441 [log4j] A low-overhead robust logging package for Java (/usr/share/log4j/package.env)
442 ...
443 </pre>
444
445 <p>
446 Again, the names in brackets (<e>[]</e>) are the IDs that you have to pass to
447 <c>java-config --set-system-classpath</c>. Here is an example:
448 </p>
449
450 <pre caption="Setting classpaths">
451 # <i>java-config --set-system-classpath log4j,xerces-2</i>
452 </pre>
453
454 <note>
455 The current directory (<path>.</path>) will not be part of the system
456 classpath, as that should be added in your system's login profile.
457 </note>
458
459 <p>
460 You will have to update your environment by logging out, then in again or
461 sourcing <path>/etc/profile</path>.
462 </p>
463
464 <p>
465 For users, <c>java-config --set-user-classpath</c> will create
466 <path>~/.gentoo/java-env-classpath</path>, which you should then source from
467 your shell's profile.
468 </p>
469
470 <pre caption="Sourcing user specific classpath">
471 <i>if [[ -f "${HOME}/.gentoo/java-env-classpath" ]]; then
472 source ${HOME}/.gentoo/java-env-classpath
473 fi</i>
474 </pre>
475
476 <p>
477 If you really want a system wide or user default classpath you can add
478 something like the following to your shell's profile. But we would advise
479 against it.
480 </p>
481
482 <pre caption="Setting classpath">
483 # <i>export CLASSPATH="${CLASSPATH}:$(java-config --classpath log4j,xerces-2)"</i>
484 </pre>
485
486 </body>
487 </section>
488 </chapter>
489
490 <chapter>
491 <title>Java Browser Plugins</title>
492 <section>
493 <title>Installing a plugin</title>
494 <body>
495
496 <p>
497 You can install a Java plugin for your web browser by emerging a Java VM with
498 the <c>nsplugin</c> USE flag set.
499 </p>
500
501 <note>
502 <c>nsplugin</c> is not available for all architectures. Check for available
503 plugins on your arch before trying to install a VM by running <c>emerge -pv
504 &lt;java-vm&gt;</c>.
505 </note>
506
507 <p>
508 Portage will allow you to install multiple versions of Java plugins, though
509 only one will be used by your browser. You can check the list of available
510 plugins by running:
511 </p>
512
513 <pre caption="Viewing available plugins">
514 # <i>eselect java-nsplugin list</i>
515 [1] sun-jre-bin-1.5
516 [2] blackdown-jre-1.4.2
517 </pre>
518
519 <p>
520 In this example, <c>sun-jre-bin</c> is selected for the browser plugin.
521 </p>
522
523 <pre caption="Selecting a plugin">
524 # <i>eselect java-nsplugin set sun-jre-bin-1.5</i>
525 </pre>
526
527 <p>
528 Verify that the correct plugin was selected:
529 </p>
530
531 <pre caption="Verifying the correct plugin">
532 # <i>eselect java-nsplugin list</i>
533 [1] sun-jre-bin-1.5 current
534 [2] blackdown-jre-1.4.2
535 </pre>
536
537 <p>
538 Java.com also provides a link to <uri
539 link="http://java.com/en/download/installed.jsp">verify your installed
540 plugin</uri>. Additionally, if you are using a Mozilla-based browser, you can
541 verify your Java plugin by typing <c>about:plugins</c> into the address bar.
542 </p>
543
544 </body>
545 </section>
546 <section>
547 <title>Plugins on multilib systems</title>
548 <body>
549
550 <p>
551 If you are running a mixed 64-bit and 32-bit multilib system (for example, on
552 AMD64), you can only use 32-bit Java plugins.
553 </p>
554
555 <p>
556 To use a 32-bit plugin, you will need to emerge <c>emul-linux-x86-java</c> with
557 the <c>nsplugin</c> USE flag enabled.
558 </p>
559
560 <pre caption="Installing a 32-bit plugin">
561 # <i>echo "app-emulation/emul-linux-x86-java nsplugin" >> /etc/portage/package.use</i>
562 # <i>emerge emul-linux-x86-java</i>
563 </pre>
564
565 <p>
566 Next, check which plugins are available:
567 </p>
568
569 <pre caption="Viewing available plugins">
570 # <i>eselect java-nsplugin list</i>
571 Available 32-bit Java browser plugins
572 [1] emul-linux-x86-java-1.4.2
573 [2] emul-linux-x86-java-1.5
574 </pre>
575
576 <p>
577 Although you must select a 32-bit browser (such as <c>mozilla-firefox-bin</c>)
578 to use with your 32-bit plugin, the 64-bit version of <c>konqueror</c> uses your
579 Java VM directly, so it's possible to use the 64-bit version of <c>blackdown</c>
580 with this browser; no further configuration is necessary.
581 </p>
582
583 <p>
584 Now select the right plugin for your 32-bit browser:
585 </p>
586
587 <pre caption="Selecting plugins">
588 # <i>eselect java-nsplugin set 32bit emul-linux-x86-java-1.5</i>
589 </pre>
590
591 <p>
592 Verify the correct plugin was selected:
593 </p>
594
595 <pre caption="Verifying the correct plugin">
596 # <i>eselect java-nsplugin list</i>
597 Available 32-bit Java browser plugins
598 [1] emul-linux-x86-java-1.4.2
599 [2] emul-linux-x86-java-1.5 current
600 </pre>
601
602 </body>
603 </section>
604 </chapter>
605
606 <chapter>
607 <title>USE flags for use with Java</title>
608 <section>
609 <title>Setting USE flags</title>
610 <body>
611
612 <p>
613 For more information regarding USE flags, refer to the <uri
614 link="/doc/en/handbook/handbook-x86.xml?part=2&amp;chap=2">USE flags</uri>
615 chapter from the Gentoo Handbook.
616 </p>
617
618 </body>
619 </section>
620 <section>
621 <title>The flags</title>
622 <body>
623
624 <ul>
625 <li>The <b>java</b> flag adds support for Java in a variety of programs</li>
626 <li>
627 The <b>nsplugin</b> flag adds support for Mozilla-like browsers (including
628 Firefox). You will need this for viewing Java applets in your Mozilla-like
629 browser.
630 </li>
631 <li>
632 The <b>source</b> flag installs a zip of the source code of a package.
633 This is traditionally used for IDEs to 'attach' source to the libraries you
634 are using.
635 </li>
636 <li>The <b>jce</b> flag adds support for the Java Cryptography Engine</li>
637 <li>
638 For Java packages, the <b>doc</b> flag will build API documentation using
639 javadoc.
640 </li>
641 </ul>
642
643 </body>
644 </section>
645 </chapter>
646
647 <chapter>
648 <title>Additional resources</title>
649 <section>
650 <title>Off-line resources</title>
651 <body>
652
653 <ul>
654 <li>java-config man page</li>
655 <li><c>java-config --help</c></li>
656 </ul>
657
658 </body>
659 </section>
660 <section>
661 <title>Online resources</title>
662 <body>
663
664 <ul>
665 <li>
666 The <uri link="http://www.gentoo.org/proj/en/java/">Java Project
667 Page</uri>
668 </li>
669 <li>
670 The <uri
671 link="http://news.gmane.org/gmane.linux.gentoo.java">gentoo-java</uri>,
672 <uri
673 link="http://news.gmane.org/gmane.linux.gentoo.user">gentoo-user</uri>, and
674 <uri
675 link="http://news.gmane.org/gmane.linux.gentoo.devel">gentoo-dev</uri>
676 mailing list archives
677 </li>
678 <li>
679 <uri link="irc://irc.gentoo.org/gentoo">#gentoo</uri> and <uri
680 link="irc://irc.gentoo.org/gentoo-java">#gentoo-java</uri> on IRC
681 </li>
682 <li>
683 <uri
684 link="http://en.wikipedia.org/wiki/Java_programming_language">Wikipedia's
685 entry for Java</uri>
686 </li>
687 <li>
688 If you have suggestions or questions regarding this document, please email
689 the Gentoo Java team: <mail>java@gentoo.org</mail>
690 </li>
691 </ul>
692
693 </body>
694 </section>
695 </chapter>
696 </guide>

  ViewVC Help
Powered by ViewVC 1.1.20