The java::defineclass Command


Usage:

java::defineclass classData

The java::defineclass command is used to convert a string of bytes into a Java Class object. The classData argument is a string of bytes that compose a class. For example, the classData string could be from reading a Java class file. The classData argument could also be a reflected instance of a byte[] Java object. The ClassData argument is passed to the TclClassLoader, where the TclClassLoader attempts to construct a Java Class object from the bytes. If classData does not represent a valid class, java::defineclass returns a null object, otherwise it will return a handle to the Java Class object. A class is not valid if; the TclClassLoader cannot decifer a class from classData, the class has already been loaded into the VM, or if the class is in the reserved java package.

The TclClassLoader maintains a cache of the Java Class objects loaded by the java::defineclass routine. The name of the class, which is stored within the class bytecodes, is extracted and is used to reference the cached Java Class object. If the class name is used in future calls (e.g. java::new) the class defined by classData is used.

Example:

In this example, a Java class is read from the filesystem and then defined using the java::defineclass command.
set fd [open Foo.class r]
fconfigure $file -translation binary
set data [read $fd]
close $fd

# This returns a java.lang.Class object.
set class [java::defineclass $data]

# Get an instance of the object by invoking the
# java.lang.Class.newInstance() method.
set object [$class newInstance]

# The class is now loaded into the VM, so we
# can also create instances of it by name
set object [java::new Foo]

Copyright © 1997-1998 Sun Microsystems, Inc.