ADC Home > Reference Library > Technical Q&As > Legacy Documents > Java >

Legacy Documentclose button

Important: This document is part of the Legacy section of the ADC Reference Library. This information should not be used for new development.

Current information on this Reference Library topic can be found here:

Scripting Java-based command-line interface applications


Q: How do I run a command-line Java application from AppleScript without getting a timeout? Since I have no interface components, the supplied MRJ 'aete' doesn't help. When I ask to generate a dictionary for the command-line class, MyParser, I just get a "MyParser" Class without methods, i.e., there is no "main" verb corresponding to the main method.

So I put the command-line parameters into JBindery and tell the JBound application to run. The JBound "MyParser" application runs fine and quits, but nothing is returned to the calling AppleScript. As a result, the event times out. How do I make this work correctly?

A: The MRJ scripting model is very similar to scripting an ordinary scriptable Mac application. You have a scriptable application running and you send scripting commands to it. Typically, you would make a JBound application, launch it, and script it by addressing one of its AWT-based windows.

If you are working with a command-line-based Java application that doesn't create any AWT windows, then your application is not following the typical MRJ scripting model. To script your application, you will need to follow a few extra steps in order to be able to use scripting.

First, you need a Java shell application that is running to send scripting commands. You can write a trivial application that has an empty main(), or you can use any existing Java application and treat it as a shell.

The next requirement is to ensure that the jar file that contains main() used by the command-line application is on the class path. If the jar file is already in the "MRJClasses" folder, you don't need to do anything. If your jar is not in the "MRJClasses" folder, you will need to add it to the classpath using the following script:

tell
application "myJavaShell"
   start tool alias "HD:myJavaStuff:TrivialClass.jar" selecting null
end tell

Now you can invoke main:

tell
application "myJavaShell"
   apply to class "com.xyz.TrivialClass" java method
"main" parameters { "alpha", "beta"}
end tell

But you still will not get any result since your class is declared to return void:

static void
main(String args[]){...}

If you want to get the output of the console window, you can do the following:

tell
application "myJavaShell"
    get text content of text area 1 of window 1
end tell

[Apr 27 1999]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.