The nsjava::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 nsjava::import command is invoked with no arguments it will return a list of all the currently imported classes. If the nsjava::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 nsjava::new command
set h1 [nsjava::new java.util.Hashtable]
# Import the class name shortcut
nsjava::import java.util.Hashtable
# Use imported name with the nsjava::new command
set h2 [nsjava::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
nsjava::import -package java.util Hashtable Vector
# Call java methods using the shortened names
set h [nsjava::new Hashtable]
set bool [nsjava::instanceof $h Hashtable]
set v [nsjava::new Vector]
set array [nsjava::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
nsjava::import java.util.Hashtable java.util.Vector
# Woops, I did not want java.util.Vector
nsjava::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
nsjava::import java.util.Hashtable java.util.Vector
# Now forget about both of them
nsjava::import -forget -package java.util
This example demonstrates how to query the import list.
# Import classes from two different packages
nsjava::import java.net.Socket java.util.Hashtable
# Will print "java.net.Socket java.util.Hashtable"
puts [nsjava::import]
# Will print "java.util.Hashtable"
puts [nsjava::import -package java.util]
Copyright © 1997-1998 Sun Microsystems, Inc.