c**********************************************************************c
c Description: Run a Java Primitive inside X-Midas
c
c Author:      whk 05/14/2004
c
c $Header: /home/scm/cvs/nextmidas/nxm/sys/host/jvm.for,v 1.4 2005/08/17 14:22:27 jph Exp $
c
c
c**********************************************************************c
      program JVM

      implicit none
      include 'primitive.inc'
      include 'flags.inc'
      include 'jni.inc'

c.. Inputs
      logical*4 debug
      character*(1024) commandname

c.. Locals
      integer*4 lcommandName
      integer*4 counter
      integer*4 ltmpstr
      character*2200 tmpstr

      integer*4 ignore
      logical*4 ignore_logical

      call M$CHECKIN

      !******* Check to debug the primitive
      debug = M$GET_PSWITCH('DEBUG')


      ignore_logical = jni$setclasspath(debug)

      ignore_logical = jni$getswitches(debug)
      	  
      if (debug) then
        write(*,*)'JVM.FOR: getting command'
      endif
      !******* Get the Java Command Name to run ***********************c
      lcommandName = M$UAPICK(1,commandname,UAP_TRANSLATE)

      counter = 2
      do while (M$PPICK(counter))
        ltmpstr = M$UAPICK(counter,tmpstr,UAP_TRANSLATE)
        ignore = jni$addarg(tmpstr(1:ltmpstr))
        counter = counter+1
      enddo

c The Java program can call xm.M.sync if necessary or the user
c can use the /SYNC switch to force it to occur before start
c the command
 
      if (M$GET_SWITCH('SYNC') .eq. 1) then
        call M$SYNC()
      endif

c Create the JVM
      ignore = jni$startjvm()


c Start the java program
      if(debug) then
        write(*,*)'JVM.FOR: running command "'/ /
     &            commandname(1:lcommandName)//'"'
      endif
      ignore = jni$run(commandname)
        
      ignore = jni$stopjvm()

      call M$RETURN
      end
