Baffling Java exception: URI is not hierarchical

Queries related to T-Plan Robot
Post Reply
AndyKrouwel

Baffling Java exception: URI is not hierarchical

Post by AndyKrouwel » Thu Feb 28, 2013 5:29 pm

Hello,

I'm having a problem with my Java project that includes testplan robot.

It's fine when I run it from inside Eclipse, but when I make a runnable jar (containing what I think are all the dependent jars) and run it from the command line, it throws an exception from deep inside my call to tplan.robot.ApplicationSupport:

java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1577)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1555)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:236)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:114)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:1119)
at com.tplan.robot.ApplicationSupport.getString(ApplicationSupport.java:1136)
at com.tplan.robot.util.Utils.<clinit>(Utils.java:90)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:236)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:114)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:1119)
at com.tplan.robot.ApplicationSupport.init(ApplicationSupport.java:396)
at com.tplan.robot.ApplicationSupport.<init>(ApplicationSupport.java:292)
at com.cognitomobile.mobile.automation.Base_Automation.setup(Base_Automation.java:192)
at com.cognitomobile.mobile.automation.CLI_Automation.<init>(CLI_Automation.java:61)
at com.cognitomobile.mobile.automation.Entry$1.run(Entry.java:36)
at java.lang.Thread.run(Unknown Source)

Looks like it's trying to open a file from inside the jar, and failing. It does this four times one after the other, then falls over.

When run from Eclipse the wider classpath is probably picking up the file from the file system and so it's happy.
When run from command line, it doesn't find the file.

I could expand the CLASSPATH for the command-line run, but this doesn't really help because then it'll only run on a development machine and not be a very useful jar.

Unfortunately I don't know what files it's not finding either, unfortunately, as it doesn't say in the exception so I can't check if they're in the JAR or not. I've recently added another external jar and dependencies, but I haven't removed anything.

StackOverflow suggests that it might be trying to open a file from the jar, rather than an inputStream but again I can't tell what's going on inside.

Can you shed any light on my troubles?

Ta

Andy Krouwel

rpes
User
User
Posts: 74
Joined: Thu Oct 29, 2009 5:43 pm
First Name: Robert
Last Name: Pes
Company: T-Plan

Re: Baffling Java exception: URI is not hierarchical

Post by rpes » Thu Feb 28, 2013 8:04 pm

Robot is in this phase trying to identify its install directory, i.e. where the robot.jar file is located. This is used for multiple features, for example loading of plugins from the plugins/ directory. In this particular case Robot needs to search the install path for third party resource bundles (translated message files). There's no harm if this fails because you are not relying on external resource files. Robot then happily falls back to the default resource bundles packed inside robot.jar.

The code responsible for the Jar location:

