The java::import Command


Usage:

java::import ?-forget? ?-package pkg? ?class ...?

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. Once imported, the Java class java.util.Hashtable could be specified with the shortened name Hashtable. Each class argument is checked to ensure that it is the name of a accessible 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 imported from. If the -package pkg arguments are given, none of the class arguments can include a package specifier, meaning the '.' character must not appear in a class name.

The imported class name may be specified using the wildcard character * (asterisk), which allows any class in the corresponding package to be imported. Classes loaded using the wildcard import are lazily-loaded and will be resolved after any explicitly-named classes.

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 forget about all the imported classes from a given package. If a wildcard class ("*") is specified, that corresponding package is forgotten, except that any classes loaded via a previous wildcard import will remain available for subsequent loading, unless explicitly forgotten.

Examples:

This example demonstrates how to allocate a java.util.Hashtable object using the fully qualified name and the imported name.
# 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]

# Note that the command above does not import all
# the classes from a named package.

This example demostrates wildcard imports.
# Wildcard import from java.util.*
java::import java.util.*

# Import java.sql.Date
java::import java.sql.Date

# Load java.util.HashMap by creating a new instance
set hashMap [java::new HashMap]

# Load java.sql.Date, with current time
# Note! java.sql.Date was imported explicitly, and takes precedence
#       over java.util.Date
set sqlDate [java::new Date [java::call System currentTimeMillis]]

# See the current import list
puts [java::import]

# result: java.sql.Date java.util.HashMap java.util.*

# Forget the java.util.* import
java::import -forget java.util.*

# See the current import list
puts [java::import]

# result: java.sql.Date java.util.HashMap
# Note that java.util.HashMap is now a resolved import


Copyright © 1997-1998 Sun Microsystems, Inc.