Generating a Java Applet and a Java Application




Before converting a project into Java, ensure that the code functions correctly in its native environment (VB or Access) and includes code that can be automatically converted into Java syntax. Unsupported Visual Basic statements generate syntax error messages. Even though there are errors, most of the code will have been converted to Java. If you do encounter errors you will either need to modify the VB code or you will need to modify the generated Java code.

Press the "Convert to Java" button to generate the Java code based on project files. The files generated in this process are output to the designated directory or to the same directory as the original files depending on the option set. The generated code will operate as both an applet or an application depending on how it is used. When it is invoked from an html file it runs as an applet. It operates as an application when invoked using the java.exe from a command-line, shortcut, or from another application. You do not need to decide at generation time how you will use it.

The following example shows the original Visual Basic files for an application named Project1 and the corresponding Java files that are produced. It assumes that the module names are the same as the file name. The file name for the Java files always corresponds to the name of the Form, Module, Control, Type, etc. A .java and .class file is also generated for every User Defined Type contained in the Forms, code modules and classes.

VB Files Java Files Java Description
Project1.vbp Applet or application
  Project1.class compiled
  Project1.html example html startup
  Project1.bat or Project1.jpr dos batch file to compile the project or a JBuilder project file.
frmTest.frm Java Form file
  frmTest.class compiled
modTest.bas Java class with each function as a static function of the class
  modTest.class compiled
clsTest.cls Java Class file
  clsTest.class compiled

Project1.class can be used as an applet by loading the Project1.html in a browser or it can be run as a stand-alone application by executing "java Test" from a DOS prompt.

The Run button runs the application as a stand-alone application in the Java Virtual Machine (JVM). Any runtime errors will be displayed in the DOS box that is also displayed.

Run as an Applet

To test the application as an applet, open the Project1.html file in a browser. The startup form is embedded into the sample Project1.html page. You can cut and paste the html code beginning with <object and ending with </object> (for Java 1.1 it is <applet and </applet>) into any other html file. If you are using the Java 2 swing option then your browser will need the Java 2 plugin. It is installed by default when you install the Java 2 SDK.

The applet contains references to all of the Forms in your project. Upon creation, it invokes the startup form specified in the project properties and embeds it in the html page.

Trouble shooting: If you get an error running the applet, you may not have everything installed correctly. It is useful to open the Java Console to see a more detailed explanation of the error condition. If you are using Java 2, the default option, you will need to run the Start->Programs->Java Plugin Control Panel menu item. If you do not have this then you have not installed the Java 2 JRE or SDK. On the Java Plugin Control Panel check the Show Java Console checkbox and the Java Console will display each time you run a Java 2 Applet in your browser.

If you are running as a Java 1.1 Applet then it will use the builtin Java engine and you should launch the Java Console by clicking on the Communicator->Tools->Java Console menu item in Netscape or the View->Java Console menu item in Internet Explorer.

If you see a class not found error then you do not have the Java class library installed properly. See the deployment section.

Run as stand-alone application

Press the Run Java button on the toolbar or you can execute the resulting standalone Java Application from the DOS prompt using the following method.

1. Start a new DOS shell (Command Prompt).
2. Change to the directory where the application resides.
3. Enter "java <project_name>" i.e. "java Project1" to invoke the application.

Java invokes the Java Virtual Machine and executes Project1 project. This invisible Java Frame contains references to all of the Forms and Modules in your VB Project. Upon creation, it invokes the startup form specified in the project settings.

Support For User Defined Types

User defined types (UDT) can be created in Visual Basic using the "Type" statement. Each UDT is generated as its own Java class by the same name as the name of the Type.