2

I'd like to ask, is there a way for a java program to see if package x is loaded? I've been making mods (for minecraft), and I have realised that almost all of my programs need to use the same methods. So I want to make an API. But I need to make sure that if the API isn't loaded, the program won't work.

Cory Kendall
  • 7,195
  • 8
  • 37
  • 64
Andrew Wong
  • 115
  • 10

3 Answers3

3

You can only check for whether a given class is available on the Classpath, but that might be good enough for what you need.

Use Class.forName(...) for this.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
1

As it already says in this post you could check if a class of the API can be loaded. If this is the case, the packages are available. But if the API can't be loaded, the program won't work anyway, so you could simply check for Exceptions the first time you access the API.

Community
  • 1
  • 1
GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • Oh ok, so like using a try block? – Andrew Wong Apr 14 '13 at 09:03
  • 1
    exactly! So when your program starts, you can either check explicitly for the availability of the API, or you could put your first parts of the initialization in a try–catch block and let the program exit *gracefully* if a `ClassNotFoundException` gets thrown. – GameDroids Apr 14 '13 at 09:08
0

I'd like to ask, is there a way for a java program to see if package x is loaded?

No. There is no such thing as loading a package in Java.

user207421
  • 305,947
  • 44
  • 307
  • 483