STK Java API - Frequently Asked Questions

Topics

Why do I receive an exception stating "jawt module load failed" at application startup?

When running an STK Java API application, if you receive the following exception message...
agi.core.AgCoreException: jawt module load failed
    at agi.core.AgCanvas.getNativeCanvas(Unknown Source)
    at agi.core.AgContainer.paint(Unknown Source)

...you can resolve this issue by implementing the following directions:

  1. The STK Java API makes use of the JDK/JRE jawt.dll on windows and libjawt.so on Unix/Linux.
  2. The STK Java API will first attempt to find this library based on the setting of the JAVA_HOME environment variable if set.
  3. Otherwise, make sure that the fully qualified directory path of the jawt library (jawt.dll or libjawt.so) that accompanies the JDK/JRE virtual machine you are using to run your Java application is set within the PATH environment variable for Windows and LD_LIBRARY_PATH for Unix/Linux.
  4. If the STK Java API can not determine the location of the jawt library it will attempt to natively load the library using the system library path settings.

Why do I receive an exception stating "java.lang.NoClassDefFoundError: agi/...JNI" at application startup?

When running the STK Java API application, if you receive an exception similar to ...
java.lang.NoClassDefFoundError: agi/stkobjects/astrogator/AgStkobjectsAstrogator_JNI
    at agi.core.awt.AgAwt_JNI.initializeJNI_All(Unknown Source)
    at agi.core.awt.AgAwt_JNI.initialize(Unknown Source)
    at agi.samples.customapplications.allowscrollbars.Main.main(Main.java:74)
Caused by: java.lang.ClassNotFoundException: agi.stkobjects.astrogator.AgStkobjectsAstrogator_JNI
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 3 more

...you can resolve this issue by adding the specific STK Java API jar file to the application's configuration settings. The exception above was experienced because the agi.stkobjects.astrogator.jar library was not added to the application's configuration settings.


Why do I receive an exception stating "java.lang.UnsatisfiedLinkError: no agjni... in java.library.path" at application startup?

When running the STK Java API application, if you receive an exception similar to ...
java.lang.UnsatisfiedLinkError: no agjnicore in java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at agi.core.AgCore_JNI.load(Unknown Source)
    at agi.core.AgCore_JNI.(Unknown Source)
    at agi.core.awt.AgAwt_JNI.initializeJNI_All(Unknown Source)
    at agi.core.awt.AgAwt_JNI.initialize(Unknown Source)
    at agi.samples.customapplications.allowscrollbars.Main.main(Main.java:74)
agi.core.awt.AgAwtException: agi.core.awt.AgAwtException: agi.core.AgCoreException: java.lang.reflect.InvocationTargetException
    at agi.core.awt.AgAwt_JNI.initialize(Unknown Source)
    at agi.samples.customapplications.allowscrollbars.Main.main(Main.java:74)
Caused by: agi.core.awt.AgAwtException: agi.core.AgCoreException: java.lang.reflect.InvocationTargetException
    at agi.core.awt.AgAwt_JNI.initializeJNI_All(Unknown Source)
    ... 2 more
Caused by: agi.core.AgCoreException: java.lang.reflect.InvocationTargetException
    at agi.core.AgCore_JNI.initialize(Unknown Source)
    ... 3 more
Caused by: agi.core.awt.AgAwtException: java.lang.reflect.InvocationTargetException
    at agi.core.awt.AgAwtToolkitThreadDelegate.invokeAndWait(Unknown Source)
    at agi.AgWindowToolkitThreadDelegate.invokeAndWait(Unknown Source)
    ... 4 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.awt.EventQueue.invokeAndWait(Unknown Source)
    at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)
    ... 6 more
Caused by: java.lang.UnsatisfiedLinkError: agi.core.AgCore_JNI.Initialize()V
    at agi.core.AgCore_JNI.Initialize(Native Method)
    at agi.core.AgCore_JNI.initializeInternal(Unknown Source)
    at agi.core.AgCore_JNI.access$200(Unknown Source)
    at agi.core.AgCore_JNI$InitializeRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

...you can resolve this issue by adding the specific STK Java API JNI library parent directory to the application's configuration settings.


Why do I receive an exception stating "wrong ELF class: ELFCLASS32" at application startup?

Currently STK Engine On Unix is only availiable in 32bit so you must make sure you are using a 32bit JDK/JRE when trying to run a java project. With eclipse go to Window -> Preferences -> Java -> Installed JREs and make sure a 32bit JRE is selected.