Code: Select all

    
public static File getJarFile(Class clazz) {
        if (clazz == null) {
            clazz = Utils.class;
        }
        String s = null;
        String thisPath = clazz.getName().replace(".", "/") + ".class";
        try {
            URI url = clazz.getClassLoader().getResource(thisPath).toURI();
            String protocol = url.getScheme();
            if (protocol != null && protocol.equals("jar")) {  // We are running from JAR file
                String path = url.getRawSchemeSpecificPart();
                s = path.substring(0, path.indexOf("!"));
                URI uri = new URL(s).toURI();
                File f = new File(uri);
                return f;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
The mechanism is fairly simple. The method takes one of the classes packed inside robot.jar, usually the com.tplan.robot.util.Utils one, and retrieves its URI using the class loader getResource() method. When Robot is running from the robot.jar file the URI usually looks like:

Code: Select all

jar:file:/tmp/Robot/robot.jar!/com/tplan/robot/util/Utils.class
Hence, when the URI starts with 'jar' the method cuts out the path to the JAR file and returns it as a new File instance.

It is not obvious why the exception is thrown. Perhaps the URI does not comply with the usual format? Anyway, it would help to see it. Should you want to pursue it please put the above method to your code, add a System.out.println(uri) line tu dump the value to the console and call it as getJarFile(com.tplan.robot.util.Utils.class). Then post the URI to this forum.

Thanks,
Robert Pes

AndyKrouwel

Re: Baffling Java exception: URI is not hierarchical

Post by AndyKrouwel » Fri Mar 01, 2013 11:48 am

Thanks,

I'll have a look with this later and get back to you with the results.
It's odd, because it's never produced the exceptions before and has been working happily for years.

Cheers!

rpes
User
User
Posts: 74
Joined: Thu Oct 29, 2009 5:43 pm
First Name: Robert
Last Name: Pes
Company: T-Plan

Re: Baffling Java exception: URI is not hierarchical

Post by rpes » Fri Mar 01, 2013 11:52 am

Same here - this functionality also hasn't been changed for years and we have never received any similar reports. It must be specific to your environment.

If you post the URI we will either fix the parsing method or suggest a workaround.

AndyKrouwel

Re: Baffling Java exception: URI is not hierarchical

Post by AndyKrouwel » Thu Mar 14, 2013 4:01 pm

OK! FInally got a chance to try this out.
The URIs are:

file:/S:/Source/AutomatedTest/Package/lib/robot.jar

in normal run, and the somewhat brief:

rsrc:robot.jar

When it gets all grumpy.
The robot.jar file is in the root of the package jar.

Full output is:

java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1221)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1199)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:232)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:113)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:885)
at com.tplan.robot.ApplicationSupport.getString(ApplicationSupport.java:902)
at com.tplan.robot.util.Utils.<clinit>(Utils.java:91)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:232)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:113)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:885)
at com.tplan.robot.ApplicationSupport.init(ApplicationSupport.java:323)
at com.tplan.robot.ApplicationSupport.<init>(ApplicationSupport.java:279)
at com.cognitomobile.mobile.automation.Base_Automation.setup(Base_Automation.java:192)
at com.cognitomobile.mobile.automation.CLI_Automation.<init>(CLI_Automation.java:87)
at com.cognitomobile.mobile.automation.Entry$1.run(Entry.java:36)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1221)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1199)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:232)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:113)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:887)
at com.tplan.robot.ApplicationSupport.getString(ApplicationSupport.java:902)
at com.tplan.robot.util.Utils.<clinit>(Utils.java:91)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:232)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:113)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:885)
at com.tplan.robot.ApplicationSupport.init(ApplicationSupport.java:323)
at com.tplan.robot.ApplicationSupport.<init>(ApplicationSupport.java:279)
at com.cognitomobile.mobile.automation.Base_Automation.setup(Base_Automation.java:192)
at com.cognitomobile.mobile.automation.CLI_Automation.<init>(CLI_Automation.java:87)
at com.cognitomobile.mobile.automation.Entry$1.run(Entry.java:36)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1221)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1199)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:232)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:113)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:885)
at com.tplan.robot.ApplicationSupport.init(ApplicationSupport.java:323)
at com.tplan.robot.ApplicationSupport.<init>(ApplicationSupport.java:279)
at com.cognitomobile.mobile.automation.Base_Automation.setup(Base_Automation.java:192)
at com.cognitomobile.mobile.automation.CLI_Automation.<init>(CLI_Automation.java:87)
at com.cognitomobile.mobile.automation.Entry$1.run(Entry.java:36)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1221)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1199)
at com.tplan.robot.l10n.LocalizationSupport.getBundleDirectory(LocalizationSupport.java:232)
at com.tplan.robot.l10n.LocalizationSupport.getResourceBundle(LocalizationSupport.java:113)
at com.tplan.robot.ApplicationSupport.getResourceBundle(ApplicationSupport.java:887)
at com.tplan.robot.ApplicationSupport.init(ApplicationSupport.java:323)
at com.tplan.robot.ApplicationSupport.<init>(ApplicationSupport.java:279)
at com.cognitomobile.mobile.automation.Base_Automation.setup(Base_Automation.java:192)
at com.cognitomobile.mobile.automation.CLI_Automation.<init>(CLI_Automation.java:87)
at com.cognitomobile.mobile.automation.Entry$1.run(Entry.java:36)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at com.tplan.robot.util.Utils.getJarFile(Utils.java:1221)
at com.tplan.robot.util.Utils.getInstallPath(Utils.java:1168)
at com.tplan.robot.util.Utils.getInstallPath(Utils.java:1158)
at com.tplan.robot.LiCheck.refresh(LiCheck.java:105)
at com.tplan.robot.LiCheck.<init>(LiCheck.java:74)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.tplan.robot.ApplicationSupport.init(ApplicationSupport.java:344)
at com.tplan.robot.ApplicationSupport.<init>(ApplicationSupport.java:279)
at com.cognitomobile.mobile.automation.Base_Automation.setup(Base_Automation.java:192)
at com.cognitomobile.mobile.automation.CLI_Automation.<init>(CLI_Automation.java:87)
at com.cognitomobile.mobile.automation.Entry$1.run(Entry.java:36)
at java.lang.Thread.run(Unknown Source)

So it's having a good few tries.

rpes
User
User
Posts: 74
Joined: Thu Oct 29, 2009 5:43 pm
First Name: Robert
Last Name: Pes
Company: T-Plan

Re: Baffling Java exception: URI is not hierarchical

Post by rpes » Thu Mar 14, 2013 4:15 pm

Thanks for the data!

I presume you are using the Eclipse' export feature. I'm afraid we can't adjust Robot for that because it hides away the JAR location. The following page should tell you how to configure your Eclipse to avoid it:

http://stackoverflow.com/questions/7507 ... tocol-rsrc

AndyKrouwel

Re: Baffling Java exception: URI is not hierarchical

Post by AndyKrouwel » Thu Mar 14, 2013 6:38 pm

Ah! Think I've found it.

The failing one is using a robot.jar from inside its own jar. The working one is using the one on the file system.
If I ditch the robot.jar from inside the not-working one the error goes away.

Phew.

That had me stumped for a good while.

rpes
User
User
Posts: 74
Joined: Thu Oct 29, 2009 5:43 pm
First Name: Robert
Last Name: Pes
Company: T-Plan

Re: Baffling Java exception: URI is not hierarchical

Post by rpes » Fri Mar 15, 2013 9:00 am

Repackaging is actually not the problem. If you unzip robot.jar, merge it with your own classes and package it into a new JAR it will work fine. The real cause is that Eclipse seems to use a custom class loader which relies on a custom resource URI.

Anyway I'll modify the getJarFile() method to display a meaningful error message when the 'rsrc' protocol is detected in the URI.

Thanks for the cooperation!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest