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.

A Java VM might already be loaded

The classpath argument is ignored if the Java VM is already running.

Supported Java VM

PowerBuilder 2017 and later is compatible with Java VM 1.6 only (not 1.7 or 1.8).

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 CreateJavaInstance method creates an instance of the Java object from a proxy name.

  • The IsJavaVMLoaded method determines whether the Java VM is already loaded. Use this method before calling CreateJavaVM if you want to enable or disable some features of your application depending on whether the Java VM has already been loaded. This will ensure that the classpath argument passed to CreateJavaVM is ignored.

  • The GetJavaVMVersion method determines which version of the Java VM is running.

  • The GetJavaClasspath method determines the runtime classpath of the 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:

  • A classpath added programmatically when the Java VM is started. For example, the classpath you pass to the CreateJavaVM method.

  • The PowerBuilder runtime static registry classpath. This path is built into the pbjvm170.dll and contains classes required at runtime for EJB clients and other PowerBuilder features that use a Java VM.

  • The PowerBuilder system classpath. This path resides in a Windows registry key installed when you install PowerBuilder. It contains classes required at design time for Java-related PowerBuilder features such as JDBC connectivity.

  • The PowerBuilder user classpath. This is the path that you specify on the Java page of the System Options dialog box.

  • The system CLASSPATH environment variable.

  • The current directory.

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:

  • A classpath added programmatically when the Java VM is started

  • The PowerBuilder runtime static registry classpath

  • The system CLASSPATH environment variable

  • The current directory

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 the J2EE application server, you need to add the classes required by the application server to the system CLASSPATH. For example:

  • For WebLogic, weblogic.jar. This file is installed in wlserver6.1\lib or weblogic700\server\lib on the server.

  • For WebSphere, JAR files installed on the server in websphere\appserver\lib.

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 wlfullclient.jar file in a WebLogic 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
// wlfullclient.jar
  classpath = &
  "D:\Program Files\weblogic\wlfullclient.jar;"
  
  isdebug = true
  choose case g_jvm.createJavaVM(classpath, isdebug)
  case 0,1
    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)