The java::import command provides a means to specify Java class names in a shortened format. This functionality is the equivalent of the import statement in Java. For example, the Java class name java.util.Hashtable could be specified with the shortened name Hashtable once it had been imported. Each class argument is checked to ensure that it is the name of a valid class and does not conflict with another class imported from another package. The optional -package pkg arguments are used to indicate the package that each class argument is defined in. If the -package pkg arguments are given, none of the class arguments can include a package specifier, meaning the '.' character must not appear in the class name.
If the java::import command is invoked with no arguments it will return a list of all the currently imported classes. If the java::import command is invoked with only the optional -package pkg arguments, then only those classes imported from the specified package will be returned.
The -forget argument will invalidate a previously imported class name. The optional -package pkg arguments can be combined with the -forget argument to remove all imported classes from a given package.
# Use fully qualified name with the java::new command
set h1 [java::new java.util.Hashtable]
# Import the class name shortcut
java::import java.util.Hashtable
# Use imported name with the java::new command
set h2 [java::new Hashtable]
This example demonstrates how to use the optional
-package pkg arguments to import more
than one class from a given package.
# Import two classes from the java.util package
java::import -package java.util Hashtable Vector
# Call java methods using the shortened names
set h [java::new Hashtable]
set bool [java::instanceof $h Hashtable]
set v [java::new Vector]
set array [java::new {Hashtable[]} {10}]
This example demonstrates how to use the optional
-forget argument to remove a class from the
import list.
# Import two classes from the java.util package
java::import java.util.Hashtable java.util.Vector
# Woops, I did not want java.util.Vector
java::import -forget java.util.Vector
This example demonstrates how to combine the
-forget argument and the
-package pkg arguments to remove
all imported classes from a given package.
# Import two classes from the java.util package
java::import java.util.Hashtable java.util.Vector
# Now forget about both of them
java::import -forget -package java.util
This example demonstrates how to query the import list.
# Import classes from two different packages
java::import java.net.Socket java.util.Hashtable
# Will print "java.net.Socket java.util.Hashtable"
puts [java::import]
# Will print "java.util.Hashtable"
puts [java::import -package java.util]
Copyright © 1997-1998 Sun Microsystems, Inc.