Why do I receive an exception stating "agi.core.AgCoreException from agi.core.AgUnknown.createInstance(Unknown Source)"?

When running the STK Java API application, if you receive an exception similar to ...
agi.core.AgCoreException
    at agi.core.AgUnknown.createInstance(Unknown Source)
    at agi.core.AgUnknown.initialize(Unknown Source)
    at agi.core.AgUnknown.(Unknown Source)
    at agi.stkx.AgSTKXApplication.(Unknown Source)
    at agi.samples.allowscrollbars.AllowScrollbars.initialize(AllowScrollbars.java:108)
    at agi.samples.allowscrollbars.AllowScrollbars.(AllowScrollbars.java:101)
    at agi.samples.allowscrollbars.AllowScrollbars.main(AllowScrollbars.java:90)
Caused by: java.lang.reflect.InvocationTargetException
    at java.awt.EventQueue.invokeAndWait(EventQueue.java:997)
    at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1323)
    ... 7 more
Caused by: agi.core.AgComException: Could not retrieve COM error description
    at agi.stkx.AgSTKXApplication.CreateInstance(Native Method)
    at agi.core.AgUnknown$CreateInstanceRunner.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

...you can resolve this issue by making sure STK / STK Engine is properly installed and its STKX / STKUtil / STKObjects dll's are registered properly.

How do I create/initialize STK Engine?

To create the STK Engine, you create and keep a reference to the agi.stkx.AgSTKXApplicationClass class. Code as follows...
import agi.stkx.*;

public class MyClass
{
    private AgSTKXApplicationClass m_AgSTKXApplication;
    
    public MyClass()
    {
        //... other STK Java API initialization depending on AWT/SWT/Headless context
        
        this.m_AgSTKXApplication = new AgSTKXApplicationClass();
        
        //... continue with additional STK Java API code
    }
    
    //... other code
}

How do I access the Object Model for the STK Engine?

To access the Object Model for the STK Engine, you create and keep a reference to the agi.stkobjects.AgStkObjectRootClass class. Code as follows...
import agi.stkx.*;
import agi.stkobjects.*;

public class MyClass
{
    private AgSTKXApplicationClass m_AgSTKXApplication;
    private AgStkObjectRootClass m_AgStkObjectRoot;
    
    public MyClass()
    {
        // ... other STK Java API initialization depending on AWT/SWT/Headless context
        
        this.m_AgSTKXApplication = new AgSTKXApplicationClass();
        
        // NOTE: You must create the AgSTKXApplication (STK Engine) before creating the Object Model Root object
        this.m_AgStkObjectRoot = new AgStkObjectRootClass();
        
        //... continue with additional STK Java API code
    }
}

How do I create a new STK application process/instance (with GUI) from my Java code?

To create the a new STK application instance (with GUI), you create and keep a reference to the agi.stk.ui.AgStkUi class. Code as follows...
import agi.stk.ui.*;

public class MyClass
{
    private AgStkUi m_AgStkUi;
    
    public MyClass()
    {
        //... other STK Java API initialization depending on AWT/SWT/Headless context
        
        this.m_AgStkUi = new AgStkUi();
        
        //... continue with additional STK Java API code
    }
    
    //... other code
}

How do I attach/communicate to an existing/running STK application process/instance (with GUI) from my Java code without using TCP/IP?

To attach (i.e., using Process-to-Process communication NOT using TCP/IP) an existing STK application process/instance (with GUI), you statically retrieve and keep a reference to the agi.stk.ui.AgStkUi class. Code as follows...
import agi.stk.ui.*;

public class MyClass
{
    private AgStkUi m_AgStkUi;
    
    public MyClass()
    {
        //... other STK Java API initialization depending on AWT/SWT/Headless context
        
        this.m_AgStkUi = AgStkUi.getStkUiInstance();
        
        //... continue with additional STK Java API code
    }
}

How do I access the Object Model from the STK instance?

To get a reference to the Object Model from a STK application process/instance (with GUI), from a reference of type agi.stk.ui.AgStkUi class. Code as follows...
    // NOTE: You must previously create/attach to an AgStkUi instance before retrieving the Object Model Root refernce
    AgStkObjectRootClass root = (AgStkObjectRootClass)this.m_AgStkUi.getIAgStkObjectRoot();

