TITLE
    WebObjects 4: Creating a Java Tool Project
Article ID:
Created:
Modified:
70110
10/4/99
10/12/99

TOPIC

    A tool is typically a GUI-less application, run from the command line, that is focused on one or just a few tasks. There are a few tricks to creating a Java tool project in Project Builder; this document helps you through the process.


DISCUSSION

    First, create your project as a WebObjects Application project type. This insures that all the required Java support is built into the project. The only frameworks that are required are the Foundation.framework and the JavaVM.framework. The only source that is required is the myWOApp_main.m file and two Java classes. Remove all other frameworks and classes not required for your project.

    Then set up the myWOApp_main.m file to initialize the Java virtual machine and instantiate a Java class. Here is the standard implementation:

    #import <JavaVM/NSJavaVirtualMachine.h>
    int main(int argc, const char *argv[])
    {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Class JavaToolClass;
    id javaToolObject;
    // required for Solaris only to notify cthreads layer the main thread is being created
    #if defined(__svr4__)
    unsigned long java_reserved[16];
    void cthread_set_java_vm_stack_base(void *);
    cthread_set_java_vm_stack_base(java_reserved);
    #endif
    [NSBundle mainBundle];  //this initializes the JVM
    JavaToolClass = NSClassFromString(@"Tool");
    javaToolObject = [[JavaToolClass alloc] init];
    NSLog (@"Java Tool Object = %@",javaToolObject);
    [pool release];
    exit(0);       // insure the process exit status is 0
    return 0;      // ...and make main fit the ANSI spec.
    }

    The Java class Tool.java has been created. In this approach, Tool.java consists only of a constructor that instantiates another Java class, ToolImplementer.java here, which does the real work of this tool. After a ToolImplementer object is instantiated, execute one of its methods to start the tool working:

    import java.lang.*;
    public class Tool extends Object {
    public Tool() {
    super();
    System.out.println("Creating a Tool instance");
    ToolImplementer toolImplementer = new ToolImplementer();
    toolImplementer.executeSomething();
    System.out.println("toolImplementer.executeSomething() is executed");
    System.exit(0);
    }
    }

    Next, create the working class. The method "executeSomething" can of course be anything you want to begin accomplishing your tool's purpose:

    import java.lang.*;
    public class ToolImplementer extends Object {
    public ToolImplementer() {
    super();
    System.out.println("Creating a ToolImplementer instance");
    }
    public void executeSomething() {
    System.out.println("This is a ToolImplementer instance method");
    }
    }

    Finally, create any other Java classes that you need for your tool. And that's all there is to it!


Document Information
Product Area: WebObjects
Category: WebObjects 4
Sub Category: Development

Copyright © 2000 Apple Computer, Inc. All rights reserved.