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. ClassData 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 file [open Foo.class r]

# Tcl Blend users must call 'fconfigure $file -translate binary'
set data [read $file]
close $file

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

# Get an instance of the object.
set object [$class newInstance]

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

Copyright © 1997-1998 Sun Microsystems, Inc.