Tcl/Java Examples

This page contains examples of using the java package to interact with a Java virtual machine from Tcl. The Java package is available in both Jacl and TclBlend. Using the java package, a Tcl script can create Java objects, invoke Java methods, and inspect results.


Allocating a Java object from Tcl

In this example, a Java object of type java.lang.String is allocated using the java::new command and the Java object handle java0x1 is saved in a variable. The String object's charAt method is then invoked on the object using the saved handle. Note that the char primitive type returned by the charAt method is implicitly converted to a Tcl string value. The unset command is called on the variable that holds the handle to indicate that we are finished with the object so that it can be garage collected.

  % package require java
  1.3.2
  % set jstr [java::new String "A Java String"]
  java0x1
  % $jstr charAt 2
  J
  % $jstr toString
  A Java String
  % unset jstr

Use of JDBC API with Jacl or Tcl Blend.

A comp.lang.tcl poster reports satisfying light-duty use of JDBC with TclBlend.

Using Tcl Blend and JDBC example can check TclBlend - Examples .


Use of TclBlend to put a JMS message on a remote IBM MQ Windows queue using MQ JMS classes.

###########################################################
# Connect to MQ listener and put message on the queue.
###########################################################

puts "\n executing [info script]\n"
# make script drive independent.
set drive [lindex [file split [info nameofexecutable]] 0 ] 
puts "\n proclib = $drive/scripts/TCL/proclib"

########################################################  
# Source packages. 
########################################################  

package require java

######################################
# Proc - put message. 
######################################

proc putMSG { QManager CCSID channel hostname queueName port traceFile msg } {
    puts "\n putMSG \n"
  
    # import required classes 
    java::import com.ibm.mq.jms.MQQueueConnectionFactory
    java::import com.ibm.mq.jms.services.ConfigEnvironment
    java::import com.ibm.mq.jms.JMSC
    java::import com.ibm.mq.jms.MQQueueSession
    java::import com.ibm.mq.jms.MQQueueSender
  
    # instanciate MQQueueConnectionFactoryI object.
    set MQQueueConnectionFactoryI [ java::new MQQueueConnectionFactory ]
  
    # set MQQueueConnectionFactory instance methods.
    $MQQueueConnectionFactoryI setQueueManager         $QManager
    $MQQueueConnectionFactoryI setPort                 $port
    $MQQueueConnectionFactoryI setHostName             $hostname
    $MQQueueConnectionFactoryI setChannel              $channel
    $MQQueueConnectionFactoryI setCCSID                $CCSID
    $MQQueueConnectionFactoryI setUseConnectionPooling true
    $MQQueueConnectionFactoryI setTransportType        [ java::field JMSC MQJMS_TP_CLIENT_MQ_TCPIP ]    
  
    # set tracing on.
    java::call ConfigEnvironment start
    
    puts "Creating a Connection...................\n"
    set connectionI [ $MQQueueConnectionFactoryI createQueueConnection ]
  
    puts "Starting the Connection.................\n"
    $connectionI start
    puts "Creating a Session......................\n"
    set transacted      false 
    set autoAcknowledge [ java::field MQQueueSession AUTO_ACKNOWLEDGE ]
    set sessionI        [ $connectionI createQueueSession $transacted $autoAcknowledge ]
  
    puts "Creating a queue......................\n"
    set queueI         [ $sessionI createQueue $queueName ]
  
    puts "Creating a queue sender ......................\n"
    set queueSenderI  [ $sessionI createSender $queueI ] 
    puts "Sending the message to..[ $queueI getQueueName ]\n" 
  
    puts "Creating a TextMessage........................\n"
    set outMsgI [ $sessionI createTextMessage ]
    $outMsgI    setText $msg
    puts "Sending TextMessage \"$msg\" \n"
    $queueSenderI send $outMsgI
    
    puts "Closing QueueSender........................\n"
    $queueSenderI close 
  
    puts "Closing Session............................\n"
    $sessionI close 
  
    puts "Closing Connection.........................\n"
    $connectionI close 
}

######################################
# Main Control.
######################################

# build tcl classpath
append x $drive/IBM/WebSphereMQ/Java/lib/com.ibm.mq.jar\;
append x $drive/IBM/WebSphereMQ/Tools/Java/base\;
append x $drive/IBM/WebSphereMQ/Java/lib/com.ibm.mqjms.jar\;
append x $drive/IBM/WebSphereMQ/Tools/Java/jms\;
append x $drive/IBM/WebSphereMQ/Java/lib/com.ibm.mqbind.jar\;
set env(TCL_CLASSPATH) $x
puts "\nTCL_CLASSPATH = [ array get env TCL_CLASSPATH ]\n"
set traceFile    $drive/IBM/WebSphereMQ/Errors/mqTrace.txt
set reportFile   $drive/reports/notify/mqConnect.txt
set reportFileId [ open $reportFile w ] 
set QManager  QM1
set CCSID     819
set channel   AAAAAAA.BBB.CCCCCC
set hostname  moonbase1
set queueName ABC.DD.EEEE.1234.FFFFFFFF
set port      1414
set msg       "Watson, please come here. I want you."
putMSG $QManager $CCSID $channel $hostname $queueName $port $traceFile $msg