Java Applet - how to determine if external jar file exist on client machine
Good afternoon
I'm in the search of a solid way to determine if an external jar exist on the client machine, who're going to use my java applet. Right now what I'm doing is this in pseudo code
try
{
controller.init()
}
catch Exception(Jar does not exist)
{
make pretty screen to tell users to download setup file
fire up browser with link to setup file
}
However the exception that is thrown at me is UnsatisfiedLinkError which I can't catch as it's not something that's recoverable. This basically means I'm "left" with a browser window with the link to the setup file, but no explanation as to why it has come up.
Of course an "easy" fix is just to add to the html page that hosts the applet that "If Java blows up, please just download the setup file that is suggested", however I would much rather like a programmatical solution, and it's here you guys and gal开发者_运维技巧s come into the picture :)
Thanks in advance for any and all answers.
/Jeb
You can check whether the jar exists in the classpath (System.getProperty("java.class.path")
).
Another approach is to see if you can load the class file resource. Say you are going to load com.thirdparty.SomeClass
. Then, in whichever class that is calling controller.init()
you can try
this.getClass().getCloassLoader.findResource("com.thirdparty.SomeClass.class")
this.getClass().getClassLoader().getResource("com.thirdparty.SomeClass.class")
If the return value is not null then the class loading should hopefully succeed.
Of course, the classes in jar may themselves depend on other classes...you have to decide how you want to handle that situation.
精彩评论