1 \input texinfo @c -*-texinfo-*-
3 @settitle Guide to GNU gcj
5 @c Merge the standard indexes into a single one.
12 @include gcc-common.texi
14 @c Note: When reading this manual you'll find lots of strange
15 @c circumlocutions like ``compiler for the Java language''.
16 @c This is necessary due to Sun's restrictions on the use of
19 @c When this manual is copyrighted.
20 @set copyrights-gcj 2001, 2002, 2003, 2004, 2005, 2006, 2007
23 @set which-gcj GCC-@value{version-GCC}
26 @c man begin COPYRIGHT
27 Copyright @copyright{} @value{copyrights-gcj} Free Software Foundation, Inc.
29 Permission is granted to copy, distribute and/or modify this document
30 under the terms of the GNU Free Documentation License, Version 1.2 or
31 any later version published by the Free Software Foundation; with the
32 Invariant Sections being ``GNU General Public License'', the Front-Cover
33 texts being (a) (see below), and with the Back-Cover Texts being (b)
34 (see below). A copy of the license is included in the
37 ``GNU Free Documentation License''.
39 @c man begin COPYRIGHT
44 @c man begin COPYRIGHT
46 (a) The FSF's Front-Cover Text is:
50 (b) The FSF's Back-Cover Text is:
52 You have freedom to copy and modify this GNU Manual, like GNU
53 software. Copies published by the Free Software Foundation raise
54 funds for GNU development.
60 @dircategory Software development
62 * Gcj: (gcj). Ahead-of-time compiler for the Java language
65 @dircategory Individual utilities
67 * gcjh: (gcj)Invoking gcjh.
68 Generate header files from Java class files
69 * gjnih: (gcj)Invoking gjnih.
70 Generate JNI header files from Java class files
71 * jcf-dump: (gcj)Invoking jcf-dump.
72 Print information about Java class files
73 * gij: (gcj)Invoking gij. GNU interpreter for Java bytecode
74 * gcj-dbtool: (gcj)Invoking gcj-dbtool.
75 Tool for manipulating class file databases.
76 * jv-convert: (gcj)Invoking jv-convert.
77 Convert file from one encoding to another
78 * grmic: (gcj)Invoking grmic.
79 Generate stubs for Remote Method Invocation.
80 * grmiregistry: (gcj)Invoking grmiregistry.
81 The remote object registry.
82 * gc-analyze: (gcj)Invoking gc-analyze.
83 Analyze Garbage Collector (GC) memory dumps.
96 @vskip 0pt plus 1filll
97 Published by the Free Software Foundation @*
98 51 Franklin Street, Fifth Floor@*
99 Boston, MA 02110-1301, USA@*
110 This manual describes how to use @command{gcj}, the GNU compiler for the
111 Java programming language. @command{gcj} can generate both @file{.class}
112 files and object files, and it can read both Java source code and
116 * Copying:: The GNU General Public License
117 * GNU Free Documentation License::
118 How you can share and copy this manual
119 * Invoking gcj:: Compiler options supported by @command{gcj}
120 * Compatibility:: Compatibility between gcj and other tools for Java
121 * Invoking gcjh:: Generate header files from class files
122 * Invoking gjnih:: Generate JNI header files from class files
123 * Invoking jcf-dump:: Print information about class files
124 * Invoking gij:: Interpreting Java bytecodes
125 * Invoking gcj-dbtool:: Tool for manipulating class file databases.
126 * Invoking jv-convert:: Converting from one encoding to another
127 * Invoking grmic:: Generate stubs for Remote Method Invocation.
128 * Invoking grmiregistry:: The remote object registry.
129 * Invoking gc-analyze:: Analyze Garbage Collector (GC) memory dumps.
130 * About CNI:: Description of the Compiled Native Interface
131 * System properties:: Modifying runtime behavior of the libgcj library
132 * Resources:: Where to look for more information
143 @chapter Invoking gcj
145 @c man title gcj Ahead-of-time compiler for the Java language
148 @c man begin SYNOPSIS gcj
149 gcj [@option{-I}@var{dir}@dots{}] [@option{-d} @var{dir}@dots{}]
150 [@option{--CLASSPATH}=@var{path}] [@option{--classpath}=@var{path}]
151 [@option{-f}@var{option}@dots{}] [@option{--encoding}=@var{name}]
152 [@option{--main}=@var{classname}] [@option{-D}@var{name}[=@var{value}]@dots{}]
153 [@option{-C}] [@option{--resource} @var{resource-name}] [@option{-d} @var{directory}]
154 [@option{-W}@var{warn}@dots{}]
155 @var{sourcefile}@dots{}
157 @c man begin SEEALSO gcj
158 gcc(1), gcjh(1), gjnih(1), gij(1), jcf-dump(1), gfdl(7),
159 and the Info entries for @file{gcj} and @file{gcc}.
163 @c man begin DESCRIPTION gcj
165 As @command{gcj} is just another front end to @command{gcc}, it supports many
166 of the same options as gcc. @xref{Option Summary, , Option Summary,
167 gcc, Using the GNU Compiler Collection (GCC)}. This manual only documents the
168 options specific to @command{gcj}.
173 * Input and output files::
174 * Input Options:: How gcj finds files
175 * Encodings:: Options controlling source file encoding
176 * Warnings:: Options controlling warnings specific to gcj
177 * Linking:: Options for making an executable
178 * Code Generation:: Options controlling the output of gcj
179 * Configure-time Options:: Options you won't use
182 @c man begin OPTIONS gcj
184 @node Input and output files
185 @section Input and output files
187 A @command{gcj} command is like a @command{gcc} command, in that it
188 consists of a number of options and file names. The following kinds
189 of input file names are supported:
192 @item @var{file}.java
194 @item @var{file}.class
197 @itemx @var{file}.jar
198 An archive containing one or more @code{.class} files, all of
199 which are compiled. The archive may be compressed. Files in
200 an archive which don't end with @samp{.class} are treated as
201 resource files; they are compiled into the resulting object file
202 as @samp{core:} URLs.
204 A file containing a whitespace-separated list of input file names.
205 (Currently, these must all be @code{.java} source files, but that
207 Each named file is compiled, just as if it had been on the command line.
208 @item @var{library}.a
209 @itemx @var{library}.so
210 @itemx -l@var{libname}
211 Libraries to use when linking. See the @command{gcc} manual.
214 You can specify more than one input file on the @command{gcj} command line,
215 in which case they will all be compiled. If you specify a
216 @code{-o @var{FILENAME}}
217 option, all the input files will be compiled together, producing a
218 single output file, named @var{FILENAME}.
219 This is allowed even when using @code{-S} or @code{-c},
220 but not when using @code{-C} or @code{--resource}.
221 (This is an extension beyond the what plain @command{gcc} allows.)
222 (If more than one input file is specified, all must currently
223 be @code{.java} files, though we hope to fix this.)
226 @section Input Options
230 @command{gcj} has options to control where it looks to find files it needs.
231 For instance, @command{gcj} might need to load a class that is referenced
232 by the file it has been asked to compile. Like other compilers for the
233 Java language, @command{gcj} has a notion of a @dfn{class path}. There are
234 several options and environment variables which can be used to
235 manipulate the class path. When @command{gcj} looks for a given class, it
236 searches the class path looking for matching @file{.class} or
237 @file{.java} file. @command{gcj} comes with a built-in class path which
238 points at the installed @file{libgcj.jar}, a file which contains all the
241 In the below, a directory or path component can refer either to an
242 actual directory on the filesystem, or to a @file{.zip} or @file{.jar}
243 file, which @command{gcj} will search as if it is a directory.
247 All directories specified by @code{-I} are kept in order and prepended
248 to the class path constructed from all the other options. Unless
249 compatibility with tools like @code{javac} is important, we recommend
250 always using @code{-I} instead of the other options for manipulating the
253 @item --classpath=@var{path}
254 This sets the class path to @var{path}, a colon-separated list of paths
255 (on Windows-based systems, a semicolon-separate list of paths).
256 This does not override the builtin (``boot'') search path.
258 @item --CLASSPATH=@var{path}
259 Deprecated synonym for @code{--classpath}.
261 @item --bootclasspath=@var{path}
262 Where to find the standard builtin classes, such as @code{java.lang.String}.
264 @item --extdirs=@var{path}
265 For each directory in the @var{path}, place the contents of that
266 directory at the end of the class path.
269 This is an environment variable which holds a list of paths.
272 The final class path is constructed like so:
276 First come all directories specified via @code{-I}.
279 If @option{--classpath} is specified, its value is appended.
280 Otherwise, if the @code{CLASSPATH} environment variable is specified,
281 then its value is appended.
282 Otherwise, the current directory (@code{"."}) is appended.
285 If @code{--bootclasspath} was specified, append its value.
286 Otherwise, append the built-in system directory, @file{libgcj.jar}.
289 Finally, if @code{--extdirs} was specified, append the contents of the
290 specified directories at the end of the class path. Otherwise, append
291 the contents of the built-in extdirs at @code{$(prefix)/share/java/ext}.
294 The classfile built by @command{gcj} for the class @code{java.lang.Object}
295 (and placed in @code{libgcj.jar}) contains a special zero length
296 attribute @code{gnu.gcj.gcj-compiled}. The compiler looks for this
297 attribute when loading @code{java.lang.Object} and will report an error
298 if it isn't found, unless it compiles to bytecode (the option
299 @code{-fforce-classes-archive-check} can be used to override this
300 behavior in this particular case.)
303 @item -fforce-classes-archive-check
304 This forces the compiler to always check for the special zero length
305 attribute @code{gnu.gcj.gcj-compiled} in @code{java.lang.Object} and
306 issue an error if it isn't found.
308 @item -fsource=@var{VERSION}
309 This option is used to choose the source version accepted by
310 @command{gcj}. The default is @samp{1.5}.
316 The Java programming language uses Unicode throughout. In an effort to
317 integrate well with other locales, @command{gcj} allows @file{.java} files
318 to be written using almost any encoding. @command{gcj} knows how to
319 convert these encodings into its internal encoding at compile time.
321 You can use the @code{--encoding=@var{NAME}} option to specify an
322 encoding (of a particular character set) to use for source files. If
323 this is not specified, the default encoding comes from your current
324 locale. If your host system has insufficient locale support, then
325 @command{gcj} assumes the default encoding to be the @samp{UTF-8} encoding
328 To implement @code{--encoding}, @command{gcj} simply uses the host
329 platform's @code{iconv} conversion routine. This means that in practice
330 @command{gcj} is limited by the capabilities of the host platform.
332 The names allowed for the argument @code{--encoding} vary from platform
333 to platform (since they are not standardized anywhere). However,
334 @command{gcj} implements the encoding named @samp{UTF-8} internally, so if
335 you choose to use this for your source files you can be assured that it
336 will work on every host.
342 @command{gcj} implements several warnings. As with other generic
343 @command{gcc} warnings, if an option of the form @code{-Wfoo} enables a
344 warning, then @code{-Wno-foo} will disable it. Here we've chosen to
345 document the form of the warning which will have an effect -- the
346 default being the opposite of what is listed.
349 @item -Wredundant-modifiers
350 With this flag, @command{gcj} will warn about redundant modifiers. For
351 instance, it will warn if an interface method is declared @code{public}.
353 @item -Wextraneous-semicolon
354 This causes @command{gcj} to warn about empty statements. Empty statements
355 have been deprecated.
357 @item -Wno-out-of-date
358 This option will cause @command{gcj} not to warn when a source file is
359 newer than its matching class file. By default @command{gcj} will warn
362 @item -Wno-deprecated
363 Warn if a deprecated class, method, or field is referred to.
366 This is the same as @command{gcc}'s @code{-Wunused}.
369 This is the same as @code{-Wredundant-modifiers -Wextraneous-semicolon
377 To turn a Java application into an executable program,
378 you need to link it with the needed libraries, just as for C or C++.
379 The linker by default looks for a global function named @code{main}.
380 Since Java does not have global functions, and a
381 collection of Java classes may have more than one class with a
382 @code{main} method, you need to let the linker know which of those
383 @code{main} methods it should invoke when starting the application.
384 You can do that in any of these ways:
388 Specify the class containing the desired @code{main} method
389 when you link the application, using the @code{--main} flag,
392 Link the Java package(s) into a shared library (dll) rather than an
393 executable. Then invoke the application using the @code{gij} program,
394 making sure that @code{gij} can find the libraries it needs.
396 Link the Java packages(s) with the flag @code{-lgij}, which links
397 in the @code{main} routine from the @code{gij} command.
398 This allows you to select the class whose @code{main} method you
399 want to run when you run the application. You can also use
400 other @code{gij} flags, such as @code{-D} flags to set properties.
401 Using the @code{-lgij} library (rather than the @code{gij} program
402 of the previous mechanism) has some advantages: it is compatible with
403 static linking, and does not require configuring or installing libraries.
406 These @code{gij} options relate to linking an executable:
409 @item --main=@var{CLASSNAME}
410 This option is used when linking to specify the name of the class whose
411 @code{main} method should be invoked when the resulting executable is
414 @item -D@var{name}[=@var{value}]
415 This option can only be used with @code{--main}. It defines a system
416 property named @var{name} with value @var{value}. If @var{value} is not
417 specified then it defaults to the empty string. These system properties
418 are initialized at the program's startup and can be retrieved at runtime
419 using the @code{java.lang.System.getProperty} method.
422 Create an application whose command-line processing is that
423 of the @code{gij} command.
425 This option is an alternative to using @code{--main}; you cannot use both.
428 This option causes linking to be done against a static version of the
429 libgcj runtime library. This option is only available if
430 corresponding linker support exists.
432 @strong{Caution:} Static linking of libgcj may cause essential parts
433 of libgcj to be omitted. Some parts of libgcj use reflection to load
434 classes at runtime. Since the linker does not see these references at
435 link time, it can omit the referred to classes. The result is usually
436 (but not always) a @code{ClassNotFoundException} being thrown at
437 runtime. Caution must be used when using this option. For more
439 @w{@uref{http://gcc.gnu.org/wiki/Statically%20linking%20libgcj}}
442 @node Code Generation
443 @section Code Generation
445 In addition to the many @command{gcc} options controlling code generation,
446 @command{gcj} has several options specific to itself.
451 This option is used to tell @command{gcj} to generate bytecode
452 (@file{.class} files) rather than object code.
454 @item --resource @var{resource-name}
455 This option is used to tell @command{gcj} to compile the contents of a
456 given file to object code so it may be accessed at runtime with the core
457 protocol handler as @samp{core:/@var{resource-name}}. Note that
458 @var{resource-name} is the name of the resource as found at runtime; for
459 instance, it could be used in a call to @code{ResourceBundle.getBundle}.
460 The actual file name to be compiled this way must be specified
463 @item -ftarget=@var{VERSION}
464 This can be used with @option{-C} to choose the version of bytecode
465 emitted by @command{gcj}. The default is @samp{1.5}. When not
466 generating bytecode, this option has no effect.
468 @item -d @var{directory}
469 When used with @code{-C}, this causes all generated @file{.class} files
470 to be put in the appropriate subdirectory of @var{directory}. By
471 default they will be put in subdirectories of the current working
474 @item -fno-bounds-check
475 By default, @command{gcj} generates code which checks the bounds of all
476 array indexing operations. With this option, these checks are omitted, which
477 can improve performance for code that uses arrays extensively. Note that this
478 can result in unpredictable behavior if the code in question actually does
479 violate array bounds constraints. It is safe to use this option if you are
480 sure that your code will never throw an @code{ArrayIndexOutOfBoundsException}.
482 @item -fno-store-check
483 Don't generate array store checks. When storing objects into arrays, a runtime
484 check is normally generated in order to ensure that the object is assignment
485 compatible with the component type of the array (which may not be known
486 at compile-time). With this option, these checks are omitted. This can
487 improve performance for code which stores objects into arrays frequently.
488 It is safe to use this option if you are sure your code will never throw an
489 @code{ArrayStoreException}.
492 With @command{gcj} there are two options for writing native methods: CNI
493 and JNI@. By default @command{gcj} assumes you are using CNI@. If you are
494 compiling a class with native methods, and these methods are implemented
495 using JNI, then you must use @code{-fjni}. This option causes
496 @command{gcj} to generate stubs which will invoke the underlying JNI
500 Don't recognize the @code{assert} keyword. This is for compatibility
501 with older versions of the language specification.
503 @item -fno-optimize-static-class-initialization
504 When the optimization level is greater or equal to @code{-O2},
505 @command{gcj} will try to optimize the way calls into the runtime are made
506 to initialize static classes upon their first use (this optimization
507 isn't carried out if @code{-C} was specified.) When compiling to native
508 code, @code{-fno-optimize-static-class-initialization} will turn this
509 optimization off, regardless of the optimization level in use.
511 @item --disable-assertions[=@var{class-or-package}]
512 Don't include code for checking assertions in the compiled code.
513 If @code{=@var{class-or-package}} is missing disables assertion code
514 generation for all classes, unless overridden by a more
515 specific @code{--enable-assertions} flag.
516 If @var{class-or-package} is a class name, only disables generating
517 assertion checks within the named class or its inner classes.
518 If @var{class-or-package} is a package name, disables generating
519 assertion checks within the named package or a subpackage.
521 By default, assertions are enabled when generating class files
522 or when not optimizing, and disabled when generating optimized binaries.
524 @item --enable-assertions[=@var{class-or-package}]
525 Generates code to check assertions. The option is perhaps misnamed,
526 as you still need to turn on assertion checking at run-time,
527 and we don't support any easy way to do that.
528 So this flag isn't very useful yet, except to partially override
529 @code{--disable-assertions}.
531 @item -findirect-dispatch
532 @command{gcj} has a special binary compatibility ABI, which is enabled
533 by the @code{-findirect-dispatch} option. In this mode, the code
534 generated by @command{gcj} honors the binary compatibility guarantees
535 in the Java Language Specification, and the resulting object files do
536 not need to be directly linked against their dependencies. Instead,
537 all dependencies are looked up at runtime. This allows free mixing of
538 interpreted and compiled code.
540 Note that, at present, @code{-findirect-dispatch} can only be used
541 when compiling @file{.class} files. It will not work when compiling
542 from source. CNI also does not yet work with the binary compatibility
543 ABI. These restrictions will be lifted in some future release.
545 However, if you compile CNI code with the standard ABI, you can call
546 it from code built with the binary compatibility ABI.
548 @item -fbootstrap-classes
549 This option can be use to tell @code{libgcj} that the compiled classes
550 should be loaded by the bootstrap loader, not the system class loader.
551 By default, if you compile a class and link it into an executable, it
552 will be treated as if it was loaded using the system class loader.
553 This is convenient, as it means that things like
554 @code{Class.forName()} will search @samp{CLASSPATH} to find the
557 @item -freduced-reflection
558 This option causes the code generated by @command{gcj} to contain a
559 reduced amount of the class meta-data used to support runtime
560 reflection. The cost of this savings is the loss of
561 the ability to use certain reflection capabilities of the standard
562 Java runtime environment. When set all meta-data except for that
563 which is needed to obtain correct runtime semantics is eliminated.
565 For code that does not use reflection (i.e. the methods in the
566 @code{java.lang.reflect} package), @code{-freduced-reflection}
567 will result in proper operation with a savings in executable code size.
569 JNI (@code{-fjni}) and the binary compatibility ABI
570 (@code{-findirect-dispatch}) do not work properly without full
571 reflection meta-data. Because of this, it is an error to use these options
572 with @code{-freduced-reflection}.
574 @strong{Caution:} If there is no reflection meta-data, code that uses
575 a @code{SecurityManager} may not work properly. Also calling
576 @code{Class.forName()} may fail if the calling method has no
577 reflection meta-data.
582 @node Configure-time Options
583 @section Configure-time Options
585 Some @command{gcj} code generations options affect the resulting ABI, and
586 so can only be meaningfully given when @code{libgcj}, the runtime
587 package, is configured. @code{libgcj} puts the appropriate options from
588 this group into a @samp{spec} file which is read by @command{gcj}. These
589 options are listed here for completeness; if you are using @code{libgcj}
590 then you won't want to touch these options.
594 This enables the use of the Boehm GC bitmap marking code. In particular
595 this causes @command{gcj} to put an object marking descriptor into each
598 @item -fhash-synchronization
599 By default, synchronization data (the data used for @code{synchronize},
600 @code{wait}, and @code{notify}) is pointed to by a word in each object.
601 With this option @command{gcj} assumes that this information is stored in a
602 hash table and not in the object itself.
604 @item -fuse-divide-subroutine
605 On some systems, a library routine is called to perform integer
606 division. This is required to get exception handling correct when
609 @item -fcheck-references
610 On some systems it's necessary to insert inline checks whenever
611 accessing an object via a reference. On other systems you won't need
612 this because null pointer accesses are caught automatically by the
619 @chapter Compatibility with the Java Platform
621 As we believe it is important that the Java platform not be fragmented,
622 @command{gcj} and @code{libgcj} try to conform to the relevant Java
623 specifications. However, limited manpower and incomplete and unclear
624 documentation work against us. So, there are caveats to using
633 @section Standard features not yet supported
635 This list of compatibility issues is by no means complete.
639 @command{gcj} implements the JDK 1.2 language. It supports inner classes
640 and the new 1.4 @code{assert} keyword. It does not yet support the Java 2
641 @code{strictfp} keyword (it recognizes the keyword but ignores it).
644 @code{libgcj} is largely compatible with the JDK 1.2 libraries.
645 However, @code{libgcj} is missing many packages, most notably
646 @code{java.awt}. There are also individual missing classes and methods.
647 We currently do not have a list showing differences between
648 @code{libgcj} and the Java 2 platform.
651 Sometimes the @code{libgcj} implementation of a method or class differs
652 from the JDK implementation. This is not always a bug. Still, if it
653 affects you, it probably makes sense to report it so that we can discuss
654 the appropriate response.
657 @command{gcj} does not currently allow for piecemeal replacement of
658 components within @code{libgcj}. Unfortunately, programmers often want
659 to use newer versions of certain packages, such as those provided by
660 the Apache Software Foundation's Jakarta project. This has forced us
661 to place the @code{org.w3c.dom} and @code{org.xml.sax} packages into
662 their own libraries, separate from @code{libgcj}. If you intend to
663 use these classes, you must link them explicitly with
664 @code{-l-org-w3c-dom} and @code{-l-org-xml-sax}. Future versions of
665 @command{gcj} may not have this restriction.
669 @section Extra features unique to gcj
671 The main feature of @command{gcj} is that it can compile programs written in
672 the Java programming language to native code. Most extensions that have been
673 added are to facilitate this functionality.
677 @command{gcj} makes it easy and efficient to mix code written in Java and C++.
678 @xref{About CNI}, for more info on how to use this in your programs.
681 When you compile your classes into a shared library using
682 @code{-findirect-dispatch} then add them to the system-wide
683 classmap.db file using @code{gcj-dbtool}, they will be automatically
684 loaded by the @code{libgcj} system classloader. This is the new,
685 preferred classname-to-library resolution mechanism. @xref{Invoking
686 gcj-dbtool}, for more information on using the classmap database.
689 The old classname-to-library lookup mechanism is still supported
690 through the @code{gnu.gcj.runtime.VMClassLoader.library_control}
691 property, but it is deprecated and will likely be removed in some
692 future release. When trying to load a class @code{gnu.pkg.SomeClass}
693 the system classloader will first try to load the shared library
694 @file{lib-gnu-pkg-SomeClass.so}, if that fails to load the class then
695 it will try to load @file{lib-gnu-pkg.so} and finally when the class
696 is still not loaded it will try to load @file{lib-gnu.so}. Note that
697 all @samp{.}s will be transformed into @samp{-}s and that searching
698 for inner classes starts with their outermost outer class. If the
699 class cannot be found this way the system classloader tries to use the
700 @code{libgcj} bytecode interpreter to load the class from the standard
701 classpath. This process can be controlled to some degree via the
702 @code{gnu.gcj.runtime.VMClassLoader.library_control} property;
703 @xref{libgcj Runtime Properties}.
706 @code{libgcj} includes a special @samp{gcjlib} URL type. A URL of
707 this form is like a @code{jar} URL, and looks like
708 @samp{gcjlib:/path/to/shared/library.so!/path/to/resource}. An access
709 to one of these URLs causes the shared library to be @code{dlopen()}d,
710 and then the resource is looked for in that library. These URLs are
711 most useful when used in conjunction with @code{java.net.URLClassLoader}.
712 Note that, due to implementation limitations, currently any such URL
713 can be accessed by only one class loader, and libraries are never
714 unloaded. This means some care must be exercised to make sure that
715 a @code{gcjlib} URL is not accessed by more than one class loader at once.
716 In a future release this limitation will be lifted, and such
717 libraries will be mapped privately.
720 A program compiled by @command{gcj} will examine the
721 @env{GCJ_PROPERTIES} environment variable and change its behavior in
722 some ways. In particular @env{GCJ_PROPERTIES} holds a list of
723 assignments to global properties, such as would be set with the
724 @option{-D} option to @command{java}. For instance,
725 @samp{java.compiler=gcj} is a valid (but currently meaningless)
727 @cindex GCJ_PROPERTIES
728 @vindex GCJ_PROPERTIES
734 @chapter Invoking gcjh
736 @c man title gcjh generate header files from Java class files
738 @c man begin DESCRIPTION gcjh
740 The @code{gcjh} program is used to generate header files from class
741 files. It can generate both CNI and JNI header files, as well as stub
742 implementation files which can be used as a basis for implementing the
743 required native methods.
748 @c man begin SYNOPSIS gcjh
749 gcjh [@option{-stubs}] [@option{-jni}]
750 [@option{-force}] [@option{-old}] [@option{-trace}] [@option{-J} @var{option}]
751 [@option{-add} @var{text}] [@option{-append} @var{text}] [@option{-friend} @var{text}]
752 [@option{-prepend} @var{text}]
753 [@option{--classpath}=@var{path}] [@option{--CLASSPATH}=@var{path}]
754 [@option{--bootclasspath}=@var{path}]
755 [@option{-I}@var{dir}@dots{}] [@option{-d} @var{dir}@dots{}]
756 [@option{-o} @var{file}] [@option{-td} @var{dir}]
757 [@option{-M}] [@option{-MM}] [@option{-MD}] [@option{-MMD}]
758 [@option{--version}] [@option{--help}] [@option{-v}] [@option{--verbose}]
759 @var{classname}@dots{}
761 @c man begin SEEALSO gcjh
762 gcc(1), gcj(1), gij(1), jcf-dump(1), gfdl(7),
763 and the Info entries for @file{gcj} and @file{gcc}.
767 @c man begin OPTIONS gcjh
771 This causes @code{gcjh} to generate stub files instead of header files.
772 By default the stub file will be named after the class, with a suffix of
773 @samp{.cc}. In JNI mode, the default output file will have the suffix
777 This tells @code{gcjh} to generate a JNI header or stub. By default,
778 CNI headers are generated.
781 This option forces @code{gcjh} to write the output file.
784 This option is accepted but ignored for compatibility.
787 This option is accepted but ignored for compatibility.
789 @item -J @var{option}
790 This option is accepted but ignored for compatibility.
792 @item -add @var{text}
793 Inserts @var{text} into the class body. This is ignored in JNI mode.
795 @item -append @var{text}
796 Inserts @var{text} into the header file after the class declaration.
797 This is ignored in JNI mode.
799 @item -friend @var{text}
800 Inserts @var{text} into the class as a @code{friend} declaration.
801 This is ignored in JNI mode.
803 @item -prepend @var{text}
804 Inserts @var{text} into the header file before the class declaration.
805 This is ignored in JNI mode.
807 @item --classpath=@var{path}
808 @itemx --CLASSPATH=@var{path}
809 @itemx --bootclasspath=@var{path}
810 @itemx -I@var{directory}
811 @itemx -d @var{directory}
813 These options are all identical to the corresponding @command{gcj} options.
816 Sets the output file name. This cannot be used if there is more than
817 one class on the command line.
819 @item -td @var{directory}
820 Sets the name of the directory to use for temporary files.
823 Print all dependencies to stdout; suppress ordinary output.
826 Print non-system dependencies to stdout; suppress ordinary output.
829 Print all dependencies to stdout.
832 Print non-system dependencies to stdout.
835 Print help about @code{gcjh} and exit. No further processing is done.
838 Print version information for @code{gcjh} and exit. No further
842 Print extra information while running.
845 All remaining options are considered to be names of classes.
850 @chapter Invoking gjnih
852 @c man title gjnih generate JNI header files from Java class files
854 @c man begin DESCRIPTION gjnih
856 The @code{gjnih} program is used to generate JNI header files from class
857 files. Running it is equivalent to running @code{gcjh -jni}.
862 @c man begin SYNOPSIS gjnih
863 gjnih [@option{-stubs}] [@option{-jni}]
864 [@option{-force}] [@option{-old}] [@option{-trace}] [@option{-J} @var{option}]
865 [@option{-add} @var{text}] [@option{-append} @var{text}] [@option{-friend} @var{text}]
866 [@option{-prepend} @var{text}]
867 [@option{--classpath}=@var{path}] [@option{--CLASSPATH}=@var{path}]
868 [@option{--bootclasspath}=@var{path}]
869 [@option{-I}@var{dir}@dots{}] [@option{-d} @var{dir}@dots{}]
870 [@option{-o} @var{file}] [@option{-td} @var{dir}]
871 [@option{-M}] [@option{-MM}] [@option{-MD}] [@option{-MMD}]
872 [@option{--version}] [@option{--help}] [@option{-v}] [@option{--verbose}]
873 @var{classname}@dots{}
875 @c man begin SEEALSO gjnih
876 gcc(1), gcj(1), gcjh(1), gij(1), jcf-dump(1), gfdl(7),
877 and the Info entries for @file{gcj} and @file{gcc}.
881 @c man begin OPTIONS gjnih
885 This causes @code{gjnih} to generate stub files instead of header files.
886 By default the stub file will be named after the class, with a suffix of
890 This option specifies the default behavior which is to generate a JNI
894 This option forces @code{gjnih} to write the output file.
897 This option is accepted but ignored for compatibility.
900 This option is accepted but ignored for compatibility.
902 @item -J @var{option}
903 This option is accepted but ignored for compatibility.
905 @item -add @var{text}
906 Inserts @var{text} into the class body. This is ignored in by
909 @item -append @var{text}
910 Inserts @var{text} into the header file after the class declaration.
911 This is ignored in by @code{gjnih}.
913 @item -friend @var{text}
914 Inserts @var{text} into the class as a @code{friend} declaration.
915 This is ignored by @code{gjnih}.
917 @item -prepend @var{text}
918 Inserts @var{text} into the header file before the class declaration.
919 This is ignored in by @code{gjnih}.
921 @item --classpath=@var{path}
922 @itemx --CLASSPATH=@var{path}
923 @itemx --bootclasspath=@var{path}
924 @itemx -I@var{directory}
925 @itemx -d @var{directory}
927 These options are all identical to the corresponding @command{gcj} options.
930 Sets the output file name. This cannot be used if there is more than
931 one class on the command line.
933 @item -td @var{directory}
934 Sets the name of the directory to use for temporary files.
937 Print all dependencies to stdout; suppress ordinary output.
940 Print non-system dependencies to stdout; suppress ordinary output.
943 Print all dependencies to stdout.
946 Print non-system dependencies to stdout.
949 Print help about @code{gjnih} and exit. No further processing is done.
952 Print version information for @code{gjnih} and exit. No further
956 Print extra information while running.
959 All remaining options are considered to be names of classes.
963 @node Invoking jcf-dump
964 @chapter Invoking jcf-dump
966 @c man title jcf-dump print information about Java class files
969 @c man begin SYNOPSIS jcf-dump
970 jcf-dump [@option{-c}] [@option{--javap}]
971 [@option{--classpath}=@var{path}] [@option{--CLASSPATH}=@var{path}]
972 [@option{-I}@var{dir}@dots{}] [@option{-o} @var{file}]
973 [@option{--version}] [@option{--help}] [@option{-v}] [@option{--verbose}]
974 @var{classname}@dots{}
976 @c man begin SEEALSO jcf-dump
977 gcc(1), gcj(1), gcjh(1), gij(1), jcf-dump(1), gfdl(7),
978 and the Info entries for @file{gcj} and @file{gcc}.
982 @c man begin DESCRIPTION jcf-dump
984 This is a class file examiner, similar to @code{javap}. It will print
985 information about a number of classes, which are specified by class name
990 @c man begin OPTIONS jcf-dump
994 Disassemble method bodies. By default method bodies are not printed.
996 @item --print-constants
997 Print the constant pool. When printing a reference to a constant
998 also print its index in the constant pool.
1001 Generate output in @code{javap} format. The implementation of this
1002 feature is very incomplete.
1004 @item --classpath=@var{path}
1005 @itemx --CLASSPATH=@var{path}
1006 @itemx -I@var{directory}
1007 @itemx -o @var{file}
1008 These options as the same as the corresponding @command{gcj} options.
1011 Print help, then exit.
1014 Print version number, then exit.
1017 Print extra information while running.
1018 Implies @code{--print-constants}.
1024 @chapter Invoking gij
1026 @c man title gij GNU interpreter for Java bytecode
1029 @c man begin SYNOPSIS gij
1030 gij [@option{OPTION}] @dots{} @var{JARFILE} [@var{ARGS}@dots{}]
1032 gij [@option{-jar}] [@option{OPTION}] @dots{} @var{CLASS} [@var{ARGS}@dots{}]
1033 [@option{-cp} @var{path}] [@option{-classpath} @var{path}]
1034 [@option{-D}@var{name}[=@var{value}]@dots{}]
1035 [@option{-ms=}@var{number}] [@option{-mx=}@var{number}]
1036 [@option{-X@var{argument}}] [@option{-verbose}] [@option{-verbose:class}]
1037 [@option{--showversion}] [@option{--version}] [@option{--help}][@option{-?}]
1039 @c man begin SEEALSO gij
1040 gcc(1), gcj(1), gcjh(1), jcf-dump(1), gfdl(7),
1041 and the Info entries for @file{gcj} and @file{gcc}.
1045 @c man begin DESCRIPTION gij
1047 @code{gij} is a Java bytecode interpreter included with @code{libgcj}.
1048 @code{gij} is not available on every platform; porting it requires a
1049 small amount of assembly programming which has not been done for all the
1050 targets supported by @command{gcj}.
1052 The primary argument to @code{gij} is the name of a class or, with
1053 @code{-jar}, a jar file. Options before this argument are interpreted
1054 by @code{gij}; remaining options are passed to the interpreted program.
1056 If a class name is specified and this class does not have a @code{main}
1057 method with the appropriate signature (a @code{static void} method with
1058 a @code{String[]} as its sole argument), then @code{gij} will print an
1061 If a jar file is specified then @code{gij} will use information in it to
1062 determine which class' @code{main} method will be invoked.
1064 @code{gij} will invoke the @code{main} method with all the remaining
1065 command-line options.
1067 Note that @code{gij} is not limited to interpreting code. Because
1068 @code{libgcj} includes a class loader which can dynamically load shared
1069 objects, it is possible to give @code{gij} the name of a class which has
1070 been compiled and put into a shared library on the class path.
1074 @c man begin OPTIONS gij
1077 @item -cp @var{path}
1078 @itemx -classpath @var{path}
1079 Set the initial class path. The class path is used for finding
1080 class and resource files. If specified, this option overrides the
1081 @code{CLASSPATH} environment variable. Note that this option is
1082 ignored if @code{-jar} is used.
1084 @item -D@var{name}[=@var{value}]
1085 This defines a system property named @var{name} with value @var{value}.
1086 If @var{value} is not specified then it defaults to the empty string.
1087 These system properties are initialized at the program's startup and can
1088 be retrieved at runtime using the @code{java.lang.System.getProperty}
1091 @item -ms=@var{number}
1092 Equivalent to @code{-Xms}.
1094 @item -mx=@var{number}
1095 Equivalent to @code{-Xmx}.
1098 Do not verify compliance of bytecode with the VM specification. In addition,
1099 this option disables type verification which is otherwise performed on BC-ABI
1103 @itemx -X@var{argument}
1104 Supplying @code{-X} by itself will cause @code{gij} to list all the
1105 supported @code{-X} options. Currently these options are supported:
1108 @item -Xms@var{size}
1109 Set the initial heap size.
1111 @item -Xmx@var{size}
1112 Set the maximum heap size.
1114 @item -Xss@var{size}
1115 Set the thread stack size.
1118 Unrecognized @code{-X} options are ignored, for compatibility with
1122 This indicates that the name passed to @code{gij} should be interpreted
1123 as the name of a jar file, not a class.
1127 Print help, then exit.
1130 Print version number and continue.
1133 Print detailed version information, then exit.
1136 Print version number, then exit.
1139 @itemx -verbose:class
1140 Each time a class is initialized, print a short message on standard error.
1143 @code{gij} also recognizes and ignores the following options, for
1144 compatibility with existing application launch scripts:
1145 @code{-client}, @code{-server}, @code{-hotspot}, @code{-jrockit},
1146 @code{-agentlib}, @code{-agentpath}, @code{-debug}, @code{-d32},
1147 @code{-d64}, @code{-javaagent}, @code{-noclassgc}, @code{-verify},
1148 and @code{-verifyremote}.
1152 @node Invoking gcj-dbtool
1153 @chapter Invoking gcj-dbtool.
1155 @c man title gcj-dbtool Manipulate class file mapping databases for libgcj
1158 @c man begin SYNOPSIS gcj-dbtool
1159 gcj-dbtool @option{OPTION} @var{DBFILE} [@option{MORE}] @dots{}
1161 gcj-dbtool [@option{-0}] [@option{-}] [@option{-n}] [@option{-a}] [@option{-f}]
1162 [@option{-t}] [@option{-l}] [@option{-p} [@var{LIBDIR}]]
1163 [@option{-v}] [@option{-m}] [@option{--version}] [@option{--help}]
1166 @c man begin SEEALSO gij
1167 gcc(1), gcj(1), gcjh(1), jcf-dump(1), gfdl(7),
1168 and the Info entries for @file{gcj} and @file{gcc}.
1172 @c man begin DESCRIPTION gcj-dbtool
1174 @code{gcj-dbtool} is a tool for creating and manipulating class file
1175 mapping databases. @code{libgcj} can use these databases to find a
1176 shared library corresponding to the bytecode representation of a
1177 class. This functionality is useful for ahead-of-time compilation of
1178 a program that has no knowledge of @code{gcj}.
1180 @code{gcj-dbtool} works best if all the jar files added to it are
1181 compiled using @code{-findirect-dispatch}.
1183 Note that @code{gcj-dbtool} is currently available as ``preview
1184 technology''. We believe it is a reasonable way to allow
1185 application-transparent ahead-of-time compilation, but this is an
1186 unexplored area. We welcome your comments.
1190 @c man begin OPTIONS gcj-dbtool
1193 @item -n @var{DBFILE} [@var{SIZE}]
1194 This creates a new database. Currently, databases cannot be resized;
1195 you can choose a larger initial size if desired. The default size is
1198 @item -a @var{DBFILE} @var{JARFILE} @var{LIB}
1199 @itemx -f @var{DBFILE} @var{JARFILE} @var{LIB}
1200 This adds a jar file to the database. For each class file in the jar,
1201 a cryptographic signature of the bytecode representation of the class
1202 is recorded in the database. At runtime, a class is looked up by its
1203 signature and the compiled form of the class is looked for in the
1204 corresponding shared library. The @option{-a} option will verify
1205 that @var{LIB} exists before adding it to the database; @option{-f}
1208 @item [@option{-}][@option{-0}] -m @var{DBFILE} @var{DBFILE},[@var{DBFILE}]
1209 Merge a number of databases. The output database overwrites any
1210 existing database. To add databases into an existing database,
1211 include the destination in the list of sources.
1213 If @option{-} or @option{-0} are used, the list of files to read is
1214 taken from standard input instead of the command line. For
1215 @option{-0}, Input filenames are terminated by a null character
1216 instead of by whitespace. Useful when arguments might contain white
1217 space. The GNU find -print0 option produces input suitable for this
1220 @item -t @var{DBFILE}
1223 @item -l @var{DBFILE}
1224 List the contents of a database.
1227 Print the name of the default database. If there is no default
1228 database, this prints a blank line. If @var{LIBDIR} is specified, use
1229 it instead of the default library directory component of the database
1233 Print a help message, then exit.
1237 Print version information, then exit.
1243 @node Invoking jv-convert
1244 @chapter Invoking jv-convert
1246 @c man title jv-convert Convert file from one encoding to another
1248 @c man begin SYNOPSIS jv-convert
1249 @command{jv-convert} [@option{OPTION}] @dots{} [@var{INPUTFILE} [@var{OUTPUTFILE}]]
1252 [@option{--encoding} @var{name}]
1253 [@option{--from} @var{name}]
1254 [@option{--to} @var{name}]
1255 [@option{-i} @var{file}] [@option{-o} @var{file}]
1256 [@option{--reverse}] [@option{--help}] [@option{--version}]
1260 @c man begin DESCRIPTION jv-convert
1262 @command{jv-convert} is a utility included with @code{libgcj} which
1263 converts a file from one encoding to another. It is similar to the Unix
1264 @command{iconv} utility.
1266 The encodings supported by @command{jv-convert} are platform-dependent.
1267 Currently there is no way to get a list of all supported encodings.
1271 @c man begin OPTIONS jv-convert
1274 @item --encoding @var{name}
1275 @itemx --from @var{name}
1276 Use @var{name} as the input encoding. The default is the current
1279 @item --to @var{name}
1280 Use @var{name} as the output encoding. The default is the
1281 @code{JavaSrc} encoding; this is ASCII with @samp{\u} escapes for
1282 non-ASCII characters.
1285 Read from @var{file}. The default is to read from standard input.
1288 Write to @var{file}. The default is to write to standard output.
1291 Swap the input and output encodings.
1294 Print a help message, then exit.
1297 Print version information, then exit.
1302 @node Invoking grmic
1303 @chapter Invoking grmic
1305 @c man title grmic Generate stubs for Remote Method Invocation
1307 @c man begin SYNOPSIS grmic
1308 @command{grmic} [@option{OPTION}] @dots{} @var{class} @dots{}
1311 [@option{-keepgenerated}]
1315 [@option{-nocompile}]
1317 [@option{-d} @var{directory}]
1323 @c man begin DESCRIPTION grmic
1325 @command{grmic} is a utility included with @code{libgcj} which generates
1326 stubs for remote objects.
1328 @c FIXME: Add real information here.
1329 @c This really isn't much more than the --help output.
1331 Note that this program isn't yet fully compatible with the JDK
1332 @command{grmic}. Some options, such as @option{-classpath}, are
1333 recognized but currently ignored. We have left these options
1334 undocumented for now.
1336 Long options can also be given with a GNU-style leading @samp{--}. For
1337 instance, @option{--help} is accepted.
1341 @c man begin OPTIONS grmic
1345 @itemx -keepgenerated
1346 By default, @command{grmic} deletes intermediate files. Either of these
1347 options causes it not to delete such files.
1350 Cause @command{grmic} to create stubs and skeletons for the 1.1
1354 Cause @command{grmic} to create stubs and skeletons compatible with both
1355 the 1.1 and 1.2 protocol versions. This is the default.
1358 Cause @command{grmic} to create stubs and skeletons for the 1.2
1362 Don't compile the generated files.
1365 Print information about what @command{grmic} is doing.
1367 @item -d @var{directory}
1368 Put output files in @var{directory}. By default the files are put in
1369 the current working directory.
1372 Print a help message, then exit.
1375 Print version information, then exit.
1381 @node Invoking grmiregistry
1382 @chapter Invoking grmiregistry
1384 @c man title grmiregistry Remote object registry
1386 @c man begin SYNOPSIS grmiregistry
1387 @command{grmic} [@option{OPTION}] @dots{} [@var{port}]
1390 [@option{--version}]
1394 @c man begin DESCRIPTION grmiregistry
1396 @command{grmiregistry} starts a remote object registry on the current
1397 host. If no port number is specified, then port 1099 is used.
1399 @c FIXME: Add real information here.
1400 @c This really isn't much more than the --help output.
1404 @c man begin OPTIONS grmiregistry
1408 Print a help message, then exit.
1411 Print version information, then exit.
1417 @node Invoking gc-analyze
1418 @chapter Invoking gc-analyze
1420 @c man title gc-analyze Analyze Garbage Collector (GC) memory dumps
1422 @c man begin SYNOPSIS gc-analyze
1423 @command{gc-analyze} [@option{OPTION}] @dots{} [@var{file}]
1426 [@option{--verbose}]
1427 [@option{-p} @var{tool-prefix}]
1428 [@option{-d} @var{directory}]
1429 [@option{--version}]
1434 @c man begin DESCRIPTION gc-analyze
1436 @command{gc-analyze} prints an analysis of a GC memory dump to
1439 The memory dumps may be created by calling
1440 @code{gnu.gcj.util.GCInfo.enumerate(String namePrefix)} from java
1441 code. A memory dump will be created on an out of memory condition if
1442 @code{gnu.gcj.util.GCInfo.setOOMDump(String namePrefix)} is called
1443 before the out of memory occurs.
1445 Running this program will create two files: @file{TestDump001} and
1446 @file{TestDump001.bytes}.
1449 import gnu.gcj.util.*;
1452 public class GCDumpTest
1454 static public void main(String args[])
1456 ArrayList<String> l = new ArrayList<String>(1000);
1458 for (int i = 1; i < 1500; i++) @{
1459 l.add("This is string #" + i);
1461 GCInfo.enumerate("TestDump");
1466 The memory dump may then be displayed by running:
1469 gc-analyze -v TestDump001
1472 @c FIXME: Add real information here.
1473 @c This really isn't much more than the --help output.
1477 @c man begin OPTIONS gc-analyze
1484 @item -p @var{tool-prefix}
1485 Prefix added to the names of the @command{nm} and @command{readelf} commands.
1487 @item -d @var{directory}
1488 Directory that contains the executable and shared libraries used when
1489 the dump was generated.
1492 Print a help message, then exit.
1495 Print version information, then exit.
1503 This documents CNI, the Compiled Native Interface,
1504 which is is a convenient way to write Java native methods using C++.
1505 This is a more efficient, more convenient, but less portable
1506 alternative to the standard JNI (Java Native Interface).
1509 * Basic concepts:: Introduction to using CNI@.
1510 * Packages:: How packages are mapped to C++.
1511 * Primitive types:: Handling primitive Java types in C++.
1512 * Reference types:: Handling Java reference types in C++.
1513 * Interfaces:: How Java interfaces map to C++.
1514 * Objects and Classes:: C++ and Java classes.
1515 * Class Initialization:: How objects are initialized.
1516 * Object allocation:: How to create Java objects in C++.
1517 * Memory allocation:: How to allocate and free memory.
1518 * Arrays:: Dealing with Java arrays in C++.
1519 * Methods:: Java methods in C++.
1520 * Strings:: Information about Java Strings.
1521 * Mixing with C++:: How CNI can interoperate with C++.
1522 * Exception Handling:: How exceptions are handled.
1523 * Synchronization:: Synchronizing between Java and C++.
1524 * Invocation:: Starting the Java runtime from C++.
1525 * Reflection:: Using reflection from C++.
1529 @node Basic concepts
1530 @section Basic concepts
1532 In terms of languages features, Java is mostly a subset
1533 of C++. Java has a few important extensions, plus a powerful standard
1534 class library, but on the whole that does not change the basic similarity.
1535 Java is a hybrid object-oriented language, with a few native types,
1536 in addition to class types. It is class-based, where a class may have
1537 static as well as per-object fields, and static as well as instance methods.
1538 Non-static methods may be virtual, and may be overloaded. Overloading is
1539 resolved at compile time by matching the actual argument types against
1540 the parameter types. Virtual methods are implemented using indirect calls
1541 through a dispatch table (virtual function table). Objects are
1542 allocated on the heap, and initialized using a constructor method.
1543 Classes are organized in a package hierarchy.
1545 All of the listed attributes are also true of C++, though C++ has
1546 extra features (for example in C++ objects may be allocated not just
1547 on the heap, but also statically or in a local stack frame). Because
1548 @command{gcj} uses the same compiler technology as G++ (the GNU
1549 C++ compiler), it is possible to make the intersection of the two
1550 languages use the same ABI (object representation and calling
1551 conventions). The key idea in CNI is that Java objects are C++
1552 objects, and all Java classes are C++ classes (but not the other way
1553 around). So the most important task in integrating Java and C++ is to
1554 remove gratuitous incompatibilities.
1556 You write CNI code as a regular C++ source file. (You do have to use
1557 a Java/CNI-aware C++ compiler, specifically a recent version of G++.)
1559 @noindent A CNI C++ source file must have:
1562 #include <gcj/cni.h>
1565 @noindent and then must include one header file for each Java class it uses, e.g.:
1568 #include <java/lang/Character.h>
1569 #include <java/util/Date.h>
1570 #include <java/lang/IndexOutOfBoundsException.h>
1573 @noindent These header files are automatically generated by @code{gcjh}.
1576 CNI provides some functions and macros to make using Java objects and
1577 primitive types from C++ easier. In general, these CNI functions and
1578 macros start with the @code{Jv} prefix, for example the function
1579 @code{JvNewObjectArray}. This convention is used to avoid conflicts
1580 with other libraries. Internal functions in CNI start with the prefix
1581 @code{_Jv_}. You should not call these; if you find a need to, let us
1582 know and we will try to come up with an alternate solution.
1585 @subsection Limitations
1587 Whilst a Java class is just a C++ class that doesn't mean that you are
1588 freed from the shackles of Java, a @acronym{CNI} C++ class must adhere to the
1589 rules of the Java programming language.
1591 For example: it is not possible to declare a method in a CNI class
1592 that will take a C string (@code{char*}) as an argument, or to declare a
1593 member variable of some non-Java datatype.
1599 The only global names in Java are class names, and packages. A
1600 @dfn{package} can contain zero or more classes, and also zero or more
1601 sub-packages. Every class belongs to either an unnamed package or a
1602 package that has a hierarchical and globally unique name.
1604 A Java package is mapped to a C++ @dfn{namespace}. The Java class
1605 @code{java.lang.String} is in the package @code{java.lang}, which is a
1606 sub-package of @code{java}. The C++ equivalent is the class
1607 @code{java::lang::String}, which is in the namespace @code{java::lang}
1608 which is in the namespace @code{java}.
1610 @noindent Here is how you could express this:
1613 (// @r{Declare the class(es), possibly in a header file:}
1622 class java::lang::String : public java::lang::Object
1628 @noindent The @code{gcjh} tool automatically generates the necessary namespace
1632 @subsection Leaving out package names
1634 Always using the fully-qualified name of a java class can be
1635 tiresomely verbose. Using the full qualified name also ties the code
1636 to a single package making code changes necessary should the class
1637 move from one package to another. The Java @code{package} declaration
1638 specifies that the following class declarations are in the named
1639 package, without having to explicitly name the full package
1640 qualifiers. The @code{package} declaration can be
1641 followed by zero or more @code{import} declarations, which
1642 allows either a single class or all the classes in a package to be
1643 named by a simple identifier. C++ provides something similar with the
1644 @code{using} declaration and directive.
1649 import @var{package-name}.@var{class-name};
1652 @noindent allows the program text to refer to @var{class-name} as a shorthand for
1653 the fully qualified name: @code{@var{package-name}.@var{class-name}}.
1656 @noindent To achieve the same effect C++, you have to do this:
1659 using @var{package-name}::@var{class-name};
1663 @noindent Java can also cause imports on demand, like this:
1666 import @var{package-name}.*;
1669 @noindent Doing this allows any class from the package @var{package-name} to be
1670 referred to only by its class-name within the program text.
1673 @noindent The same effect can be achieved in C++ like this:
1676 using namespace @var{package-name};
1680 @node Primitive types
1681 @section Primitive types
1683 Java provides 8 @dfn{primitives} types which represent integers, floats,
1684 characters and booleans (and also the void type). C++ has its own
1685 very similar concrete types. Such types in C++ however are not always
1686 implemented in the same way (an int might be 16, 32 or 64 bits for example)
1687 so CNI provides a special C++ type for each primitive Java type:
1689 @multitable @columnfractions .20 .25 .60
1690 @item @strong{Java type} @tab @strong{C/C++ typename} @tab @strong{Description}
1691 @item @code{char} @tab @code{jchar} @tab 16 bit Unicode character
1692 @item @code{boolean} @tab @code{jboolean} @tab logical (true or false) values
1693 @item @code{byte} @tab @code{jbyte} @tab 8-bit signed integer
1694 @item @code{short} @tab @code{jshort} @tab 16 bit signed integer
1695 @item @code{int} @tab @code{jint} @tab 32 bit signed integer
1696 @item @code{long} @tab @code{jlong} @tab 64 bit signed integer
1697 @item @code{float} @tab @code{jfloat} @tab 32 bit IEEE floating point number
1698 @item @code{double} @tab @code{jdouble} @tab 64 bit IEEE floating point number
1699 @item @code{void} @tab @code{void} @tab no value
1702 When referring to a Java type You should always use these C++ typenames (e.g.: @code{jint})
1703 to avoid disappointment.
1706 @subsection Reference types associated with primitive types
1708 In Java each primitive type has an associated reference type,
1709 e.g.: @code{boolean} has an associated @code{java.lang.Boolean.TYPE} class.
1710 In order to make working with such classes easier GCJ provides the macro
1713 @deffn macro JvPrimClass type
1714 Return a pointer to the @code{Class} object corresponding to the type supplied.
1717 JvPrimClass(void) @result{} java.lang.Void.TYPE
1723 @node Reference types
1724 @section Reference types
1726 A Java reference type is treated as a class in C++. Classes and
1727 interfaces are handled this way. A Java reference is translated to a
1728 C++ pointer, so for instance a Java @code{java.lang.String} becomes,
1729 in C++, @code{java::lang::String *}.
1731 CNI provides a few built-in typedefs for the most common classes:
1732 @multitable @columnfractions .30 .25 .60
1733 @item @strong{Java type} @tab @strong{C++ typename} @tab @strong{Description}
1734 @item @code{java.lang.Object} @tab @code{jobject} @tab Object type
1735 @item @code{java.lang.String} @tab @code{jstring} @tab String type
1736 @item @code{java.lang.Class} @tab @code{jclass} @tab Class type
1742 Every Java class or interface has a corresponding @code{Class}
1743 instance. These can be accessed in CNI via the static @code{class$}
1744 field of a class. The @code{class$} field is of type @code{Class}
1745 (and not @code{Class *}), so you will typically take the address of
1749 Here is how you can refer to the class of @code{String}, which in
1750 Java would be written @code{String.class}:
1753 using namespace java::lang;
1754 doSomething (&String::class$);
1761 A Java class can @dfn{implement} zero or more
1762 @dfn{interfaces}, in addition to inheriting from
1763 a single base class.
1765 @acronym{CNI} allows CNI code to implement methods of interfaces.
1766 You can also call methods through interface references, with some
1769 @acronym{CNI} doesn't understand interface inheritance at all yet. So,
1770 you can only call an interface method when the declared type of the
1771 field being called matches the interface which declares that
1772 method. The workaround is to cast the interface reference to the right
1775 For example if you have:
1783 interface B extends A
1789 and declare a variable of type @code{B} in C++, you can't call
1790 @code{a()} unless you cast it to an @code{A} first.
1792 @node Objects and Classes
1793 @section Objects and Classes
1797 All Java classes are derived from @code{java.lang.Object}. C++ does
1798 not have a unique root class, but we use the C++ class
1799 @code{java::lang::Object} as the C++ version of the
1800 @code{java.lang.Object} Java class. All other Java classes are mapped
1801 into corresponding C++ classes derived from @code{java::lang::Object}.
1803 Interface inheritance (the @code{implements} keyword) is currently not
1804 reflected in the C++ mapping.
1807 @subsection Object fields
1809 Each object contains an object header, followed by the instance fields
1810 of the class, in order. The object header consists of a single
1811 pointer to a dispatch or virtual function table. (There may be extra
1812 fields @emph{in front of} the object, for example for memory
1813 management, but this is invisible to the application, and the
1814 reference to the object points to the dispatch table pointer.)
1816 The fields are laid out in the same order, alignment, and size as in
1817 C++. Specifically, 8-bit and 16-bit native types (@code{byte},
1818 @code{short}, @code{char}, and @code{boolean}) are @emph{not} widened
1819 to 32 bits. Note that the Java VM does extend 8-bit and 16-bit types
1820 to 32 bits when on the VM stack or temporary registers.
1822 If you include the @code{gcjh}-generated header for a
1823 class, you can access fields of Java classes in the @emph{natural}
1824 way. For example, given the following Java class:
1830 public Int (int i) @{ this.i = i; @}
1831 public static Int zero = new Int(0);
1838 #include <gcj/cni.h>;
1842 mult (Int *p, jint k)
1845 return Int::zero; // @r{Static member access.}
1846 return new Int(p->i * k);
1851 @subsection Access specifiers
1853 CNI does not strictly enforce the Java access
1854 specifiers, because Java permissions cannot be directly mapped
1855 into C++ permission. Private Java fields and methods are mapped
1856 to private C++ fields and methods, but other fields and methods
1857 are mapped to public fields and methods.
1861 @node Class Initialization
1862 @section Class Initialization
1864 Java requires that each class be automatically initialized at the time
1865 of the first active use. Initializing a class involves
1866 initializing the static fields, running code in class initializer
1867 methods, and initializing base classes. There may also be
1868 some implementation specific actions, such as allocating
1869 @code{String} objects corresponding to string literals in
1872 The GCJ compiler inserts calls to @code{JvInitClass} at appropriate
1873 places to ensure that a class is initialized when required. The C++
1874 compiler does not insert these calls automatically---it is the
1875 programmer's responsibility to make sure classes are initialized.
1876 However, this is fairly painless because of the conventions assumed by
1879 First, @code{libgcj} will make sure a class is initialized before an
1880 instance of that object is created. This is one of the
1881 responsibilities of the @code{new} operation. This is taken care of
1882 both in Java code, and in C++ code. When G++ sees a @code{new} of a
1883 Java class, it will call a routine in @code{libgcj} to allocate the
1884 object, and that routine will take care of initializing the class.
1885 Note however that this does not happen for Java arrays; you must
1886 allocate those using the appropriate CNI function. It follows that
1887 you can access an instance field, or call an instance (non-static)
1888 method and be safe in the knowledge that the class and all of its base
1889 classes have been initialized.
1891 Invoking a static method is also safe. This is because the
1892 Java compiler adds code to the start of a static method to make sure
1893 the class is initialized. However, the C++ compiler does not
1894 add this extra code. Hence, if you write a native static method
1895 using CNI, you are responsible for calling @code{JvInitClass}
1896 before doing anything else in the method (unless you are sure
1897 it is safe to leave it out).
1899 Accessing a static field also requires the class of the
1900 field to be initialized. The Java compiler will generate code
1901 to call @code{JvInitClass} before getting or setting the field.
1902 However, the C++ compiler will not generate this extra code,
1903 so it is your responsibility to make sure the class is
1904 initialized before you access a static field from C++.
1907 @node Object allocation
1908 @section Object allocation
1910 New Java objects are allocated using a
1911 @dfn{class instance creation expression}, e.g.:
1914 new @var{Type} ( ... )
1917 The same syntax is used in C++. The main difference is that
1918 C++ objects have to be explicitly deleted; in Java they are
1919 automatically deleted by the garbage collector.
1920 Using @acronym{CNI}, you can allocate a new Java object
1921 using standard C++ syntax and the C++ compiler will allocate
1922 memory from the garbage collector. If you have overloaded
1923 constructors, the compiler will choose the correct one
1924 using standard C++ overload resolution rules.
1926 @noindent For example:
1929 java::util::Hashtable *ht = new java::util::Hashtable(120);
1933 @node Memory allocation
1934 @section Memory allocation
1936 When allocating memory in @acronym{CNI} methods it is best to handle
1937 out-of-memory conditions by throwing a Java exception. These
1938 functions are provided for that purpose:
1940 @deftypefun void* JvMalloc (jsize @var{size})
1941 Calls malloc. Throws @code{java.lang.OutOfMemoryError} if allocation
1945 @deftypefun void* JvRealloc (void* @var{ptr}, jsize @var{size})
1946 Calls realloc. Throws @code{java.lang.OutOfMemoryError} if
1950 @deftypefun void JvFree (void* @var{ptr})
1957 While in many ways Java is similar to C and C++, it is quite different
1958 in its treatment of arrays. C arrays are based on the idea of pointer
1959 arithmetic, which would be incompatible with Java's security
1960 requirements. Java arrays are true objects (array types inherit from
1961 @code{java.lang.Object}). An array-valued variable is one that
1962 contains a reference (pointer) to an array object.
1964 Referencing a Java array in C++ code is done using the
1965 @code{JArray} template, which as defined as follows:
1968 class __JArray : public java::lang::Object
1975 class JArray : public __JArray
1979 T& operator[](jint i) @{ return data[i]; @}
1984 There are a number of @code{typedef}s which correspond to @code{typedef}s
1985 from the @acronym{JNI}. Each is the type of an array holding objects
1986 of the relevant type:
1989 typedef __JArray *jarray;
1990 typedef JArray<jobject> *jobjectArray;
1991 typedef JArray<jboolean> *jbooleanArray;
1992 typedef JArray<jbyte> *jbyteArray;
1993 typedef JArray<jchar> *jcharArray;
1994 typedef JArray<jshort> *jshortArray;
1995 typedef JArray<jint> *jintArray;
1996 typedef JArray<jlong> *jlongArray;
1997 typedef JArray<jfloat> *jfloatArray;
1998 typedef JArray<jdouble> *jdoubleArray;
2002 @deftypemethod {template<class T>} T* elements (JArray<T> @var{array})
2003 This template function can be used to get a pointer to the elements of
2004 the @code{array}. For instance, you can fetch a pointer to the
2005 integers that make up an @code{int[]} like so:
2008 extern jintArray foo;
2009 jint *intp = elements (foo);
2012 The name of this function may change in the future.
2016 @deftypefun jobjectArray JvNewObjectArray (jsize @var{length}, jclass @var{klass}, jobject @var{init})
2017 This creates a new array whose elements have reference type.
2018 @code{klass} is the type of elements of the array and
2019 @code{init} is the initial value put into every slot in the array.
2023 using namespace java::lang;
2024 JArray<String *> *array
2025 = (JArray<String *> *) JvNewObjectArray(length, &String::class$, NULL);
2029 @subsection Creating arrays
2031 For each primitive type there is a function which can be used to
2032 create a new array of that type. The name of the function is of the
2036 JvNew@var{Type}Array
2039 @noindent For example:
2045 @noindent can be used to create an array of Java primitive boolean types.
2047 @noindent The following function definition is the template for all such functions:
2049 @deftypefun jbooleanArray JvNewBooleanArray (jint @var{length})
2050 Creates an array @var{length} indices long.
2053 @deftypefun jsize JvGetArrayLength (jarray @var{array})
2054 Returns the length of the @var{array}.
2061 Java methods are mapped directly into C++ methods.
2062 The header files generated by @code{gcjh}
2063 include the appropriate method definitions.
2064 Basically, the generated methods have the same names and
2065 @emph{corresponding} types as the Java methods,
2066 and are called in the natural manner.
2068 @subsection Overloading
2070 Both Java and C++ provide method overloading, where multiple
2071 methods in a class have the same name, and the correct one is chosen
2072 (at compile time) depending on the argument types.
2073 The rules for choosing the correct method are (as expected) more complicated
2074 in C++ than in Java, but given a set of overloaded methods
2075 generated by @code{gcjh} the C++ compiler will choose
2078 Common assemblers and linkers are not aware of C++ overloading,
2079 so the standard implementation strategy is to encode the
2080 parameter types of a method into its assembly-level name.
2081 This encoding is called @dfn{mangling},
2082 and the encoded name is the @dfn{mangled name}.
2083 The same mechanism is used to implement Java overloading.
2084 For C++/Java interoperability, it is important that both the Java
2085 and C++ compilers use the @emph{same} encoding scheme.
2087 @subsection Static methods
2089 Static Java methods are invoked in @acronym{CNI} using the standard
2090 C++ syntax, using the @code{::} operator rather
2091 than the @code{.} operator.
2093 @noindent For example:
2096 jint i = java::lang::Math::round((jfloat) 2.3);
2099 @noindent C++ method definition syntax is used to define a static native method.
2103 #include <java/lang/Integer>
2104 java::lang::Integer*
2105 java::lang::Integer::getInteger(jstring str)
2112 @subsection Object Constructors
2114 Constructors are called implicitly as part of object allocation
2115 using the @code{new} operator.
2117 @noindent For example:
2120 java::lang::Integer *x = new java::lang::Integer(234);
2123 Java does not allow a constructor to be a native method.
2124 This limitation can be coded round however because a constructor
2125 can @emph{call} a native method.
2128 @subsection Instance methods
2130 Calling a Java instance method from a C++ @acronym{CNI} method is done
2131 using the standard C++ syntax, e.g.:
2134 // @r{First create the Java object.}
2135 java::lang::Integer *x = new java::lang::Integer(234);
2136 // @r{Now call a method.}
2137 jint prim_value = x->intValue();
2138 if (x->longValue == 0)
2142 @noindent Defining a Java native instance method is also done the natural way:
2145 #include <java/lang/Integer.h>
2148 java::lang:Integer::doubleValue()
2150 return (jdouble) value;
2155 @subsection Interface methods
2157 In Java you can call a method using an interface reference. This is
2158 supported, but not completely. @xref{Interfaces}.
2166 @acronym{CNI} provides a number of utility functions for
2167 working with Java Java @code{String} objects.
2168 The names and interfaces are analogous to those of @acronym{JNI}.
2171 @deftypefun jstring JvNewString (const jchar* @var{chars}, jsize @var{len})
2172 Returns a Java @code{String} object with characters from the array of
2173 Unicode characters @var{chars} up to the index @var{len} in that array.
2176 @deftypefun jstring JvNewStringLatin1 (const char* @var{bytes}, jsize @var{len})
2177 Returns a Java @code{String} made up of @var{len} bytes from @var{bytes}.
2181 @deftypefun jstring JvNewStringLatin1 (const char* @var{bytes})
2182 As above but the length of the @code{String} is @code{strlen(@var{bytes})}.
2185 @deftypefun jstring JvNewStringUTF (const char* @var{bytes})
2186 Returns a @code{String} which is made up of the UTF encoded characters
2187 present in the C string @var{bytes}.
2190 @deftypefun jchar* JvGetStringChars (jstring @var{str})
2191 Returns a pointer to an array of characters making up the @code{String} @var{str}.
2194 @deftypefun int JvGetStringUTFLength (jstring @var{str})
2195 Returns the number of bytes required to encode the contents of the
2196 @code{String} @var{str} in UTF-8.
2199 @deftypefun jsize JvGetStringUTFRegion (jstring @var{str}, jsize @var{start}, jsize @var{len}, char* @var{buf})
2200 Puts the UTF-8 encoding of a region of the @code{String} @var{str} into
2201 the buffer @code{buf}. The region to fetch is marked by @var{start} and @var{len}.
2203 Note that @var{buf} is a buffer, not a C string. It is @emph{not}
2208 @node Mixing with C++
2209 @section Interoperating with C/C++
2211 Because @acronym{CNI} is designed to represent Java classes and methods it
2212 cannot be mixed readily with C/C++ types.
2214 One important restriction is that Java classes cannot have non-Java
2215 type instance or static variables and cannot have methods which take
2216 non-Java types as arguments or return non-Java types.
2218 @noindent None of the following is possible with CNI:
2222 class ::MyClass : public java::lang::Object
2224 char* variable; // @r{char* is not a valid Java type.}
2229 ::SomeClass::someMethod (char *arg)
2234 @} // @r{@code{uint} is not a valid Java type, neither is @code{char*}}
2237 @noindent Of course, it is ok to use C/C++ types within the scope of a method:
2242 ::SomeClass::otherMethod (jstring str)
2253 The above restriction can be problematic, so @acronym{CNI} includes the
2254 @code{gnu.gcj.RawData} class. The @code{RawData} class is a
2255 @dfn{non-scanned reference} type. In other words variables declared
2256 of type @code{RawData} can contain any data and are not checked by the
2257 compiler or memory manager in any way.
2259 This means that you can put C/C++ data structures (including classes)
2260 in your @acronym{CNI} classes, as long as you use the appropriate cast.
2262 @noindent Here are some examples:
2266 class ::MyClass : public java::lang::Object
2268 gnu.gcj.RawData string;
2271 gnu.gcj.RawData getText ();
2275 ::MyClass::MyClass ()
2282 ::MyClass::getText ()
2288 ::MyClass::printText ()
2290 printf("%s\n", (char*) string);
2295 @subsection RawDataManaged
2297 @code{gnu.gcj.RawDataManaged} is another type used to indicate special data used
2298 by native code. Unlike the @code{RawData} type, fields declared as
2299 @code{RawDataManaged} will be "marked" by the memory manager and
2300 considered for garbage collection.
2302 Native data which is allocated using CNI's @code{JvAllocBytes()}
2303 function and stored in a @code{RawDataManaged} will be automatically
2304 freed when the Java object it is associated with becomes unreachable.
2306 @subsection Native memory allocation
2308 @deftypefun void* JvAllocBytes (jsize @var{size})
2309 Allocates @var{size} bytes from the heap. The memory returned is zeroed.
2310 This memory is not scanned for pointers by the garbage collector, but will
2311 be freed if no references to it are discovered.
2313 This function can be useful if you need to associate some native data with a
2314 Java object. Using a CNI's special @code{RawDataManaged} type, native data
2315 allocated with @code{JvAllocBytes} will be automatically freed when the Java
2316 object itself becomes unreachable.
2319 @subsection Posix signals
2321 On Posix based systems the @code{libgcj} library uses several signals
2322 internally. @acronym{CNI} code should not attempt to use the same
2323 signals as doing so may cause @code{libgcj} and/or the @acronym{CNI}
2326 SIGSEGV is used on many systems to generate
2327 @code{NullPointerExceptions}. SIGCHLD is used internally by
2328 @code{Runtime.exec()}. Several other signals (that vary from platform to
2329 platform) can be used by the memory manager and by
2330 @code{Thread.interrupt()}.
2332 @node Exception Handling
2333 @section Exception Handling
2335 While C++ and Java share a common exception handling framework,
2336 things are not yet perfectly integrated. The main issue is that the
2337 run-time type information facilities of the two
2338 languages are not integrated.
2340 Still, things work fairly well. You can throw a Java exception from
2341 C++ using the ordinary @code{throw} construct, and this
2342 exception can be caught by Java code. Similarly, you can catch an
2343 exception thrown from Java using the C++ @code{catch}
2346 @noindent Here is an example:
2350 throw new java::lang::IndexOutOfBoundsException();
2353 Normally, G++ will automatically detect when you are writing C++
2354 code that uses Java exceptions, and handle them appropriately.
2355 However, if C++ code only needs to execute destructors when Java
2356 exceptions are thrown through it, GCC will guess incorrectly. Sample
2360 struct S @{ ~S(); @};
2362 extern void bar(); // @r{Is implemented in Java and may throw exceptions.}
2371 The usual effect of an incorrect guess is a link failure, complaining of
2372 a missing routine called @code{__gxx_personality_v0}.
2374 You can inform the compiler that Java exceptions are to be used in a
2375 translation unit, irrespective of what it might think, by writing
2376 @code{#pragma GCC java_exceptions} at the head of the
2377 file. This @code{#pragma} must appear before any
2378 functions that throw or catch exceptions, or run destructors when
2379 exceptions are thrown through them.
2381 @node Synchronization
2382 @section Synchronization
2384 Each Java object has an implicit monitor.
2385 The Java VM uses the instruction @code{monitorenter} to acquire
2386 and lock a monitor, and @code{monitorexit} to release it.
2388 The corresponding CNI macros are @code{JvMonitorEnter} and
2389 @code{JvMonitorExit} (JNI has similar methods @code{MonitorEnter}
2390 and @code{MonitorExit}).
2393 The Java source language does not provide direct access to these primitives.
2394 Instead, there is a @code{synchronized} statement that does an
2395 implicit @code{monitorenter} before entry to the block,
2396 and does a @code{monitorexit} on exit from the block.
2397 Note that the lock has to be released even when the block is abnormally
2398 terminated by an exception, which means there is an implicit
2399 @code{try finally} surrounding synchronization locks.
2401 From C++, it makes sense to use a destructor to release a lock.
2402 @acronym{CNI} defines the following utility class:
2405 class JvSynchronize() @{
2407 JvSynchronize(jobject o) @{ obj = o; JvMonitorEnter(o); @}
2408 ~JvSynchronize() @{ JvMonitorExit(obj); @}
2421 @noindent might become this C++ code:
2425 JvSynchronize dummy (OBJ);
2430 Java also has methods with the @code{synchronized} attribute.
2431 This is equivalent to wrapping the entire method body in a
2432 @code{synchronized} statement.
2433 (Alternatively, an implementation could require the caller to do
2434 the synchronization. This is not practical for a compiler, because
2435 each virtual method call would have to test at run-time if
2436 synchronization is needed.) Since in @command{gcj}
2437 the @code{synchronized} attribute is handled by the
2438 method implementation, it is up to the programmer
2439 of a synchronized native method to handle the synchronization
2440 (in the C++ implementation of the method).
2441 In other words, you need to manually add @code{JvSynchronize}
2442 in a @code{native synchronized} method.
2447 CNI permits C++ applications to make calls into Java classes, in addition to
2448 allowing Java code to call into C++. Several functions, known as the
2449 @dfn{invocation API}, are provided to support this.
2451 @deftypefun jint JvCreateJavaVM (JvVMInitArgs* @var{vm_args})
2453 Initializes the Java runtime. This function performs essential initialization
2454 of the threads interface, garbage collector, exception handling and other key
2455 aspects of the runtime. It must be called once by an application with
2456 a non-Java @code{main()} function, before any other Java or CNI calls are made.
2457 It is safe, but not recommended, to call @code{JvCreateJavaVM()} more than
2458 once provided it is only called from a single thread.
2459 The @var{vmargs} parameter can be used to specify initialization parameters
2460 for the Java runtime. It may be @code{NULL}.
2462 JvVMInitArgs represents a list of virtual machine initialization
2463 arguments. @code{JvCreateJavaVM()} ignores the version field.
2466 typedef struct JvVMOption
2468 // a VM initialization option
2470 // extra information associated with this option
2474 typedef struct JvVMInitArgs
2476 // for compatibility with JavaVMInitArgs
2479 // number of VM initialization options
2482 // an array of VM initialization options
2483 JvVMOption* options;
2485 // true if the option parser should ignore unrecognized options
2486 jboolean ignoreUnrecognized;
2490 @code{JvCreateJavaVM()} returns @code{0} upon success, or @code{-1} if
2491 the runtime is already initialized.
2493 @emph{Note:} In GCJ 3.1, the @code{vm_args} parameter is ignored. It
2494 is recognized and used as of release 4.0.
2497 @deftypefun java::lang::Thread* JvAttachCurrentThread (jstring @var{name}, java::lang::ThreadGroup* @var{group})
2498 Registers an existing thread with the Java runtime. This must be called once
2499 from each thread, before that thread makes any other Java or CNI calls. It
2500 must be called after @code{JvCreateJavaVM}.
2501 @var{name} specifies a name for the thread. It may be @code{NULL}, in which
2502 case a name will be generated.
2503 @var{group} is the ThreadGroup in which this thread will be a member. If it
2504 is @code{NULL}, the thread will be a member of the main thread group.
2505 The return value is the Java @code{Thread} object that represents the thread.
2506 It is safe to call @code{JvAttachCurrentThread()} more than once from the same
2507 thread. If the thread is already attached, the call is ignored and the current
2508 thread object is returned.
2511 @deftypefun jint JvDetachCurrentThread ()
2512 Unregisters a thread from the Java runtime. This should be called by threads
2513 that were attached using @code{JvAttachCurrentThread()}, after they have
2514 finished making calls to Java code. This ensures that any resources associated
2515 with the thread become eligible for garbage collection.
2516 This function returns @code{0} upon success, or @code{-1} if the current thread
2520 @subsection Handling uncaught exceptions
2522 If an exception is thrown from Java code called using the invocation API, and
2523 no handler for the exception can be found, the runtime will abort the
2524 application. In order to make the application more robust, it is recommended
2525 that code which uses the invocation API be wrapped by a top-level try/catch
2526 block that catches all Java exceptions.
2530 The following code demonstrates the use of the invocation API. In this
2531 example, the C++ application initializes the Java runtime and attaches
2532 itself. The @code{java.lang.System} class is initialized in order to
2533 access its @code{out} field, and a Java string is printed. Finally, the thread
2534 is detached from the runtime once it has finished making Java calls. Everything
2535 is wrapped with a try/catch block to provide a default handler for any uncaught
2538 The example can be compiled with @command{c++ -c test.cc; gcj test.o}.
2542 #include <gcj/cni.h>
2543 #include <java/lang/System.h>
2544 #include <java/io/PrintStream.h>
2545 #include <java/lang/Throwable.h>
2547 int main(int argc, char *argv[])
2549 using namespace java::lang;
2553 JvCreateJavaVM(NULL);
2554 JvAttachCurrentThread(NULL, NULL);
2556 String *message = JvNewStringLatin1("Hello from C++");
2557 JvInitClass(&System::class$);
2558 System::out->println(message);
2560 JvDetachCurrentThread();
2562 catch (Throwable *t)
2564 System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
2565 t->printStackTrace();
2573 Reflection is possible with CNI code, it functions similarly to how it
2574 functions with JNI@.
2576 @c clean this up... I mean, what are the types jfieldID and jmethodID in JNI?
2577 The types @code{jfieldID} and @code{jmethodID}
2580 @noindent The functions:
2583 @item @code{JvFromReflectedField},
2584 @item @code{JvFromReflectedMethod},
2585 @item @code{JvToReflectedField}
2586 @item @code{JvToFromReflectedMethod}
2589 @noindent will be added shortly, as will other functions corresponding to JNI@.
2592 @node System properties
2593 @chapter System properties
2595 The runtime behavior of the @code{libgcj} library can be modified by setting
2596 certain system properties. These properties can be compiled into the program
2597 using the @code{-D@var{name}[=@var{value}]} option to @command{gcj} or by
2598 setting them explicitly in the program by calling the
2599 @code{java.lang.System.setProperty()} method. Some system properties are only
2600 used for informational purposes (like giving a version number or a user name).
2601 A program can inspect the current value of a property by calling the
2602 @code{java.lang.System.getProperty()} method.
2605 * Standard Properties:: Standard properties supported by @code{libgcj}
2606 * GNU Classpath Properties:: Properties found in Classpath based libraries
2607 * libgcj Runtime Properties:: Properties specific to @code{libgcj}
2610 @node Standard Properties
2611 @section Standard Properties
2613 The following properties are normally found in all implementations of the core
2614 libraries for the Java language.
2619 The @code{libgcj} version number.
2622 Set to @samp{The Free Software Foundation, Inc.}
2624 @item java.vendor.url
2625 Set to @uref{http://gcc.gnu.org/java/}.
2628 The directory where @code{gcj} was installed. Taken from the @code{--prefix}
2629 option given to @command{configure}.
2631 @item java.class.version
2632 The class format version number supported by the libgcj byte code interpreter.
2633 (Currently @samp{46.0})
2635 @item java.vm.specification.version
2636 The Virtual Machine Specification version implemented by @code{libgcj}.
2637 (Currently @samp{1.0})
2639 @item java.vm.specification.vendor
2640 The name of the Virtual Machine specification designer.
2642 @item java.vm.specification.name
2643 The name of the Virtual Machine specification
2644 (Set to @samp{Java Virtual Machine Specification}).
2646 @item java.vm.version
2647 The @command{gcj} version number.
2649 @item java.vm.vendor
2650 Set to @samp{The Free Software Foundation, Inc.}
2653 Set to @samp{GNU libgcj}.
2655 @item java.specification.version
2656 The Runtime Environment specification version implemented by @code{libgcj}.
2657 (Currently set to @samp{1.3})
2659 @item java.specification.vendor
2660 The Runtime Environment specification designer.
2662 @item java.specification.name
2663 The name of the Runtime Environment specification
2664 (Set to @samp{Java Platform API Specification}).
2666 @item java.class.path
2667 The paths (jar files, zip files and directories) used for finding class files.
2669 @item java.library.path
2670 Directory path used for finding native libraries.
2672 @item java.io.tmpdir
2673 The directory used to put temporary files in.
2676 Name of the Just In Time compiler to use by the byte code interpreter.
2677 Currently not used in @code{libgcj}.
2680 Directories containing jar files with extra libraries. Will be used when
2683 @item java.protocol.handler.pkgs
2684 A @samp{|} separated list of package names that is used to find classes that
2685 implement handlers for @code{java.net.URL}.
2687 @item java.rmi.server.codebase
2688 A list of URLs that is used by the @code{java.rmi.server.RMIClassLoader}
2689 to load classes from.
2692 A list of class names that will be loaded by the @code{java.sql.DriverManager}
2695 @item file.separator
2696 The separator used in when directories are included in a filename
2697 (normally @samp{/} or @samp{\} ).
2700 The default character encoding used when converting platform native files to
2701 Unicode (usually set to @samp{8859_1}).
2703 @item path.separator
2704 The standard separator used when a string contains multiple paths
2705 (normally @samp{:} or @samp{;}), the string is usually not a valid character
2706 to use in normal directory names.)
2708 @item line.separator
2709 The default line separator used on the platform (normally @samp{\n}, @samp{\r}
2710 or a combination of those two characters).
2712 @item policy.provider
2713 The class name used for the default policy provider returned by
2714 @code{java.security.Policy.getPolicy}.
2717 The name of the user running the program. Can be the full name, the login name
2718 or empty if unknown.
2721 The default directory to put user specific files in.
2724 The current working directory from which the program was started.
2727 The default language as used by the @code{java.util.Locale} class.
2730 The default region as used by the @code{java.util.Local} class.
2733 The default variant of the language and region local used.
2736 The default timezone as used by the @code{java.util.TimeZone} class.
2739 The operating system/kernel name that the program runs on.
2742 The hardware that we are running on.
2745 The version number of the operating system/kernel.
2747 @item awt.appletWarning
2748 The string to display when an untrusted applet is displayed.
2749 Returned by @code{java.awt.Window.getWarningString()} when the window is
2753 The class name used for initializing the default @code{java.awt.Toolkit}.
2754 Defaults to @code{gnu.awt.gtk.GtkToolkit}.
2756 @item http.proxyHost
2757 Name of proxy host for http connections.
2759 @item http.proxyPort
2760 Port number to use when a proxy host is in use.
2764 @node GNU Classpath Properties
2765 @section GNU Classpath Properties
2767 @code{libgcj} is based on the GNU Classpath (Essential Libraries for Java) a
2768 GNU project to create free core class libraries for use with virtual machines
2769 and compilers for the Java language. The following properties are common to
2770 libraries based on GNU Classpath.
2774 @item gcj.dumpobject
2775 Enables printing serialization debugging by the @code{java.io.ObjectInput} and
2776 @code{java.io.ObjectOutput} classes when set to something else then the empty
2777 string. Only used when running a debug build of the library.
2779 @item gnu.classpath.vm.shortname
2780 This is a succinct name of the virtual machine. For @code{libgcj},
2781 this will always be @samp{libgcj}.
2783 @item gnu.classpath.home.url
2784 A base URL used for finding system property files (e.g.,
2785 @file{classpath.security}). By default this is a @samp{file:} URL
2786 pointing to the @file{lib} directory under @samp{java.home}.
2790 @node libgcj Runtime Properties
2791 @section libgcj Runtime Properties
2793 The following properties are specific to the @code{libgcj} runtime and will
2794 normally not be found in other core libraries for the java language.
2798 @item java.fullversion
2799 The combination of @code{java.vm.name} and @code{java.vm.version}.
2802 Same as @code{java.fullversion}.
2805 Used by the @code{java.net.DatagramSocket} class when set to something else
2806 then the empty string. When set all newly created @code{DatagramSocket}s will
2807 try to load a class @code{java.net.[impl.prefix]DatagramSocketImpl} instead of
2808 the normal @code{java.net.PlainDatagramSocketImpl}.
2810 @item gnu.gcj.progname
2811 The class or binary name that was used to invoke the program. This will be
2812 the name of the "main" class in the case where the @code{gij} front end is
2813 used, or the program binary name in the case where an application is compiled
2816 @item gnu.gcj.user.realname
2817 The real name of the user, as taken from the password file. This may
2818 not always hold only the user's name (as some sites put extra
2819 information in this field). Also, this property is not available on
2822 @item gnu.gcj.runtime.NameFinder.use_addr2line
2823 Whether an external process, @command{addr2line}, should be used to determine
2824 line number information when tracing the stack. Setting this to @code{false}
2825 may suppress line numbers when printing stack traces and when using
2826 the java.util.logging infrastructure. However, performance may improve
2827 significantly for applications that print stack traces or make logging calls
2830 @item gnu.gcj.runtime.NameFinder.show_raw
2831 Whether the address of a stack frame should be printed when the line
2832 number is unavailable. Setting this to @code{true} will cause the name
2833 of the object and the offset within that object to be printed when no
2834 line number is available. This allows for off-line decoding of
2835 stack traces if necessary debug information is available. The default
2836 is @code{false}, no raw addresses are printed.
2838 @item gnu.gcj.runtime.NameFinder.remove_unknown
2839 Whether stack frames for non-java code should be included in a stack
2840 trace. The default value is @code{true}, stack frames for non-java
2841 code are suppressed. Setting this to @code{false} will cause any
2842 non-java stack frames to be printed in addition to frames for the java
2845 @item gnu.gcj.runtime.VMClassLoader.library_control
2846 This controls how shared libraries are automatically loaded by the
2847 built-in class loader. If this property is set to @samp{full}, a full
2848 search is done for each requested class. If this property is set to
2849 @samp{cache}, then any failed lookups are cached and not tried again.
2850 If this property is set to @samp{never} (the default), then lookups
2851 are never done. For more information, @xref{Extensions}.
2853 @item gnu.gcj.runtime.endorsed.dirs
2854 This is like the standard @code{java.endorsed.dirs}, property, but
2855 specifies some extra directories which are searched after the standard
2856 endorsed directories. This is primarily useful for telling
2857 @code{libgcj} about additional libraries which are ordinarily
2858 incorporated into the JDK, and which should be loaded by the bootstrap
2859 class loader, but which are not yet part of @code{libgcj} itself for
2862 @item gnu.gcj.jit.compiler
2863 @c FIXME we should probably have a whole node on this...
2864 This is the full path to @command{gcj} executable which should be
2865 used to compile classes just-in-time when
2866 @code{ClassLoader.defineClass} is called. If not set, @command{gcj}
2867 will not be invoked by the runtime; this can also be controlled via
2868 @code{Compiler.disable}.
2870 @item gnu.gcj.jit.options
2871 This is a space-separated string of options which should be passed to
2872 @command{gcj} when in JIT mode. If not set, a sensible default is
2875 @item gnu.gcj.jit.cachedir
2876 This is the directory where cached shared library files are
2877 stored. If not set, JIT compilation is disabled. This should never
2878 be set to a directory that is writable by any other user.
2880 @item gnu.gcj.precompiled.db.path
2881 This is a sequence of file names, each referring to a file created by
2882 @command{gcj-dbtool}. These files will be used by @code{libgcj} to
2883 find shared libraries corresponding to classes that are loaded from
2884 bytecode. @code{libgcj} often has a built-in default database; it
2885 can be queried using @code{gcj-dbtool -p}.
2893 While writing @command{gcj} and @code{libgcj} we have, of course, relied
2894 heavily on documentation from Sun Microsystems. In particular we have
2895 used The Java Language Specification (both first and second editions),
2896 the Java Class Libraries (volumes one and two), and the Java Virtual
2897 Machine Specification. In addition we've used the online documentation
2898 at @uref{http://java.sun.com/}.
2900 The current @command{gcj} home page is
2901 @uref{http://gcc.gnu.org/java/}.
2903 For more information on gcc, see @uref{http://gcc.gnu.org/}.
2905 Some @code{libgcj} testing is done using the Mauve test suite. This is
2906 a free software Java class library test suite which is being written
2907 because the JCK is not free. See
2908 @uref{http://sources.redhat.com/mauve/} for more information.