Running Gosu programs and calling other classes

To use Gosu, the initial file that you run must be a Gosu program. A Gosu program file has the .gsp file name extension. Gosu code in a program can call out to other Gosu classes and other types, such as Java classes.

You can run Gosu programs (.gsp files) directly from the command line. You cannot run a Gosu class file or other types of file directly. If you want to call a Gosu class or other type of file, make a Gosu program that uses those other types.

In Java, you define a main method in a class and tell Java which main class to run. This method calls out to other classes as needed.

In Gosu, your main Gosu program (.gsp file) can call any necessary code, including Gosu or Java classes. If you want to mirror the Java style, your .gsp file can contain a single line that calls a main method on a Gosu class or Java class.

To tell Gosu where to load additional classes, do either of the following:

  • Use the Class-Path attribute in the manifest of a JAR file.
  • Use the -classpath option on the command-prompt tool.

You can combine these two options for greater flexibility and to support very long class paths.

Using the Class-Path attribute in the manifest of a Java library file

You can add a Class-Path attribute line in the MANIFEST.MF file, which is the manifest file in any Java library file. The value of the Class-Path attribute has an arbitrary length, so you can use this attribute if you need a class path that is longer than a command prompt supports.

The Class-Path attribute can contain one or more URLs for JAR file names. To specify multiple JAR files in the class path, use a space character to separate the file names. To specify an absolute path to a local file use the file:/ prefix.

The following lines show a manifest file that contains a Class-Path attribute. The class path contains a library in the same directory as the library that contains the manifest, a library in a subdirectory, and a library at an absolute path.
Manifest-Version: 1.0
Class-Path: MyUtils.jar classfiles/MyClasses.jar file:/C:/javalib/AppHelpers.jar
Created-By: 1.8.0_92 (Oracle Corporation)

Using the -classpath option on the command-prompt tool

To use other Gosu classes, Java classes, or Java libraries, you perform the following steps.

  1. Create a package-style hierarchy for your class files on your disk. For example, if the root of your files is PolicyCenter/MyProject/, put the class files for the Gosu class com.example.MyClass at the location PolicyCenter/MyProject/com/example/MyClass.gs.
  2. At the command prompt, tell Gosu where to find your other Gosu classes and Java classes by adding the ‑classpath option to the gosu command.

Typically you place Java classes, Gosu classes, or libraries in subdirectories of your main Gosu program.

For example, suppose you have a Gosu program at this location:

C:\gosu\myprograms\test1\test.gsp

Copy your class file for the class mypackage.MyClass to the location:

C:\gosu\myprograms\test1\src\mypackage\MyClass.class

Copy your library files to the location:

C:\gosu\myprograms\test1\lib\mylibrary.jar

For this example, add two directories to the class path with the following command:

test.gsp -classpath "src;lib"

See also