Select to view content in your preferred language

Unable to initialize ArcObjects environment (Java API)

4954
4
11-16-2010 12:25 AM
AlexandreHirzel
Occasional Contributor
Hello,

I have a simple console application developed in Java. It works fine in the NetBeans IDE, but when I try to run the jar file from a command line (Windows XP) I get this error message: "Unable to initialize ArcObjects environment. AutomationException: 0x800401f5 - The specified product or version does not exist on this machine. in 'ArcGISVersion.Version'". This error is triggered by the EngineInitializer.initializeEngine() line of my code.

Of course, I do have ArcGIS 10 installed on my machine, and this error does not happen in NetBeans IDE.

The main class of my application is the Bootstrapper I found somewhere on this forum and it is working well (its purpose is to locate the arcobjects.jar). The error happens later, when I try to initialise the engine.

Here are snippets of my code:
The method causing the error:
  public static void main(String[] args) {
    try {
      // Initialise ArcGIS
      EngineInitializer.initializeEngine(); // This line triggers the error
      AoInitialize aoInit = new AoInitialize();
       initializeArcInfoLicense(aoInit);

      INetworkQuery networkQuery =openNetworkQuery(args[0], args[1], args[2]);
      computeMetrics(networkQuery);

      // Release the license.
      aoInit.shutdown();

    } catch (IOException ex) {
      System.out.println(ex.getMessage());
    }
  } //End of method main.


The bootstrapper class:
public class Bootstrapper {
    public static void main(String[] args)throws Exception{
        bootstrapArcobjectsJar();
        //Replace the following line with the name of your main application.
        // App.main(args);
        TopologicalMetrics.main(args);
        System.out.println("done.");
    }

    public static void bootstrapArcobjectsJar(){
        String arcEngineHome = System.getenv("ARCGISHOME");
        String jarPath = arcEngineHome + "java" + File.separator + "lib" +
            File.separator + "arcobjects.jar";

        File f = new File(jarPath);

        URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader()
            ;
        Class sysclass = URLClassLoader.class;

        try{

            Method method = sysclass.getDeclaredMethod("addURL", new Class[]{
                URL.class
            }
            );
            method.setAccessible(true);
            method.invoke(sysloader, new Object[]{
                f.toURL()
            }
            );

        }
        catch (Throwable t){

            t.printStackTrace();
            System.err.println("Could not add arcobjects.jar to system classloader");

        }
    }
0 Kudos
4 Replies
StevenRozic
Emerging Contributor
First, if you have ArcGIS 10, then your code should not work. At the release of ArcGIS 10, the ARCGISHOME environment variable is no longer used because the SDK can be installed in an entirely different location from the Engine or Desktop runtime.

The environment variables that replaced ARCGISHOME are the following:

1. If you have ArcGIS Desktop installed and are trying to use the Engine runtime that is packaged with desktop, then use the AGSDESKTOPJAVA. This environment variable will point to the runtime location of where ArcGIS Desktop is installed on the machine. If you do a default installation, then the path would be something like the following on XP: C:\program files\ArcGIS\Desktop10.0\java\lib (the desktop version of arcobjects.jar should be in this location).

2. If you have ArcGIS Engine Runtime installed and are trying to use this as your runtime, then use the AGSENGINEJAVA environment variable. This environment variable will point to the runtime location of where ArcGIS Engine is installed on the machine (yes, this location can now be different than your desktop software and yes you can have desktop and engine installed on a machine). If you do a default installation, then the path would be something like the following on XP: C:\program files\ArcGIS\Engine10.0\java\lib (the engine runtime version of arcobjects.jar should be in this location).

On thing to pay attention to is that sometimes Windows does not remove environment variables, so you might still have ARCGISHOME as an environment variable on your machine, even though you uninstalled the previous version of ArcGIS from your computer. If this is the case, remove this old environment variable and double-check that the new environment variables above are present and pointing to the correct locations (they should be, unless you manually moved your ArcGIS installation location).

I think this will help your application discover the correct runtime, then allow it to initialize correctly.

Hope this helps!

-=Steve
0 Kudos
StevenRozic
Emerging Contributor
I also forgot to mention that a large majority of the samples require the boostrapping process. If you have a look at the loadsample package and open the Main.java, you can see a code sample of how our samples boostrap the location of the arcobjects.jar and execute (might be another useful tool to help you troubleshoot with).

-=Steve
0 Kudos
AlexandreHirzel
Occasional Contributor
Thank you very much.

Your pointers were instrumental in getting me back on the rails.

I used the bootstrapping method I found in the samples. The next step to get it work, was to remove the arcobjects.jar that NetBeans automatically places in the dist/lib folder.

Cheers!

          Alexandre
0 Kudos
StevenRozic
Emerging Contributor
Excellent! 😄

-=Steve
0 Kudos