How do I create a new Scenario and Facility using Object Model?

To create scenario on an instance of the Object Model Root, code as follows...
    this.m_AgStkObjectRoot._newScenario("test");
    IAgStkObject scenObj = this.m_AgStkObjectRoot.getCurrentScenario();
    IAgStkObject facObj = scenObj.getChildren()._new(AgESTKObjectType.eFacility, "facility1");

If I DON'T use the Auto Class Cast feature provided in STK 10.0 or later, how do I cast from the IAgStkObject interface to a derived type such as AgScenario/IAgScenario or AgFacility/IAgFacility?

    // NOTE: Instead of using a casting operator you must pass a reference
    // of a base type into a constructor of a derived type in order to access a specific type's functionality. This is due to the
    // fact that there is no Java operator overloading and the necessity to do a native cast on the native resources.

    IAgScenario scen1 = new AgScenario(m_AgStkObjectRoot.getCurrentScenario(),b>);

    // or

    IAgScenario scen2 = new AgScenarioClass(m_AgStkObjectRoot.getCurrentScenario());

    // or

    AgScenarioClass scen3 = new AgScenarioClass(m_AgStkObjectRoot.getCurrentScenario());
    
    // NOTE: Instead of using a casting operator you must pass a reference
    // of a base type into a constructor of a derived type in order to access a specific type's functionality. This is due to the
    // fact that there is no Java operator overloading and the necessity to do a native cast on the native resources.

    IAgFacility fac1 = new AgFacility(scen3.getChildren()._new(AgESTKObjectType.eFacility, "Fac1"));

    // or

    IAgFacility fac2 = new AgFacilityClass(scen3.getChildren()._new(AgESTKObjectType.eFacility, "Fac1"));

    // or

    AgFacilityClass fac3 = new AgFacilityClass(scen3.getChildren()._new(AgESTKObjectType.eFacility, "Fac1"));

If I DO use the Auto Class Cast feature provided in STK 10.0 or later, how do I cast from the IAgStkObject interface to a derived type such as AgScenario/IAgScenario or AgFacility/IAgFacility?

    // NOTE: Simply use standard Java casting operators, and the native resource is
    // auto casted to its "*Class" equivalent that implements all possible interfaces for that type.

    // The following are all possible due to this multiple implementations of interfaces within
    // the "*Class" version returned from methods when the Auto Class Cast feature is used


    IAgScenario scen1 = (IAgScenario)m_AgStkObjectRoot.getCurrentScenario();

    // or

    IAgScenario scen2 = (AgScenarioClass)m_AgStkObjectRoot.getCurrentScenario();

    // or

    AgScenarioClass scen3 = (AgScenarioClass)m_AgStkObjectRoot.getCurrentScenario();
    
    // NOTE: Simply use standard Java casting operators, and the native
    // resource is auto casted to its "*Class" equivalent that implements all
    // possible interfaces for that type.

    IAgFacility fac1 = (IAgFacility)scen3.getChildren()._new(AgESTKObjectType.eFacility, "Fac1");

    // or

    IAgFacility fac2 = (AgFacilityClass)scen3.getChildren()._new(AgESTKObjectType.eFacility, "Fac1");

    // or

    AgFacilityClass fac3 = (AgFacilityClass)scen3.getChildren()._new(AgESTKObjectType.eFacility, "Fac1");

How do I call a method with one or more AgVariant type parameters?

    IAgStkObject acobj = scenario.getChildren().getItem(new AgVariant("F16"));

    // or

    IAgStkObject acobj = scenario.getChildren().getItem(new AgVariant(1));

How do I call a method with one or more Object type parameters?

    IAgStkObject acobj = scenario.getChildren().getItem("F16");

    // or

    // If auto boxing IS NOT supported in your JRE version
    IAgStkObject acobj = scenario.getChildren().getItem(new Integer(1));
    // or

    // If auto boxing IS supported in your JRE version
    IAgStkObject acobj = scenario.getChildren().getItem(1);

How do I call a method with one or more AgSafeArray type parameters?

    AgVariant[] vars = new AgVariant[]
    {
        new AgVariant(39.88), new AgVariant(-75.25), new AgVariant(0), // Philadelphia
        new AgVariant(38.85), new AgVariant(-77.04), new AgVariant(0), // Washington, D.C.
        new AgVariant(29.98), new AgVariant(-90.25), new AgVariant(0), // New Orleans
        new AgVariant(37.37), new AgVariant(-121.92), new AgVariant(0) // San Jose
    };
    
    AgSafeArray positions = new AgSafeArray(AgVariantTypes.VT_VARIANT, vars.length);
    positions.setVariantArray(vars);
    
    IAgStkGraphicsMarkerBatchPrimitive markerBatch = null;
    markerBatch = manager.getInitializers().getMarkerBatchPrimitive().initializeDefault();
    String filePath = DataPaths.getDataPaths().getSharedDataPath("Markers/facility.png");
    markerBatch.setTexture(manager.getTextures().loadFromStringUri(filePath));
    markerBatch.setCartographic("Earth", positions);

How do I call a method with one or more Object reference types that are arrays as parameters?

    Object[] positions = new Object[]
    {
        new Double(39.88), new Double(-75.25), new Double(0), // Philadelphia
        new Double(38.85), new Double(-77.04), new Double(0), // Washington, D.C.
        new Double(29.98), new Double(-90.25), new Double(0), // New Orleans
        new Double(37.37), new Double(-121.92), new Double(0) // San Jose
    };
    
    IAgStkGraphicsMarkerBatchPrimitive markerBatch = null;
    markerBatch = manager.getInitializers().getMarkerBatchPrimitive().initializeDefault();
    String filePath = DataPaths.getDataPaths().getSharedDataPath("Markers/facility.png");
    markerBatch.setTexture(manager.getTextures().loadFromStringUri(filePath));
    markerBatch.setCartographic("Earth", positions);

How do I call a method with a AgVariant return type and convert it to a native Java Object reference type?

    // NOTE: Use the method that returns an AgVariant,
    // then call the getObject() method on the variant
    // and then cast to the dervied object reference type.


    AgVariant vStartTime = scenario.getStartTime();
    String sStartTime = vStartTime.getString();

How do I call a method with a Object return type and convert it to a native Java Object reference type?

    // NOTE: Use sibling methods with _AsObject suffixes
    // and then cast to the dervied object reference type.


    String startTime = (String)scenario.getStartTime_AsObject();

How do I call a method with a AgSafeArray return type?


    AgSafeArray markers = scenario.getVO().availableMarkerTypes();

    int dimCount = markers.getDimCount();
    short saType = markers.getVariantType();
    int rowCount = markers.getRowCount();
    int colCount = markers.getColumnCount();

    // Single dimensional array (1D), determined by dimCount or rowCount of 1.
    // If two dimensional array (2D), dimCount would be 2, but rowCount could be anything
    // from 1 ... *
    if( dimCount == 1 && rowCount == 1)
    {
        if(saType == AgVariantTypes.VT_VARIANT )
        {
            for( int colIndex = 0; colIndex < colCount; colIndex++ )
            {
                // Using only the 1D method for simplification.
                AgVariant vMarkerType = markers.getVariant(colIndex);
                String sMarkerType = (String)vMarkerType.getObject();

                System.out.println("\t[" + colIndex + "] = " + sMarkerType);
            }
        }
    }

How do I call a method with an Object return type that is an array?

    // NOTE: Simply use standard Java casting operators from Object to String, Double, etc.

    Object markers = scenario.getVO().availableMarkerTypes_AsObject();

    // Using only the 1D method for simplification.
    if(markers instanceof Object[])
    {
        Object[] markersArray = (Object[])markers;

        int colCount = markers.length;

        for(int colIndex = 0; colIndex < colCount; colIndex++)
        {
            String markerType = (String)markersArray[colIndex]);

            System.out.println("\t[" + colIndex + "] = " + markerType);
        }
    }
    else if(markers instanceof Object[][])
    {
        // Handle 2 dimensional array
    }

How do I convert an AWT color to an OLE color?

    Color awtColor = Color.BLACK;
    long oleColor = AgAWTOLEColorTranslator.fromAWTtoOLE(awtColor);
    AgGlobeCntrlClass globe = new AgGlobeCntrlClass();
    globe.setBackColor(oleColor);

How do I convert an OLE color to an AWT color?

    AgGlobeCntrlClass globe = new AgGlobeCntrlClass();
    long oleColor = globe.getBackColor();
    Color awtColor = AgAWTOLEColorTranslator.fromOLEtoAWT(oleColor);

STK Programming Interface 11.0.1