Creating a Java VM

Before calling an EJB component, you need to create a Java VM using the CreateJavaVM method of the JavaVM class. The first argument is a string that specifies a classpath to be added to the beginning of the classpath used by the Java VM.

NoteA Java VM might already be loaded The classpath argument is ignored if the Java VM is already running.

The second argument to createJavaVM is a boolean that specifies whether debug information is written to a text file. See “Debugging the client”.

The JavaVM class has other methods that you can use when you create a Java VM:

The JavaVM that you create using CreateJavaVM should be a global or instance variable for the client application and should not be destroyed explicitly.

The Java VM classpath in the development environment

When PowerBuilder starts a Java VM, the Java VM uses internal path and classpath information to ensure that required Java classes are always available.

In the development environment, you can check whether the JVM is running and, if so, which classpath it is using, on the Java page of the System Options dialog box. The classpath is constructed by concatenating these paths:

The runtime Java VM classpath

At runtime, you can use the GetJavaClasspath method to determine what classpath the Java VM is using. The Java VM uses the following classpath at runtime:

For more information about the Java classpath at runtime, see “Java support”.

Classes required by servers

The classpath contains the classes required by EJB clients for EAServer. If you are using a different J2EE server, you need to add additional classes required by the application server to the system CLASSPATH. For example:

For detailed information about the files required on the client by each application server, see the documentation for the server.

Examples

This example demonstrates the creation of an instance of the Java VM that specifies the html\classes folder in an EAServer installation as a class path:

// global variables javavm g_jvm, 
// boolean gb_jvm_started
boolean isdebug
string classpath

if NOT gb_jvm_started then
  //create JAVAVM
  g_jvm = create javavm

// The Java package for the EJB is in the 
// EAServer html/classes folder
  classpath = &
  "D:\Program Files\Sybase\EAServer\html\classes;"
  
  isdebug = true
  choose case g_jvm.createJavaVM(classpath, isdebug)
  case 0
    gb_jvm_started = true
  case -1 
    MessageBox("Error", "Failed to load JavaVM")
  case -2
    MessageBox("Error", "Failed to load EJBLocator")
  end choose
end if

This additional code can be added to the previous example to create a record of the Java VM version and classpath used:

integer li_FileNum
string ls_classpath, ls_version, ls_string

li_FileNum = FileOpen("C:\temp\PBJavaVM.log", &
   LineMode!, Write!, LockWrite!, Append!)

ls_classpath = i_jvm.getjavaclasspath()
ls_version = i_jvm.getjavavmversion()
ls_string = String(Today()) + " " + String(Now())
ls_string += " Java VM Version: " + ls_version
ls_string += " ~r~n" + ls_classpath + "~r~n"

FileWrite(li_FileNum, ls_string)
FileClose(li_filenum)