0

I have jar file Test2 contained in jar file Test1, and I execute Test1. When I try to execute Test2 using java.awt.Desktop I get the java.lang.IllegalArgumentException: URI is not hierarchical exception and message. I can do it successfully when I have Test2 separate from Test1 (i.e. Test2 in a different folder) and access it like below:

Desktop.getDesktop().open(new File("path-to-file\\Test2.jar"));

And I can do the following from my IDE without any problems:

Desktop.getDesktop().open(new File(Test.class.getResource("Test2.jar").toURI()));

I have problems running Test2 when I run the jar file Test1 using the above line of code.

Through research, I learned that you have to do something like InputStream is = Test.class.getClassLoader().getResourceAsStream(...);. What I couldn't figure out, however, is what, if anything, you have to do with this InputStream to successfully execute a jar from within a jar, or what this InputStream does to make the URI hierarchical. So far this is I came up with (I don't know if I even have the first file correct):

InputStream is = Test.class.getClassLoader().getResourceAsStream("Test2.jar");
Desktop.getDesktop().open(new File(Test.class.getResource("Test2.jar").toURI()));

Can anyone shed light on this?

Community
  • 1
  • 1
TNT
  • 2,900
  • 3
  • 23
  • 34
  • AFAIK you **cannot** execute a jar within a jar, why do you want to? – Elliott Frisch Jan 03 '15 at 01:37
  • Simply put, I have this project originally created into a jar that I revamped and modified (and I made the new version into a jar as well), and I wanted to give users the option to use the previous version by programmatically running the old jar file. – TNT Jan 03 '15 at 01:45
  • The jar seems to be in your project folder but is it packaged into your final jar? – Benoît Dubreuil Jan 03 '15 at 01:46
  • 1
    @user2924010 Do you mean packaged with all the other classes and the manifest file? If so, yes. – TNT Jan 03 '15 at 01:49
  • I meant like putting the jar in a in the other jar. A jar is a zip. So if you put lib.jar into final.jar, maybe it'll work. – Benoît Dubreuil Jan 03 '15 at 01:53
  • I believe OP wants to programmatically select his entry-point. Obviously you might select an alternate `Main-Class` from the command line, but I don't think you can embed a jar within a jar and execute the embedded jar from the jar it is embedded in... at least, not without writing a custom container. – Elliott Frisch Jan 03 '15 at 02:04

0 Answers0