Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Check If JAR Exists Before Importing

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
4 replies to this topic

#1 Code Fox   GDNet+   

3043
Like
0Likes
Like

Posted 05 December 2012 - 02:57 PM

I have run into an issue while making a plugin that is dependent on another plugin.
How do I check to see if a .jar exists BEFORE importing it - and if it exists, than import it ? The other plugin has 3 different versions, each one is incompatible with the other versions.
Note: the other plugin actively runs on the server, so including copies of it inside of my own .jar will not work - and I have no clue which version each server has.

Any help would be greatly appreciated.

Edited by Shippou, 05 December 2012 - 02:57 PM.

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Code_Fox_Sig.png


#2 dmatter   Members   

4648
Like
1Likes
Like

Posted 06 December 2012 - 02:27 PM

I'm not entirely clear what you're trying to do exactly.

If you're trying to detect the presence of a jar file itself then you could check the filesystem, if you know where to look.

Alternatively perhaps what you want is to check whether a particular class, which may well happen to be packaged in that jar, is available on the classpath or classloader chain. In which case you could use Class.forName(String) and see whether it throws a ClassNotFoundException.

#3 Code Fox   GDNet+   

3043
Like
0Likes
Like

Posted 06 December 2012 - 05:00 PM

If plugin exists - import it.
Trying to import a non existent .jar, will auto crash.

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Code_Fox_Sig.png


#4 Code Fox   GDNet+   

3043
Like
0Likes
Like

Posted 10 December 2012 - 05:28 AM

After a long time, I found the answer to my own question.
The way to load classes dynamically is to use a ClassLoader. Here is a good reference for how to do this. http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Code_Fox_Sig.png


#5 fae   Members   

214
Like
0Likes
Like

Posted 10 December 2012 - 12:50 PM

Also before writing your own class loader I'd recommend you check out URLClassLoader, see http://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html

With URL class loader you can load files from directories or jars in your file system, e.g.
[source lang="java"]URLClassLoader classLoader = new URLClassLoader(new URL[]{ new URL("file:///pathToMy.jar") }, ClassLoader.getSystemClassLoader());Object instance = classLoader.loadClass("package.AndNameOfMyClass").newInstance();[/source]




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.