/[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.34 - (show annotations) (download) (as text)
Sun Oct 8 22:52:16 2006 UTC (8 years, 2 months ago) by nightmorph
Branch: MAIN
Changes since 1.33: +121 -16 lines
File MIME type: application/xml
Updated java.xml for bug 148699. includes new java browser plug-in chapter, as well as updated code listings. thanks to nichoj for the help

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

  ViewVC Help
Powered by ViewVC 1.1.20